Skip to content

Commit

Permalink
feat: add getElementPixelColor function
Browse files Browse the repository at this point in the history
  • Loading branch information
Miki-Session committed Jan 15, 2025
1 parent 3d34a4c commit 312cc67
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 19 deletions.
20 changes: 8 additions & 12 deletions run/test/specs/check_avatar_color.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { USERNAME } from '../../types/testing';
import { newUser } from './utils/create_account';
import { newContact } from './utils/create_contact';
import { SupportedPlatformsType, closeApp, openAppTwoDevices } from './utils/open_app';
import { isSameColor, parseDataImage } from './utils/check_colour';
import { isSameColor } from './utils/check_colour';
import { UserSettings } from './locators/settings';
import { ConversationItem } from './locators/home';
import { ConversationAvatar, ConversationSettings } from './locators/conversation';
Expand All @@ -19,22 +19,18 @@ async function avatarColor(platform: SupportedPlatformsType) {
await newContact(platform, device1, userA, device2, userB);
await Promise.all([device1.navigateBack(), device2.navigateBack()]);
// Get Alice's avatar color on device 1 (Home Screen avatar) and turn it into a hex value
const device1Avatar = await device1.waitForTextElementToBePresent(new UserSettings(device1));
const device1Base64 = await device1.getElementScreenshot(device1Avatar.ELEMENT);
const device1PixelColor = await parseDataImage(device1Base64);
const device1PixelColor = await device1.getElementPixelColor(new UserSettings(device1));
// Get Alice's avatar color on device 2 and turn it into a hex value
await device2.clickOnElementAll(new ConversationItem(device2));
let device2Avatar;
// The conversation screen looks slightly different per platform:
// On iOS the avatar doubles as the Conversation Settings button
// On Android, the avatar is a separate, non-interactable element (and the settings has the 3-dot icon)
let device2PixelColor;
// The conversation screen looks slightly different per platform so we're grabbing the avatar from different locators
// On iOS the avatar doubles as the Conversation Settings button on the right
// On Android, the avatar is a separate, non-interactable element on the left (and the settings has the 3-dot icon)
if (platform === 'ios') {
device2Avatar = await device2.waitForTextElementToBePresent(new ConversationSettings(device2));
device2PixelColor = await device2.getElementPixelColor(new ConversationSettings(device2));
} else {
device2Avatar = await device2.waitForTextElementToBePresent(new ConversationAvatar(device2));
device2PixelColor = await device2.getElementPixelColor(new ConversationAvatar(device2));
}
const device2Base64 = await device2.getElementScreenshot(device2Avatar.ELEMENT);
const device2PixelColor = await parseDataImage(device2Base64);
// Color matching devices 1 and 2
const colorMatch = isSameColor(device1PixelColor, device2PixelColor);
if (!colorMatch) {
Expand Down
10 changes: 3 additions & 7 deletions run/test/specs/linked_device_avatar_color.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { bothPlatformsIt } from '../../types/sessionIt';
import { USERNAME } from '../../types/testing';
import { linkedDevice } from './utils/link_device';
import { SupportedPlatformsType, closeApp, openAppTwoDevices } from './utils/open_app';
import { isSameColor, parseDataImage } from './utils/check_colour';
import { isSameColor } from './utils/check_colour';
import { UserSettings } from './locators/settings';

bothPlatformsIt('Avatar color linked device', 'medium', avatarColorLinkedDevice);
Expand All @@ -11,13 +11,9 @@ async function avatarColorLinkedDevice(platform: SupportedPlatformsType) {
const { device1, device2 } = await openAppTwoDevices(platform);
const userA = await linkedDevice(device1, device2, USERNAME.ALICE);
// Get Alice's avatar color on device 1 (Home Screen avatar) and turn it into a hex value
const device1Avatar = await device1.waitForTextElementToBePresent(new UserSettings(device1));
const device1Base64 = await device1.getElementScreenshot(device1Avatar.ELEMENT);
const device1PixelColor = await parseDataImage(device1Base64);
const device1PixelColor = await device1.getElementPixelColor(new UserSettings(device1));
// Get Alice's avatar color on the linked device (Home Screen avatar) and turn it into a hex value
const device2Avatar = await device2.waitForTextElementToBePresent(new UserSettings(device2));
const device2Base64 = await device2.getElementScreenshot(device2Avatar.ELEMENT);
const device2PixelColor = await parseDataImage(device2Base64);
const device2PixelColor = await device2.getElementPixelColor(new UserSettings(device2));
// Color matching devices 1 and 2
const colorMatch = isSameColor(device1PixelColor, device2PixelColor);
if (!colorMatch) {
Expand Down
15 changes: 15 additions & 0 deletions run/types/DeviceWrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import {
User,
XPath,
} from './testing';
import { parseDataImage } from '../test/specs/utils/check_colour';

export type Coordinates = {
x: number;
Expand Down Expand Up @@ -1787,6 +1788,20 @@ export class DeviceWrapper {
}
}

public async getElementPixelColor(
args: {
text?: string;
maxWait?: number;
} & (StrategyExtractionObj | LocatorsInterface)
): Promise<string> {
// Wait for the element to be present
const element = await this.waitForTextElementToBePresent(args);
// Take a screenshot and return a hex color value
const base64image = await this.getElementScreenshot(element.ELEMENT);
const pixelColor = await parseDataImage(base64image);
return pixelColor;
}

/* === all the utilities function === */
public isIOS(): boolean {
return isDeviceIOS(this.device);
Expand Down

0 comments on commit 312cc67

Please sign in to comment.