Skip to content

Commit

Permalink
chore(appium): replace nut js library
Browse files Browse the repository at this point in the history
  • Loading branch information
luisecm committed May 4, 2024
1 parent 3571f53 commit 307652f
Show file tree
Hide file tree
Showing 12 changed files with 541 additions and 1,215 deletions.
1,631 changes: 485 additions & 1,146 deletions package-lock.json

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@
],
"devDependencies": {
"@faker-js/faker": "^8.4.1",
"@nut-tree/libnut": "github:luisecm/libnut-core#124d71e",
"@nut-tree/nut-js": "github:luisecm/nut.js#d01410f",
"@typescript-eslint/eslint-plugin": "^7.3.1",
"@typescript-eslint/parser": "^7.3.1",
"@wdio/allure-reporter": "^8.34.0",
Expand All @@ -40,10 +38,12 @@
"appium": "^2.5.3",
"appium-mac2-driver": "^1.12.0",
"appium-windows-driver": "^2.12.21",
"clipboardy": "^4.0.0",
"eslint-plugin-wdio": "^8.24.12",
"kill-port": "^2.0.1",
"module-alias": "^2.2.3",
"prettier": "^3.2.5",
"robotjs": "^0.6.0",
"ts-node": "^10.9.2",
"typescript": "^5.4.3",
"wdio-winappdriver-service": "^0.1.1",
Expand Down
51 changes: 17 additions & 34 deletions tests/helpers/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
const { readFileSync, rmSync, writeFileSync } = require("fs");
const { execSync } = require("child_process");
const fsp = require("fs").promises;
const { clipboard, keyboard, mouse, Button } = require("@nut-tree/nut-js");
const robot = require("robotjs");

// Users cache helper functions

Expand Down Expand Up @@ -359,11 +359,6 @@ export async function getClipboardMacOS() {
return clipboard;
}

export async function getClipboardValue() {
const clipboardValue = await clipboard.getContent();
return clipboardValue;
}

export async function hoverOnMacOS(locator: WebdriverIO.Element | undefined) {
const elementId = await locator?.elementId;
await driver.executeScript("macos: hover", [
Expand All @@ -373,16 +368,12 @@ export async function hoverOnMacOS(locator: WebdriverIO.Element | undefined) {
]);
}

export async function setClipboardValue(value: string) {
await clipboard.setContent(value);
}

export async function scrollUp(deltaX: number) {
await mouse.scrollUp(deltaX);
await robot.scrollMouse(deltaX, 0);
}

export async function scrollDown(deltaX: number) {
await mouse.scrollDown(deltaX);
await robot.scrollMouse(-deltaX, 0);
}

export async function saveFileOnMacOS(filename: string) {
Expand Down Expand Up @@ -451,7 +442,7 @@ export async function leftClickOnMacOS(locator: WebdriverIO.Element) {
elementId: elementId,
},
]);
await mouse.click(Button.LEFT);
await robot.mouseClick();
}

export async function rightClickOnMacOS(
Expand All @@ -463,7 +454,7 @@ export async function rightClickOnMacOS(
elementId: elementId,
},
]);
await mouse.click(Button.RIGHT);
await robot.mouseClick("right");
}

