Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ Integrate selenium and appium session support #1239

Merged
merged 61 commits into from
May 24, 2023
Merged
Show file tree
Hide file tree
Changes from 56 commits
Commits
Show all changes
61 commits
Select commit Hold shift + click to select a range
51bf525
[PPLT-2008] POC CLI
Amit3200 Apr 10, 2023
3055888
[refactor] remove selenium-webdriver dependency in poa
nilshah98 Apr 18, 2023
d625419
[refactor] rename driver2 -> driver
nilshah98 Apr 18, 2023
2e406ed
[refactor] remove all usage of metaData
nilshah98 Apr 18, 2023
6d4c8a4
[refactor] rename getTag1 -> getTag
nilshah98 Apr 18, 2023
d306ef8
[chore] remove unused metaData resolvers
nilshah98 Apr 18, 2023
38707d0
[feat] add screenshot feature for driverWrapper
nilshah98 Apr 19, 2023
86f5253
[refactor] use await in function calls + use fs/promises
nilshah98 Apr 19, 2023
6035b7a
[refactor] remove un-necessary async-await in metadata resolvers
nilshah98 Apr 19, 2023
1923ede
[wip] added genericProvider
nilshah98 Apr 19, 2023
db8b2fd
[feat] added support for automateprovider + debugUrl
nilshah98 Apr 21, 2023
cca6bae
[chore] update packages.json
nilshah98 Apr 21, 2023
4a0c2c4
[chore] remove snapshotName from constructor initialization
nilshah98 Apr 21, 2023
b2b6810
[chore] rename endpoint driverWrapper -> automateScreenshot
nilshah98 Apr 21, 2023
c8c2d12
[refactor] move driverResolver/driverWrapper -> driver.js
nilshah98 Apr 21, 2023
0b78bf7
[refactor] use cli-poa/main.js only for import and export, use Automa…
nilshah98 Apr 21, 2023
fa873b2
[chore] lint using --fix flag
nilshah98 Apr 21, 2023
2014e5f
[chore] remove constructor from automateProvider as it is just initia…
nilshah98 Apr 21, 2023
fb49fb1
[test] instantiate tests
nilshah98 Apr 24, 2023
2d34329
[test] verify browserstackExecutor passed correct arguments
nilshah98 Apr 25, 2023
ca456af
[fix] await on response.json()
nilshah98 Apr 25, 2023
e5efcd4
[chore] add test and test:coverage scripts
nilshah98 Apr 25, 2023
d377484
[test] update automateProvider coverage to 100%
nilshah98 Apr 25, 2023
90c5721
[test] add tests for providerResolver
nilshah98 Apr 25, 2023
c3a7063
[test] add commonMetaDataResolver.test.js
nilshah98 Apr 25, 2023
0556687
[test] added driver check for commonMetaDataResolver
nilshah98 Apr 25, 2023
46b0fa4
[test] added desktop and mobile metadata resolver tests
nilshah98 Apr 25, 2023
18dbc54
[chore] lint using lint:fix
nilshah98 Apr 25, 2023
e34bb7d
[test] added genericProvider tests
nilshah98 Apr 26, 2023
0664df1
[chore] lint using lint:fix
nilshah98 Apr 26, 2023
016ddd8
[refactor] move automateScreenshot to poa
nilshah98 Apr 28, 2023
e5f26eb
[refactor] rename cli-poa to selenium-utils
nilshah98 Apr 28, 2023
e598b8c
[refactor] rename selenium-utils -> webdriver-utils
nilshah98 Apr 28, 2023
df08c52
[refactor] instantiate logging in individual files
nilshah98 Apr 28, 2023
6ce4a33
[chore] updated webdriver-utils package.json
nilshah98 May 3, 2023
cb87112
[feat] updated getTag as per BYOS contract
nilshah98 May 3, 2023
d9d1a63
[feat] update test.yml + move from main.js to index.js for tests to r…
nilshah98 May 3, 2023
035ace3
[chore] lint using yarn lint --fix
nilshah98 May 3, 2023
6a08f7e
[chore] fix lint errors
nilshah98 May 3, 2023
9170a5b
[feat] add support for adding individual coverage threshold levels
nilshah98 May 3, 2023
2a9a116
[chore] rename commonMetadata to Metadata
nilshah98 May 4, 2023
433a98f
[fix] update content-type headers for executeScript call
nilshah98 May 4, 2023
17c2c91
[fix] handle embedded url
nilshah98 May 4, 2023
359a405
[feat] use sdk-utils/requests instead of node-fetch
nilshah98 May 6, 2023
8b39ec9
[feat] call percy.upload(...) directly which skips latency due to ser…
nilshah98 May 6, 2023
f34b79e
[refactor] pass base64 data directly instead of writing to temp files
nilshah98 May 6, 2023
c75cfc2
[chore] yarn lint fix
nilshah98 May 6, 2023
158b452
[refactor] use ternary opeartor instead of if-else
nilshah98 May 6, 2023
0016b82
[fix] refactor conditional imports
nilshah98 May 6, 2023
9887349
[refactor] switch to importing default and extract props from it
nilshah98 May 6, 2023
30c929c
[test] added tests for webdriver-utils/driver
nilshah98 May 6, 2023
ae3f9a6
[test] added genericProvider tests
nilshah98 May 8, 2023
241f2d8
[chore] removed fallback parameters as we always pass some capabilities
nilshah98 May 8, 2023
6eae220
[refactor] remove customized threshold support for nyc
nilshah98 May 8, 2023
4c0830d
[chore] lint using fix
nilshah98 May 8, 2023
b207f99
[feat] added cache support
nilshah98 May 8, 2023
94289c1
[test] add cache, genericProvider tests for webdriver-utils
nilshah98 May 12, 2023
e351591
[refactor] webdriverutils to class based structure for better testabi…
nilshah98 May 19, 2023
5323de9
[test] added api.test.js
nilshah98 May 19, 2023
3f164a2
[test] setup https server + tests for requests
nilshah98 May 19, 2023
91492dd
[chore] lint using lint --fix
nilshah98 May 19, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ jobs:
- '@percy/cli-build'
- '@percy/cli-config'
- '@percy/sdk-utils'
- '@percy/webdriver-utils'
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v3
Expand Down
5 changes: 5 additions & 0 deletions packages/core/src/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { createRequire } from 'module';
import logger from '@percy/logger';
import { normalize } from '@percy/config/utils';
import { getPackageJSON, Server } from './utils.js';
// TODO Remove below esline disable once we publish webdriver-util
import automateScreenshot from '@percy/webdriver-utils'; // eslint-disable-line import/no-extraneous-dependencies

