Skip to content

Commit 9430344

Browse files
changed behavior of _I switch to {string} window_ step (now it is wait for window existence) (#91)
changed behavior of _I switch to {string} window_ step (now it is wait for window existence) (#91) fixed getting of electron context
1 parent 9f08de9 commit 9430344

File tree

3 files changed

+37
-19
lines changed

3 files changed

+37
-19
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@ Check [Keep a Changelog](http://keepachangelog.com/) for recommendations on how
1010

1111
## [0.40.0]
1212
- :rocket: changed simple expects to poll expects
13-
- :rocket: replaced _wdioValueWait_ type with more generic _wdioValidation_ allowing more wait types
13+
- :rocket: changed behavior of _I switch to {string} window_ step (now it is wait for window existence)
14+
- :rocket: replaced _playwrightValueWait_ type with more generic _playwrightValueWait_ allowing more wait types
1415
Breaking change: value waits now depends on _value_ timeout
16+
- :beetle: fixed getting of electron context
1517

1618
## [0.39.0]
1719
- :rocket: enabled logger for page objects

src/actions.ts

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { getValue, getElement } from './transformers';
33
import { po } from '@qavajs/po-playwright';
44
import { expect } from '@playwright/test';
55
import { parseCoords, parseCoordsAsObject } from './utils/utils';
6-
import {Browser, BrowserContext, Page} from 'playwright';
6+
import { Browser, BrowserContext, Page } from 'playwright';
77

88
declare global {
99
var browser: Browser;
@@ -144,17 +144,25 @@ When('I switch to {int} window', async function (index: number) {
144144
*/
145145
When('I switch to {string} window', async function (matcher: string) {
146146
const urlOrTitle = await getValue(matcher);
147-
const pages = context.pages();
148-
for (const p of pages) {
149-
if (p.url().includes(urlOrTitle) || (await p.title()).includes(urlOrTitle)) {
150-
global.page = p;
151-
//@ts-ignore
152-
po.driver = p;
153-
await page.bringToFront();
154-
return;
147+
const poll = async () => {
148+
const pages = context.pages();
149+
for (const currentPage of pages) {
150+
if (currentPage.url().includes(urlOrTitle) || (await currentPage.title()).includes(urlOrTitle)) {
151+
return currentPage
152+
}
155153
}
156154
}
157-
throw new Error(`Page matching '${matcher}' is not found`);
155+
await expect.poll(
156+
poll,
157+
{
158+
timeout: config.browser.timeout.page,
159+
message: `Page matching ${urlOrTitle} was not found`
160+
}
161+
).toBeDefined();
162+
const targetPage = await poll() as Page;
163+
global.page = targetPage;
164+
(po as any).driver = targetPage;
165+
await targetPage.bringToFront();
158166
});
159167

160168
/**

src/hooks.ts

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
ITestStepHookParameter
1010
} from '@cucumber/cucumber';
1111
import defaultTimeouts from './defaultTimeouts';
12-
import { Browser, BrowserContext, Page } from 'playwright';
12+
import { Browser, BrowserContext, Page, ElectronApplication } from 'playwright';
1313
import { po } from '@qavajs/po-playwright';
1414
import { driverProvider } from './driverProvider';
1515
import {
@@ -50,8 +50,7 @@ Before({name: 'context init'}, async function () {
5050
config.driverConfig.capabilities.recordVideo = config.driverConfig.video;
5151
}
5252
global.context = driverConfig.isElectron
53-
//@ts-ignore
54-
? browser.context()
53+
? (browser as any as ElectronApplication).context()
5554
: await browser.newContext(config?.driverConfig?.capabilities);
5655
if (config.driverConfig.trace) {
5756
await context.tracing.start({
@@ -60,8 +59,7 @@ Before({name: 'context init'}, async function () {
6059
});
6160
}
6261
global.page = driverConfig.isElectron
63-
//@ts-ignore
64-
? browser.firstWindow()
62+
? await (browser as any as ElectronApplication).firstWindow()
6563
: await context.newPage();
6664
global.driver = global.browser;
6765
po.init(page, { timeout: config.driverConfig.timeout.present, logger: this });
@@ -106,8 +104,16 @@ After({name: 'context teardown'}, async function (scenario: ITestCaseHookParamet
106104
}
107105
global.contexts = null;
108106
} else {
109-
await context.close();
110-
this.log('context closed');
107+
if (!config.driverConfig.isElectron) {
108+
await context.close();
109+
this.log('context closed');
110+
} else {
111+
await (browser as any as ElectronApplication)
112+
.evaluate((main: any) => { main.app.exit(0) });
113+
// @ts-ignore
114+
global.browser = null;
115+
this.log('electron app closed');
116+
}
111117
}
112118
}
113119
if (saveVideo(config.driverConfig, scenario)) {
@@ -121,6 +127,8 @@ After({name: 'context teardown'}, async function (scenario: ITestCaseHookParamet
121127

122128
AfterAll(async function () {
123129
if (global.browser) {
124-
await browser.close();
130+
if (!config.driverConfig.isElectron) {
131+
await browser.close();
132+
}
125133
}
126134
});

0 commit comments

Comments
 (0)