Skip to content

Commit 6de8d7f

Browse files
feat: migrate to webdriverio@7
BREAKING CHANGE: drop support for hermione@3
1 parent 64de755 commit 6de8d7f

31 files changed

+277
-390
lines changed

lib/command-helpers/context-switcher.js

+2-2
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

+1-1
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

+21-17
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

+3-2
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

+1-1
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

+11-5
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

+7-3
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

+2-2
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

+3-7
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

+6-12
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
};

lib/commands/swipe.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,5 @@ module.exports = (browser, {elementUtils}) => {
3131
if (yOffset) {
3232
resetTestContextValues(browser.executionContext, [TOP_TOOLBAR_SIZE, BOTTOM_TOOLBAR_LOCATION]);
3333
}
34-
}, true);
34+
});
3535
};

lib/commands/touch.js

+10-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
11
'use strict';
22

33
module.exports = (browser, {elementUtils}) => {
4-
browser.addCommand('touch', async (selector) => {
5-
const {x, y} = await elementUtils.getElemCenterLocation(browser, selector);
4+
browser.addCommand('touch', async function() {
5+
const {x, y} = await elementUtils.getElemCenterLocation(browser, this.selector);
66

77
await browser.touchAction([
88
{action: 'tap', x, y}
99
]);
1010
}, true);
11+
12+
// TODO
13+
if (browser.touch) {
14+
browser.overwriteCommand('touch', async function(baseTouchFn, selector) {
15+
const elem = await browser.$(selector);
16+
await elem.touch();
17+
});
18+
}
1119
};

lib/commands/url.js

+8-6
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,24 @@
33
const {PAGE_LOAD_TIMEOUT} = require('../constants');
44

55
module.exports = (browser, {config}) => {
6-
const baseUrlFn = browser.url;
76
const pageLoadTimeout = config.pageLoadTimeout || PAGE_LOAD_TIMEOUT;
87

9-
browser.addCommand('url', async (uri) => {
8+
browser.overwriteCommand('url', async (baseUrlFn, uri) => {
109
if (!uri) {
11-
return baseUrlFn.call(browser, uri);
10+
return baseUrlFn(uri);
1211
}
1312

1413
// in order to clear the page from previous search result
1514
await browser.execute(() => document.body && document.body.remove());
16-
await baseUrlFn.call(browser, uri);
15+
await baseUrlFn(uri);
1716

1817
await browser.waitUntil(
19-
() => browser.isVisible('body'),
18+
async () => {
19+
const elem = await browser.$('body');
20+
return elem.isDisplayed();
21+
},
2022
pageLoadTimeout,
2123
`The page did not load in ${pageLoadTimeout} ms`
2224
);
23-
}, true);
25+
});
2426
};

lib/index.js

+2-10
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ const _ = require('lodash');
44
const parseConfig = require('./config');
55
const browserCommands = require('./commands');
66
const {WEB_VIEW_CTX} = require('./command-helpers/test-context');
7-
const utils = require('./utils');
87
const {getNativeLocators} = require('./native-locators');
98
const {getElementUtils} = require('./command-helpers/element-utils');
109

@@ -30,14 +29,7 @@ module.exports = (hermione, opts) => {
3029
input.focus();
3130
});
3231

33-
const isWdioLatest = utils.isWdioLatest(browser);
34-
let contexts;
35-
36-
if (isWdioLatest) {
37-
contexts = await browser.getContexts();
38-
} else {
39-
contexts = (await browser.contexts()).value;
40-
}
32+
const contexts = await browser.getContexts();
4133

4234
await browser.extendOptions({[WEB_VIEW_CTX]: contexts[1]});
4335
});
@@ -83,7 +75,7 @@ module.exports = (hermione, opts) => {
8375
}
8476

8577
root.beforeEach(async function() {
86-
await this.browser.context(this.browser.options[WEB_VIEW_CTX]);
78+
await this.browser.switchContext(this.browser.options[WEB_VIEW_CTX]);
8779
});
8880
});
8981
});

lib/utils.js

-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
'use strict';
22

3-
exports.isWdioLatest = (browser) => Boolean(browser.overwriteCommand);
4-
53
exports.getSafariVersion = (broConfig) => {
64
const currentVersion = broConfig.desiredCapabilities.version || broConfig.desiredCapabilities.browserVersion;
75

test/lib/command-helpers/context-switcher.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ describe('"context-switcher" helper', () => {
5656
await runInContext_(browser, action);
5757

5858
assert.calledOnceWith(actionFn, ...action.args);
59-
assert.notCalled(browser.context);
60-
assert.notCalled(browser.contexts);
59+
assert.notCalled(browser.getContext);
60+
assert.notCalled(browser.getContexts);
6161
});
6262

6363
it('should return result from calling passed action', async () => {
@@ -74,7 +74,7 @@ describe('"context-switcher" helper', () => {
7474
await runInContext_(browser, {args: 'foo-bar'});
7575

7676
assert.callOrder(
77-
browser.context.withArgs(NATIVE_CONTEXT),
77+
browser.switchContext.withArgs(NATIVE_CONTEXT),
7878
actionFn.withArgs('foo-bar')
7979
);
8080
});
@@ -86,7 +86,7 @@ describe('"context-switcher" helper', () => {
8686

8787
assert.callOrder(
8888
actionFn.withArgs('foo-bar'),
89-
browser.context.withArgs('WEBVIEW_12345')
89+
browser.switchContext.withArgs('WEBVIEW_12345')
9090
);
9191
});
9292

@@ -123,7 +123,7 @@ describe('"context-switcher" helper', () => {
123123
let isNativeCtxBefore;
124124

125125
browser.options[WEB_VIEW_CTX] = 'WEBVIEW_12345';
126-
browser.context.withArgs('WEBVIEW_12345').callsFake(() => {
126+
browser.switchContext.withArgs('WEBVIEW_12345').callsFake(() => {
127127
isNativeCtxBefore = testCtx[IS_NATIVE_CTX];
128128
});
129129

0 commit comments

Comments
 (0)