// need require.resolve until import.meta.resolve can be transpiled
export const PERCY_DOM = createRequire(import.meta.url).resolve('@percy/dom');
Expand Down Expand Up @@ -115,6 +117,9 @@ export function createPercyServer(percy, port) {
.route('post', '/percy/flush', async (req, res) => res.json(200, {
success: await percy.flush(req.body).then(() => true)
}))
.route('post', '/percy/automateScreenshot', async (req, res) => res.json(200, {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

automateScreenshot should we keep it in camel case instead we can call it on screenshot as well just saying because url's in general don't have camel casing.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's rename it.

success: await (percy.upload(await automateScreenshot(req.body))).then(() => true)
}))
// stops percy at the end of the current event loop
.route('/percy/stop', (req, res) => {
setImmediate(() => percy.stop());
Expand Down
7 changes: 5 additions & 2 deletions packages/sdk-utils/src/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import percy from './percy-info.js';

// Helper to send a request to the local CLI API
export async function request(path, options = {}) {
let response = await request.fetch(`${percy.address}${path}`, options);
let url = path.startsWith('http') ? path : `${percy.address}${path}`;
let response = await request.fetch(url, options);

// maybe parse response body as json
if (typeof response.body === 'string' &&
Expand Down Expand Up @@ -48,7 +49,9 @@ if (process.env.__PERCY_BROWSERIFIED__) {
} else {
// use http.request in node
request.fetch = async function fetch(url, options) {
let { default: http } = await import('http');
let { protocol } = new URL(url);
// rollup throws error for -> await import(protocol === 'https:' ? 'https' : 'http')
let { default: http } = protocol === 'https:' ? await import('https') : await import('http');

return new Promise((resolve, reject) => {
http.request(url, options)
Expand Down
148 changes: 148 additions & 0 deletions packages/webdriver-utils/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 30 additions & 0 deletions packages/webdriver-utils/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
nilshah98 marked this conversation as resolved.
Show resolved Hide resolved
"name": "@percy/webdriver-utils",
"version": "1.22.0",
"license": "MIT",
"repository": {
"type": "git",
"url": "https://github.com/percy/cli",
"directory": "packages/webdriver-utils"
},
"engines": {
"node": ">=14"
},
"files": [
"dist"
],
"main": "./dist/index.js",
"type": "module",
"exports": {
".": "./dist/index.js"
},
"scripts": {
"build": "node ../../scripts/build",
"lint": "eslint --ignore-path ../../.gitignore .",
"test": "node ../../scripts/test",
"test:coverage": "yarn test --coverage"
nilshah98 marked this conversation as resolved.
Show resolved Hide resolved
},
"dependencies": {
"@percy/sdk-utils": "1.22.0"
}
}
51 changes: 51 additions & 0 deletions packages/webdriver-utils/src/driver.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import utils from '@percy/sdk-utils';
import Cache from './util/cache.js';
const { request } = utils;

export default class Driver {
constructor(sessionId, executorUrl) {
this.sessionId = sessionId;
this.executorUrl = executorUrl.includes('@') ? `https://${executorUrl.split('@')[1]}` : executorUrl;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also can we add this check below this 😅

this.executorUrl = executorUrl.endsWith('/') ? this.executorUrl.slice(0, -1) : this.executorUrl;

It turns out that C# ends up adding / at the end which causes the request to fail so if we add this check then C# test passes.

}

async getCapabilites() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use cache class from appium js sdk here. It becomes pretty expensive to fetch capabilities per screenshot [ ideally you only want to do it once per sessionId ]

return await Cache.withCache(Cache.caps, this.sessionId, async() => {
const baseUrl = `${this.executorUrl}/session/${this.sessionId}`;
const caps = JSON.parse((await request(baseUrl)).body);
return caps.value;
})
}

async getWindowSize() {
const baseUrl = `${this.executorUrl}/session/${this.sessionId}/window/current/size`;
const windowSize = JSON.parse((await request(baseUrl)).body);
return windowSize;
}

// command => {script: "", args: []}
async executeScript(command) {
if ((!command.constructor === Object) ||
!(Object.keys(command).length === 2 &&
Object.keys(command).includes('script') &&
Object.keys(command).includes('args'))
) {
throw new Error('Please pass command as {script: "", args: []}');
}
const options = {
method: 'POST',
headers: {
'Content-Type': 'application/json;charset=utf-8'
},
body: JSON.stringify(command)
};
const baseUrl = `${this.executorUrl}/session/${this.sessionId}/execute/sync`;
const response = JSON.parse((await request(baseUrl, options)).body);
return response;
}

async takeScreenshot() {
const baseUrl = `${this.executorUrl}/session/${this.sessionId}/screenshot`;
const screenShot = JSON.parse((await request(baseUrl)).body);
return screenShot.value;
}
}
11 changes: 11 additions & 0 deletions packages/webdriver-utils/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import ProviderResolver from './providers/providerResolver.js';
import utils from '@percy/sdk-utils';

const log = utils.logger('webdriver-utils:main');

export default async function automateScreenshot(options) {
log.info('Starting automate screenshot');
const automate = ProviderResolver.resolve(options.sessionId, options.commandExecutorUrl, options.capabilities, options.sessionCapabilites);
await automate.createDriver();
return await automate.screenshot(options.snapshotName);
}
43 changes: 43 additions & 0 deletions packages/webdriver-utils/src/metadata/desktopMetaData.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
export default class DesktopMetaData {
constructor(driver, opts) {
this.driver = driver;
this.capabilities = opts;
}

browserName() {
return this.capabilities.browserName.toLowerCase();
}

osName() {
let osName = this.capabilities.osVersion;
if (osName) return osName.toLowerCase();

osName = this.capabilities.platform;
return osName;
}

// desktop will show this as browser version
osVersion() {
return this.capabilities.version.split('.')[0];
}

deviceName() {
return this.browserName() + '_' + this.osVersion() + '_' + this.osName();
}

orientation() {
return 'landscape';
}

async windowSize() {
const dpr = await this.devicePixelRatio();
const data = await this.driver.getWindowSize();
const width = parseInt(data.value.width * dpr), height = parseInt(data.value.height * dpr);
return { width, height };
}

async devicePixelRatio() {
const devicePixelRatio = await this.driver.executeScript({ script: 'return window.devicePixelRatio;', args: [] });
return devicePixelRatio.value;
}
}
15 changes: 15 additions & 0 deletions packages/webdriver-utils/src/metadata/metaDataResolver.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import DesktopMetaData from './desktopMetaData.js';
import MobileMetaData from './mobileMetaData.js';

export default class MetaDataResolver {
static resolve(driver, capabilities, opts) {
if (!driver) throw new Error('Please pass a Driver object');

const platform = opts.platformName || opts.platform;
if (['ios', 'android'].includes(platform.toLowerCase())) {
return new MobileMetaData(driver, capabilities);
} else {
return new DesktopMetaData(driver, capabilities);
}
}
}
42 changes: 42 additions & 0 deletions packages/webdriver-utils/src/metadata/mobileMetaData.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
export default class MobileMetaData {
constructor(driver, opts) {
this.driver = driver;
this.capabilities = opts;
}

browserName() {
return this.capabilities.browserName.toLowerCase();
}

osName() {
let osName = this.capabilities.os.toLowerCase();
if (osName === 'mac' && this.browserName() === 'iphone') {
osName = 'ios';
}
return osName;
}

osVersion() {
return this.capabilities.osVersion.split('.')[0];
}

deviceName() {
return this.capabilities.deviceName.split('-')[0];
}

orientation() {
return this.capabilities.orientation;
}

async windowSize() {
const dpr = await this.devicePixelRatio();
const data = await this.driver.getWindowSize();
const width = parseInt(data.value.width * dpr), height = parseInt(data.value.height * dpr);
return { width, height };
}

async devicePixelRatio() {
const devicePixelRatio = await this.driver.executeScript({ script: 'return window.devicePixelRatio;', args: [] });
return devicePixelRatio.value;
}
}
Loading