Skip to content

Commit 82aec8f

Browse files
authored
Merge pull request #18 from gemini-testing/FEI-21949.change_ctx_to_web_on_before_each
fix: change ctx to web before each test execution
2 parents e93c782 + 6829051 commit 82aec8f

File tree

7 files changed

+2233
-89
lines changed

7 files changed

+2233
-89
lines changed

lib/command-helpers/context-switcher.js

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
const {getTestContext, IS_NATIVE_CTX, WEB_VIEW_CTX} = require('./test-context');
44
const {NATIVE_CONTEXT} = require('../constants');
5-
const {isWdioLatest} = require('../utils');
65

76
exports.runInNativeContext = async function(browser, action, testCtx) {
87
if (!testCtx) {
@@ -13,19 +12,12 @@ exports.runInNativeContext = async function(browser, action, testCtx) {
1312
return action.fn.call(browser, ...[].concat(action.args));
1413
}
1514

16-
if (!testCtx[WEB_VIEW_CTX]) {
17-
const result = await browser.contexts();
18-
const contexts = isWdioLatest(browser) ? result : result.value;
19-
20-
testCtx[WEB_VIEW_CTX] = contexts[1];
21-
}
22-
2315
await browser.context(NATIVE_CONTEXT);
2416
testCtx[IS_NATIVE_CTX] = true;
2517

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

28-
await browser.context(testCtx[WEB_VIEW_CTX]);
20+
await browser.context(browser.options[WEB_VIEW_CTX]);
2921
testCtx[IS_NATIVE_CTX] = false;
3022

3123
return result;

lib/index.js

Lines changed: 53 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
const _ = require('lodash');
44
const parseConfig = require('./config');
55
const browserCommands = require('./commands');
6+
const {WEB_VIEW_CTX} = require('./command-helpers/test-context');
7+
const utils = require('./utils');
68

79
module.exports = (hermione, opts) => {
810
const pluginConfig = parseConfig(opts);
@@ -13,24 +15,42 @@ module.exports = (hermione, opts) => {
1315

1416
if (!hermione.isWorker()) {
1517
hermione.on(hermione.events.SESSION_START, async (browser, {browserId}) => {
18+
if (_.isEmpty(getBrowserPluginCfg(pluginConfig, browserId))) {
19+
return;
20+
}
21+
1622
// Clicking into the fake input before tests started
1723
// to fix bug with caret-color https://bugs.webkit.org/show_bug.cgi?id=228859
18-
if (Object.keys(pluginConfig.browsers).includes(browserId)) {
19-
await browser.execute(function() {
20-
const input = document.createElement('input');
21-
input.setAttribute('type', 'text');
22-
document.body.append(input);
23-
input.focus();
24-
});
24+
await browser.execute(function() {
25+
const input = document.createElement('input');
26+
input.setAttribute('type', 'text');
27+
document.body.append(input);
28+
input.focus();
29+
});
30+
31+
const isWdioLatest = utils.isWdioLatest(browser);
32+
let contexts;
33+
34+
if (isWdioLatest) {
35+
contexts = await browser.getContexts();
36+
} else {
37+
contexts = (await browser.contexts()).value;
2538
}
39+
40+
await browser.extendOptions({[WEB_VIEW_CTX]: contexts[1]});
2641
});
2742

2843
return;
2944
}
3045

3146
hermione.on(hermione.events.NEW_BROWSER, (browser, {browserId}) => {
32-
const {commands = []} = _.get(pluginConfig, `browsers[${browserId}]`, {});
33-
const config = hermione.config.forBrowser(browserId);
47+
const {commands = []} = getBrowserPluginCfg(pluginConfig, browserId);
48+
49+
if (_.isEmpty(commands)) {
50+
return;
51+
}
52+
53+
const broConfig = hermione.config.forBrowser(browserId);
3454

3555
if (commands.includes('screenshot') && !commands.includes('orientation')) {
3656
commands.push('orientation');
@@ -41,7 +61,30 @@ module.exports = (hermione, opts) => {
4161
throw new TypeError(`Can not find "${commandName}" command`);
4262
}
4363

44-
browserCommands[commandName](browser, config);
64+
browserCommands[commandName](browser, broConfig);
65+
});
66+
});
67+
68+
hermione.on(hermione.events.AFTER_TESTS_READ, (collection) => {
69+
collection.eachRootSuite((root, browserId) => {
70+
if (_.isEmpty(getBrowserPluginCfg(pluginConfig, browserId))) {
71+
return;
72+
}
73+
74+
const {testsPerSession} = hermione.config.forBrowser(browserId);
75+
const isTestRunsInOneSession = testsPerSession === 1;
76+
77+
if (isTestRunsInOneSession) {
78+
return;
79+
}
80+
81+
root.beforeEach(async function() {
82+
await this.browser.context(this.browser.options[WEB_VIEW_CTX]);
83+
});
4584
});
4685
});
4786
};
87+
88+
function getBrowserPluginCfg(pluginConfig, browserId) {
89+
return _.get(pluginConfig, `browsers[${browserId}]`, {});
90+
}

0 commit comments

Comments
 (0)