Skip to content

Commit 276b067

Browse files
Merge pull request #23 from gemini-testing/HERMIONE-119.migrate_to_wdio7
feat: migrate to webdriverio@7
2 parents 64de755 + 6de8d7f commit 276b067

31 files changed

+277
-390
lines changed

lib/command-helpers/context-switcher.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@ exports.runInNativeContext = async function(browser, action, testCtx) {
1212
return action.fn.call(browser, ...[].concat(action.args));
1313
}
1414

15-
await browser.context(NATIVE_CONTEXT);
15+
await browser.switchContext(NATIVE_CONTEXT);
1616
testCtx[IS_NATIVE_CTX] = true;
1717

1818
const result = await action.fn.call(browser, ...[].concat(action.args));
1919

20-
await browser.context(browser.options[WEB_VIEW_CTX]);
20+
await browser.switchContext(browser.options[WEB_VIEW_CTX]);
2121
testCtx[IS_NATIVE_CTX] = false;
2222

2323
return result;

lib/command-helpers/element-utils/<v15.0/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const {TOP_TOOLBAR_SIZE} = require('../../test-context');
88
module.exports = class SafariOldUtils extends CommonUtils {
99
async getTopToolbarHeight(browser) {
1010
const {TOP_TOOLBAR} = this._nativeLocators;
11-
const action = {fn: browser.getElementSize, args: TOP_TOOLBAR, default: {width: 0, height: 0}};
11+
const action = {fn: this.getElementSize, args: [browser, TOP_TOOLBAR], default: {width: 0, height: 0}};
1212
const existingWrapper = {fn: withExisting, args: action};
1313
const inNativeCtxWrapper = {fn: withNativeCtx, args: existingWrapper};
1414

lib/command-helpers/element-utils/common/index.js

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,44 @@
11
'use strict';
22

3-
const _ = require('lodash');
43
const {withExisting, withNativeCtx, withTestCtxMemo} = require('../decorators');
54
const {WEB_VIEW_SIZE, BOTTOM_TOOLBAR_LOCATION, PIXEL_RATIO} = require('../../test-context');
6-
const {isWdioLatest} = require('../../../utils');
75

86
module.exports = class CommonUtils {
97
constructor(nativeLocators) {
108
this._nativeLocators = nativeLocators;
119
}
1210

11+
async getLocation(browser, selector) {
12+
const elem = await browser.$(selector);
13+
return elem.getLocation();
14+
}
15+
16+
async getElementSize(browser, selector) {
17+
const elem = await browser.$(selector);
18+
return elem.getSize();
19+
}
20+
1321
async getBottomToolbarY(browser) {
1422
const {BOTTOM_TOOLBAR} = this._nativeLocators;
15-
const action = {fn: browser.getLocation, args: BOTTOM_TOOLBAR, default: {x: 0, y: 0}};
23+
const action = {fn: this.getLocation, args: [browser, BOTTOM_TOOLBAR], default: {x: 0, y: 0}};
1624
const existingWrapper = {fn: withExisting, args: action};
1725
const inNativeCtxWrapper = {fn: withNativeCtx, args: existingWrapper};
1826

1927
return (await withTestCtxMemo.call(browser, inNativeCtxWrapper, BOTTOM_TOOLBAR_LOCATION)).y;
2028
}
2129

22-
async getWebViewSize(browser) {
30+
getWebViewSize(browser) {
2331
const {WEB_VIEW} = this._nativeLocators;
24-
const action = {fn: browser.getElementSize, args: WEB_VIEW};
32+
const action = {fn: this.getElementSize, args: [browser, WEB_VIEW]};
2533
const inNativeCtxWrapper = {fn: withNativeCtx, args: action};
2634

27-
return await withTestCtxMemo.call(browser, inNativeCtxWrapper, WEB_VIEW_SIZE);
35+
return withTestCtxMemo.call(browser, inNativeCtxWrapper, WEB_VIEW_SIZE);
2836
}
2937

3038
async getElemCoords(browser, selector) {
31-
const [size, location] = await Promise.all([browser.getElementSize(selector), browser.getLocation(selector)]);
32-
const {width, height} = _.isArray(size) ? size[0] : size;
33-
// wdio returns elements in reverse order, so we need to take the last element in the array to pick first element on the page
34-
// https://github.com/webdriverio/webdriverio/blob/v4.14.1/lib/commands/getLocation.js#L48.
35-
const {x, y} = _.isArray(location) ? location[location.length - 1] : location;
39+
const [size, location] = await Promise.all([this.getElementSize(browser, selector), this.getLocation(browser, selector)]);
40+
const {width, height} = size;
41+
const {x, y} = location;
3642

3743
const topToolbarHeight = await this.getTopToolbarHeight(browser);
3844

@@ -48,14 +54,12 @@ module.exports = class CommonUtils {
4854
};
4955
}
5056

51-
async getPixelRatio(browser) {
52-
const action = {fn: async () => {
53-
const result = await browser.execute(() => window.devicePixelRatio);
54-
55-
return isWdioLatest(browser) ? result : result.value;
57+
getPixelRatio(browser) {
58+
const action = {fn: () => {
59+
return browser.execute(() => window.devicePixelRatio);
5660
}};
5761

58-
return await withTestCtxMemo.call(browser, action, PIXEL_RATIO);
62+
return withTestCtxMemo.call(browser, action, PIXEL_RATIO);
5963
}
6064

6165
async calcWebViewCoords(browser, {bodyWidth, pixelRatio = 1} = {}) {

lib/command-helpers/element-utils/decorators.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@ const {runInNativeContext} = require('../context-switcher');
1414
* @returns {Promise}
1515
*/
1616
exports.withExisting = async function(action) {
17-
const locator = _.isArray(action.args) ? action.args[0] : action.args;
18-
const isExisting = await this.isExisting(locator);
17+
const locator = _.isArray(action.args) ? action.args[1] : action.args;
18+
const elem = await this.$(locator);
19+
const isExisting = await elem.isExisting();
1920

2021
if (!isExisting) {
2122
return action.default;

lib/command-helpers/element-utils/v15.0/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const {TOP_TOOLBAR_SIZE} = require('../../test-context');
88
module.exports = class Safari15Utils extends CommonUtils {
99
async getTopToolbarHeight(browser) {
1010
const {VIEW_PORT} = this._nativeLocators;
11-
const action = {fn: browser.getLocation, args: VIEW_PORT, default: {x: 0, y: 0}};
11+
const action = {fn: this.getLocation, args: [browser, VIEW_PORT], default: {x: 0, y: 0}};
1212
const existingWrapper = {fn: withExisting, args: action};
1313
const inNativeCtxWrapper = {fn: withNativeCtx, args: existingWrapper};
1414

lib/commands/click.js

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
'use strict';
22

33
module.exports = (browser) => {
4-
const baseClickFn = browser.click;
5-
6-
browser.addCommand('click', (selector, opts = {}) => {
4+
browser.overwriteCommand('click', async function(baseClickFn, opts = {}) {
75
return opts.unwrap
8-
? baseClickFn.call(browser, selector)
9-
: browser.touch(selector);
6+
? baseClickFn()
7+
: this.touch();
108
}, true);
9+
10+
// TODO
11+
if (browser.click) {
12+
browser.overwriteCommand('click', async function(baseClickFn, selector, opts) {
13+
const elem = await browser.$(selector);
14+
return elem.click(opts);
15+
});
16+
}
1117
};

lib/commands/deviceClickBack.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,14 @@
33
const {runInNativeContext} = require('../command-helpers/context-switcher');
44

55
module.exports = (browser, {nativeLocators}) => {
6-
browser.addCommand('deviceClickBack', async () => {
6+
browser.addCommand('deviceClickBack', async function() {
77
const {DEVICE_BACK} = nativeLocators;
8-
const action = {fn: browser.click, args: [DEVICE_BACK, {unwrap: true}]};
8+
async function clickDeviceBack(opts = {}) {
9+
const deviceBackButton = await this.$(DEVICE_BACK);
10+
return deviceBackButton.click(opts);
11+
}
12+
const action = {fn: clickDeviceBack, args: [{unwrap: true}]};
913

1014
await runInNativeContext(browser, action);
11-
}, true);
15+
});
1216
};

lib/commands/dragAndDrop.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
const {WAIT_BETWEEN_ACTIONS_IN_MS} = require('../constants');
44

55
module.exports = (browser, {elementUtils}) => {
6-
browser.addCommand('dragAndDrop', async (srcSelector, destSelector) => {
7-
const {x: srcX, y: srcY} = await elementUtils.getElemCenterLocation(browser, srcSelector);
6+
browser.overwriteCommand('dragAndDrop', async function(baseDragAndDropFn, destSelector) {
7+
const {x: srcX, y: srcY} = await elementUtils.getElemCenterLocation(browser, this.selector);
88
const {x: destX, y: destY} = await elementUtils.getElemCenterLocation(browser, destSelector);
99

1010
await browser.touchAction([

lib/commands/orientation.js

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,13 @@
11
'use strict';
22

33
const {resetTestContextValues, TOP_TOOLBAR_SIZE, BOTTOM_TOOLBAR_LOCATION, WEB_VIEW_SIZE} = require('../command-helpers/test-context');
4-
const {isWdioLatest} = require('../utils');
54

65
module.exports = (browser) => {
7-
const commandName = isWdioLatest(browser) ? 'setOrientation' : 'orientation';
8-
const baseOrientationFn = browser[commandName];
9-
10-
browser.addCommand(commandName, async (orientation) => {
6+
browser.overwriteCommand('setOrientation', async (baseOrientationFn, orientation) => {
117
if (orientation && browser.executionContext) {
128
resetTestContextValues(browser.executionContext, [TOP_TOOLBAR_SIZE, BOTTOM_TOOLBAR_LOCATION, WEB_VIEW_SIZE]);
139
}
1410

15-
return baseOrientationFn.call(browser, orientation);
16-
}, true);
11+
return baseOrientationFn(orientation);
12+
});
1713
};

lib/commands/screenshot.js

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,18 @@ const {PNG} = require('pngjs');
44
const concat = require('concat-stream');
55
const streamifier = require('streamifier');
66
const {runInNativeContext} = require('../command-helpers/context-switcher');
7-
const {isWdioLatest} = require('../utils');
87

98
module.exports = (browser, {elementUtils}) => {
10-
const commandName = isWdioLatest(browser) ? 'takeScreenshot' : 'screenshot';
11-
const baseScreenshotFn = browser[commandName];
12-
13-
browser.addCommand(commandName, async () => {
14-
const {width: bodyWidth} = await browser.getElementSize('body');
9+
browser.overwriteCommand('takeScreenshot', async (baseScreenshotFn) => {
10+
const {width: bodyWidth} = await elementUtils.getElementSize(browser, 'body');
1511
const pixelRatio = await elementUtils.getPixelRatio(browser);
1612
const cropCoords = await runInNativeContext(browser, {fn: elementUtils.calcWebViewCoords.bind(elementUtils), args: [browser, {bodyWidth, pixelRatio}]});
17-
const screenshotResult = await baseScreenshotFn.call(browser);
18-
const base64 = isWdioLatest(browser) ? screenshotResult : screenshotResult.value;
13+
const screenshotResult = await baseScreenshotFn();
1914

2015
return new Promise((resolve, reject) => {
2116
const handleError = (msg) => (err) => reject(`Error occured while ${msg}: ${err.message}`);
2217

23-
streamifier.createReadStream(Buffer.from(base64, 'base64'))
18+
streamifier.createReadStream(Buffer.from(screenshotResult, 'base64'))
2419
.on('error', handleError('converting buffer to readable stream'))
2520
.pipe(new PNG())
2621
.on('error', handleError('writing buffer to png data'))
@@ -37,12 +32,11 @@ module.exports = (browser, {elementUtils}) => {
3732
.on('error', handleError('packing png data to buffer'))
3833
.pipe(concat((buffer) => {
3934
const strBase64 = buffer.toString('base64');
40-
const result = isWdioLatest(browser) ? strBase64 : {value: strBase64};
4135

42-
resolve(result);
36+
resolve(strBase64);
4337
}))
4438
.on('error', handleError('concatenating png data to a single buffer'));
4539
});
4640
});
47-
}, true);
41+
});
4842
};

0 commit comments

Comments
 (0)