feat(electron): add app.firstWindow convenience method (#2195)

This commit is contained in:
Pavel Feldman 2020-05-12 08:43:41 -07:00 committed by GitHub
parent fdc9ce8e07
commit 3f8dfed67f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 19 additions and 2 deletions

View File

@ -82,7 +82,7 @@ describe('Sanity checks', function () {
it('sanity checks', async () => {
// Wait for the first window to appear.
const window = await this.app.waitForEvent('window');
const window = await this.app.firstWindow();
// Assert window title.
assert.equal(await window.title(), 'Hello World!');

View File

@ -83,6 +83,7 @@ export class ElectronApplication extends ExtendedEventEmitter {
this._windows.delete(page);
});
this._windows.add(page);
await page.waitForLoadState('domcontentloaded');
this.emit(ElectronEvents.ElectronApplication.Window, page);
}
@ -90,6 +91,12 @@ export class ElectronApplication extends ExtendedEventEmitter {
return [...this._windows];
}
async firstWindow(): Promise<Page> {
if (this._windows.size)
return this._windows.values().next().value;
return this.waitForEvent('window');
}
async newBrowserWindow(options: any): Promise<Page> {
const windowId = await this.evaluate(async ({ BrowserWindow }, options) => {
const win = new BrowserWindow(options);

View File

@ -24,6 +24,7 @@ describe('Electron', function() {
const electronPath = path.join(__dirname, '..', '..', 'node_modules', '.bin', electronName);
state.application = await playwright.electron.launch(electronPath, {
args: [path.join(__dirname, 'testApp.js')],
// This is for our own extensive protocol logging, customers don't need it.
logger: {
isEnabled: (name, severity) => {
return name === 'browser' ||
@ -44,6 +45,7 @@ describe('Electron', function() {
});
afterEach(async (state, testRun) => {
await state.application.close();
// This is for our own extensive protocol logging, customers don't need it.
if (config.dumpProtocolOnFailure) {
if (testRun.ok())
testRun.output().splice(0);
@ -115,9 +117,17 @@ describe('Electron', function() {
await page.goto('data:text/html,<script>window.result = add(20, 22);</script>');
expect(await page.evaluate(() => result)).toBe(42);
});
it('should wait for first window', async ({ application }) => {
application.evaluate(({ BrowserWindow }) => {
const window = new BrowserWindow({ width: 800, height: 600 });
window.loadURL('data:text/html,<title>Hello World!</title>');
});
const window = await application.firstWindow();
expect(await window.title()).toBe('Hello World!');
});
});
describe('Electron window', function() {
describe('Electron per window', function() {
beforeAll(async state => {
const electronPath = path.join(__dirname, '..', '..', 'node_modules', '.bin', electronName);
state.application = await playwright.electron.launch(electronPath, {