33const _ = require ( 'lodash' ) ;
44const parseConfig = require ( './config' ) ;
55const browserCommands = require ( './commands' ) ;
6+ const { WEB_VIEW_CTX } = require ( './command-helpers/test-context' ) ;
7+ const utils = require ( './utils' ) ;
68
79module . 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