Skip to content

Commit

Permalink
Merge pull request #18 from gemini-testing/FEI-21949.change_ctx_to_we…
Browse files Browse the repository at this point in the history
…b_on_before_each

fix: change ctx to web before each test execution
  • Loading branch information
DudaGod authored Oct 5, 2021
2 parents e93c782 + 6829051 commit 82aec8f
Show file tree
Hide file tree
Showing 7 changed files with 2,233 additions and 89 deletions.
10 changes: 1 addition & 9 deletions lib/command-helpers/context-switcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

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

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

if (!testCtx[WEB_VIEW_CTX]) {
const result = await browser.contexts();
const contexts = isWdioLatest(browser) ? result : result.value;

testCtx[WEB_VIEW_CTX] = contexts[1];
}

await browser.context(NATIVE_CONTEXT);
testCtx[IS_NATIVE_CTX] = true;

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

await browser.context(testCtx[WEB_VIEW_CTX]);
await browser.context(browser.options[WEB_VIEW_CTX]);
testCtx[IS_NATIVE_CTX] = false;

return result;
Expand Down
63 changes: 53 additions & 10 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
const _ = require('lodash');
const parseConfig = require('./config');
const browserCommands = require('./commands');
const {WEB_VIEW_CTX} = require('./command-helpers/test-context');
const utils = require('./utils');

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

if (!hermione.isWorker()) {
hermione.on(hermione.events.SESSION_START, async (browser, {browserId}) => {
if (_.isEmpty(getBrowserPluginCfg(pluginConfig, browserId))) {
return;
}

// Clicking into the fake input before tests started
// to fix bug with caret-color https://bugs.webkit.org/show_bug.cgi?id=228859
if (Object.keys(pluginConfig.browsers).includes(browserId)) {
await browser.execute(function() {
const input = document.createElement('input');
input.setAttribute('type', 'text');
document.body.append(input);
input.focus();
});
await browser.execute(function() {
const input = document.createElement('input');
input.setAttribute('type', 'text');
document.body.append(input);
input.focus();
});

const isWdioLatest = utils.isWdioLatest(browser);
let contexts;

if (isWdioLatest) {
contexts = await browser.getContexts();
} else {
contexts = (await browser.contexts()).value;
}

await browser.extendOptions({[WEB_VIEW_CTX]: contexts[1]});
});

return;
}

hermione.on(hermione.events.NEW_BROWSER, (browser, {browserId}) => {
const {commands = []} = _.get(pluginConfig, `browsers[${browserId}]`, {});
const config = hermione.config.forBrowser(browserId);
const {commands = []} = getBrowserPluginCfg(pluginConfig, browserId);

if (_.isEmpty(commands)) {
return;
}

const broConfig = hermione.config.forBrowser(browserId);

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

browserCommands[commandName](browser, config);
browserCommands[commandName](browser, broConfig);
});
});

hermione.on(hermione.events.AFTER_TESTS_READ, (collection) => {
collection.eachRootSuite((root, browserId) => {
if (_.isEmpty(getBrowserPluginCfg(pluginConfig, browserId))) {
return;
}

const {testsPerSession} = hermione.config.forBrowser(browserId);
const isTestRunsInOneSession = testsPerSession === 1;

if (isTestRunsInOneSession) {
return;
}

root.beforeEach(async function() {
await this.browser.context(this.browser.options[WEB_VIEW_CTX]);
});
});
});
};

function getBrowserPluginCfg(pluginConfig, browserId) {
return _.get(pluginConfig, `browsers[${browserId}]`, {});
}
Loading

0 comments on commit 82aec8f

Please sign in to comment.