// Windows driver helper functions
Expand All @@ -474,14 +465,14 @@ export async function hoverOnWindows(locator: WebdriverIO.Element | undefined) {

export async function leftClickOnWindows(locator: WebdriverIO.Element) {
await driver.moveToElement(locator.elementId);
await mouse.click(Button.LEFT);
await robot.mouseClick();
}

export async function rightClickOnWindows(
locator: WebdriverIO.Element | undefined,
) {
await driver.moveToElement(locator?.elementId);
await mouse.click(Button.RIGHT);
await robot.mouseClick("right");
}

export async function saveFileOnWindows(
Expand Down Expand Up @@ -564,35 +555,27 @@ export async function getUplinkWindowHandle() {

export async function keyboardShortcutPaste() {
if (process.env.DRIVER === WINDOWS_DRIVER) {
await keyboard.type(4, 66);
await robot.keyTap("v", ["control"]);
} else if (process.env.DRIVER === MACOS_DRIVER) {
await keyboard.type(129, 66);
await robot.keyTap("v", ["command"]);
}
}

export async function keyboardShiftEnter() {
await keyboard.pressKey(7);
await keyboard.type(101);
await keyboard.releaseKey(7);
await robot.keyToggle("Enter", ["shift"]);
}

export async function pressEnterKey() {
await keyboard.type(101);
await robot.keyTap("enter");
}

export async function pressEscKey() {
await keyboard.type(1);
await robot.keyTap("escape");
}

export async function sendCustomKeybinds(...keys: number[]) {
// To find the number associated for each key to send on keybind, refer to node_modules/@nut-tree/nut-js/dist/lib/key.enum.d.ts
for (let i = 0; i < keys.length - 1; i++) {
await keyboard.pressKey(keys[i]);
}

await keyboard.type(keys[keys.length - 1]);

for (let i = keys.length - 2; i >= 0; i--) {
await keyboard.releaseKey(keys[i]);
}
export async function sendCustomKeybinds(...keys: string[]) {
const finalKey = keys.pop(); // Extract the final key
const modifierKeys = keys.filter((key) => key !== finalKey);
// Press the combination of keys
await robot.keyTap(finalKey, modifierKeys);
}
10 changes: 10 additions & 0 deletions tests/helpers/commandsClipboard.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const clipboard = require("clipboardy");

export async function getClipboardValue() {
const clipboardValue = await clipboard.readSync();
return clipboardValue;
}

export async function setClipboardValue(value: string) {
await clipboard.writeSync(value);
}
3 changes: 2 additions & 1 deletion tests/screenobjects/chats/CreateGroupChat.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require("module-alias/register");
import { keyboardShortcutPaste, setClipboardValue } from "@helpers/commands";
import { keyboardShortcutPaste } from "@helpers/commands";
import { setClipboardValue } from "@helpers/commandsClipboard";
import { faker } from "@faker-js/faker";
import { MACOS_DRIVER, WINDOWS_DRIVER } from "@helpers/constants";
import UplinkMainScreen from "@screenobjects/UplinkMainScreen";
Expand Down
4 changes: 2 additions & 2 deletions tests/screenobjects/chats/InputBar.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
require("module-alias/register");
import { faker } from "@faker-js/faker";
import { keyboard } from "@nut-tree/nut-js";
import { MACOS_DRIVER, WINDOWS_DRIVER } from "@helpers/constants";
import {
keyboardShiftEnter,
Expand All @@ -11,6 +10,7 @@ import {
} from "@helpers/commands";
import UplinkMainScreen from "@screenobjects/UplinkMainScreen";
import { selectorContainer } from "@screenobjects/AppScreen";
const robot = require("robotjs");

let SELECTORS: selectorContainer = {};

Expand Down Expand Up @@ -219,7 +219,7 @@ class InputBar extends UplinkMainScreen {
await this.typeCodeOnInputBar(language, codeToType);
} else {
await keyboardShiftEnter();
await keyboard.type(codeToType);
await robot.typeString(codeToType);
let inputTextValueCode = await inputText.getText();
inputTextValueCode += " ";
if (inputTextValueCode.includes(codeToType) === false) {
Expand Down
12 changes: 6 additions & 6 deletions tests/screenobjects/settings/SettingsKeybindsScreen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -485,13 +485,13 @@ class SettingsKeybindsScreen extends SettingsBaseScreen {
let sectionToEdit;
const currentDriver = await this.getCurrentDriver();
if (currentDriver === MACOS_DRIVER) {
sectionToEdit = await $(`~${section}-section`).$(
SELECTORS.KEYBIND_SECTION_KEYS,
);
sectionToEdit = await $(`~${section}-section`)
.$(SELECTORS.KEYBIND_SECTION_KEYS)
.$$(SELECTORS.KEYBIND_KEY)[0];
} else if (currentDriver === WINDOWS_DRIVER) {
sectionToEdit = await $(`[name="${section}-section"]`).$(
SELECTORS.KEYBIND_SECTION_KEYS,
);
sectionToEdit = await $(`[name="${section}-section"]`)
.$(SELECTORS.KEYBIND_SECTION_KEYS)
.$$(SELECTORS.KEYBIND_KEY)[0];
}
await sectionToEdit?.click();
}
Expand Down
2 changes: 1 addition & 1 deletion tests/specs/13-settings-developer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ export default async function settingsDeveloperTests() {
await WebInspector.validateWebInspectorIsShown();

// Press Ctrl + Shift + I to Close Web Inspector
await sendCustomKeybinds(4, 7, 53);
await sendCustomKeybinds("control", "shift", "i");

// Validate Web Inspector is not displayed
await WebInspector.validateWebInspectorIsNotShown();
Expand Down
34 changes: 14 additions & 20 deletions tests/specs/18-settings-keybinds.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,9 @@ export default async function settingsKeybindsTests() {
});

it("Settings Keyboards Shortcuts - Change Increase Font Size Keybind", async () => {
// Type Ctrl + Shift + =
await sendCustomKeybinds(4, 7, 73);
// // Edit Increase Font Size Keybind to Ctrl + A
await SettingsKeybindsScreen.editKeybind("increase-font-size");
// Type Ctrl + A
await sendCustomKeybinds(4, 45);
await sendCustomKeybinds("control", "a");

const increaseFontSizeKeybind =
await SettingsKeybindsScreen.getKeybinds("increase-font-size");
Expand All @@ -144,7 +142,7 @@ export default async function settingsKeybindsTests() {
await SettingsGeneralScreen.waitForIsShown(true);

// Press Ctrl + A to increase font size from 0.75 to 1.0
await sendCustomKeybinds(4, 45);
await sendCustomKeybinds("control", "a");
const valueAfterIncreasing = await SettingsGeneralScreen.fontScalingValue;
await expect(valueAfterIncreasing).toHaveText("1");
});
Expand All @@ -162,22 +160,18 @@ export default async function settingsKeybindsTests() {
});

it("Settings Keyboards Shortcuts - User can update more than one keybind", async () => {
// Change at least two different keybinds. First, change Decrease Font Size Keybind, by typing Ctrl + Shift + -
await sendCustomKeybinds(4, 7, 72);
// Edit Decrease Font Size Keybind to Ctrl + B
await SettingsKeybindsScreen.editKeybind("decrease-font-size");
await sendCustomKeybinds(4, 46);
await sendCustomKeybinds("control", "b");

// Validate change was applied correctly to Decrease Font Size Keybind
const decreaseFontSizeKeybind =
await SettingsKeybindsScreen.getKeybinds("decrease-font-size");
await expect(decreaseFontSizeKeybind).toEqual(["CONTROL", "B"]);

// Now, change Hide/Focus Uplink Keybind by typing Ctrl + Shift + U
await sendCustomKeybinds(4, 7, 65);
// Change Hide/Focus Uplink Keybind to Ctrl + Shift + Alt + P
await SettingsKeybindsScreen.editKeybind("hide-focus-uplink");
// Type Ctrl + Shift + Alt + P
await sendCustomKeybinds(4, 7, 3, 60);
await sendCustomKeybinds("control", "shift", "alt", "p");

// Validate change was applied correctly to Hide/Focus Uplink Keybind
const hideFocusUplinkKeybind =
Expand Down Expand Up @@ -229,12 +223,12 @@ export default async function settingsKeybindsTests() {
await SettingsGeneralScreen.waitForIsShown(true);

// Press Ctrl + Shift + = to increase font size from 1.0 to 1.25
await sendCustomKeybinds(4, 7, 73);
await sendCustomKeybinds("control", "shift", "=");
const valueAfterIncreasing = await SettingsGeneralScreen.fontScalingValue;
await expect(valueAfterIncreasing).toHaveText("1.25");

// Press Ctrl + Shift + Minus to decrease font size from 1.25 to 1.0
await sendCustomKeybinds(4, 7, 72);
await sendCustomKeybinds("control", "shift", "-");
const valueAfterDecreasing = await SettingsGeneralScreen.fontScalingValue;
await expect(valueAfterDecreasing).toHaveText("1");
});
Expand All @@ -243,13 +237,13 @@ export default async function settingsKeybindsTests() {
const currentDriver = await SettingsGeneralScreen.getCurrentDriver();
if (currentDriver === MACOS_DRIVER) {
// Press Ctrl + Shift + I to Open Web Inspector
await sendCustomKeybinds(4, 7, 53);
await sendCustomKeybinds("control", "shift", "i");

// Validate Web Inspector is displayed
await WebInspector.validateWebInspectorIsShown();

// Press Ctrl + Shift + I to Close Web Inspector
await sendCustomKeybinds(4, 7, 53);
await sendCustomKeybinds("control", "shift", "i");

// Validate Web Inspector is not displayed
await WebInspector.validateWebInspectorIsNotShown();
Expand All @@ -258,24 +252,24 @@ export default async function settingsKeybindsTests() {

it("Keybind Shortcuts - Validate default keybind for Developer Mode is working", async () => {
// Press Ctrl + Shift + D to Open Developer Mode
await sendCustomKeybinds(4, 7, 48);
await sendCustomKeybinds("control", "shift", "d");

// Validate Debug Logger is Displayed
await DebugLogger.validateDebugLoggerIsDisplayed();

// Press Ctrl + Shift + D to Close Developer Mode
await sendCustomKeybinds(4, 7, 48);
await sendCustomKeybinds("control", "shift", "d");

// Validate Debug Logger is Not Shown
await DebugLogger.validateDebugLoggerIsNotDisplayed();
});

it("Keybind Shortcuts - Validate default keybind for Hide/Focus Uplink is working", async () => {
// Press Ctrl + Shift + U to Hide Uplink
await sendCustomKeybinds(4, 7, 65);
await sendCustomKeybinds("control", "shift", "u");

// Press Ctrl + Shift + U to Focus Uplink
await sendCustomKeybinds(4, 7, 65);
await sendCustomKeybinds("control", "shift", "u");

// Validate Uplink is displayed
await SettingsGeneralScreen.waitForIsShown(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ require("module-alias/register");
import {
activateFirstApplication,
activateSecondApplication,
getClipboardValue,
} from "@helpers/commands";
import { getClipboardValue } from "@helpers/commandsClipboard";
import {
acceptFriendRequest,
createUserAndSaveDidKey,
Expand Down
2 changes: 1 addition & 1 deletion tests/specs/reusable-accounts/04-message-input.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import {
activateSecondApplication,
keyboardShortcutPaste,
pressEnterKey,
setClipboardValue,
} from "@helpers/commands";
import { setClipboardValue } from "@helpers/commandsClipboard";
import {
acceptFriendRequest,
createUserAndSaveDidKey,
Expand Down
1 change: 0 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
"@wdio/mocha-framework",
"expect-webdriverio",
"@wdio/globals/types",
"@nut-tree/types",
"@wdio/visual-service"
],
"baseUrl": "./",
Expand Down

0 comments on commit 307652f

Please sign in to comment.