Skip to content

Commit

Permalink
chore(test): move electron tests to typescript (#3379)
Browse files Browse the repository at this point in the history
  • Loading branch information
JoelEinbinder authored Aug 11, 2020
1 parent d8d845a commit 77e75b4
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 80 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ src/firefox/protocol.ts
src/webkit/protocol.ts
/types/*
/index.d.ts
/electron-types.d.ts
utils/generate_types/overrides.d.ts
utils/generate_types/test/test.ts
test/
1 change: 1 addition & 0 deletions .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ lib/injected/
#types
!types/*
!index.d.ts
!electron-types.d.ts

!index.js
!index.mjs
Expand Down
52 changes: 52 additions & 0 deletions electron-types.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/**
* Copyright (c) Microsoft Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { Logger, Page, JSHandle, ChromiumBrowserContext } from './types/types';
import { BrowserWindow, BrowserWindowConstructorOptions } from 'electron';

export type ElectronLaunchOptions = {
args?: string[],
cwd?: string,
env?: {[key: string]: string|number|boolean},
handleSIGINT?: boolean,
handleSIGTERM?: boolean,
handleSIGHUP?: boolean,
timeout?: number,
logger?: Logger,
};
export interface ElectronLauncher {
launch(executablePath: string, options?: ElectronLaunchOptions): Promise<ElectronApplication>;
}
export interface ElectronApplication {
on(event: 'window', listener: (page : ElectronPage) => void): this;
addListener(event: 'window', listener: (page : ElectronPage) => void): this;
waitForEvent(event: 'window', optionsOrPredicate?: { predicate?: (page : ElectronPage) => boolean, timeout?: number }): Promise<ElectronPage>;

on(event: 'close', listener: (exitCode? : number) => void): this;
addListener(event: 'close', listener: (exitCode? : number) => void): this;
waitForEvent(event: 'close', optionsOrPredicate?: { predicate?: (exitCode? : number) => boolean, timeout?: number }): Promise<number|undefined>;

context(): ChromiumBrowserContext;
windows(): ElectronPage[];
firstWindow(): Promise<ElectronPage>;
newBrowserWindow(options?: BrowserWindowConstructorOptions): Promise<ElectronPage>;
close(): Promise<void>;
evaluate: JSHandle<typeof import('electron')>['evaluate'];
evaluateHandle: JSHandle<typeof import('electron')>['evaluateHandle'];
}
export interface ElectronPage extends Page {
browserWindow: JSHandle<BrowserWindow>;
}
38 changes: 2 additions & 36 deletions packages/playwright-electron/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,41 +14,7 @@
* limitations under the License.
*/

import { Logger, Page, JSHandle, ChromiumBrowserContext } from 'playwright-core/types/types';
import { BrowserWindow, BrowserWindowConstructorOptions } from 'electron';

import { ElectronLauncher } from 'playwright-core/electron-types';
export * from 'playwright-core/types/types';
export type ElectronLaunchOptions = {
args?: string[],
cwd?: string,
env?: {[key: string]: string|number|boolean},
handleSIGINT?: boolean,
handleSIGTERM?: boolean,
handleSIGHUP?: boolean,
timeout?: number,
logger?: Logger,
};
export interface ElectronLauncher {
launch(executablePath: string, options?: ElectronLaunchOptions): Promise<ElectronApplication>;
}
export interface ElectronApplication {
on(event: 'window', listener: (page : ElectronPage) => void): this;
addListener(event: 'window', listener: (page : ElectronPage) => void): this;
waitForEvent(event: 'window', optionsOrPredicate?: { predicate?: (page : ElectronPage) => boolean, timeout?: number }): Promise<ElectronPage>;

on(event: 'close', listener: (exitCode? : number) => void): this;
addListener(event: 'close', listener: (exitCode? : number) => void): this;
waitForEvent(event: 'close', optionsOrPredicate?: { predicate?: (exitCode? : number) => boolean, timeout?: number }): Promise<number|undefined>;

context(): ChromiumBrowserContext;
windows(): ElectronPage[];
firstWindow(): Promise<ElectronPage>;
newBrowserWindow(options?: BrowserWindowConstructorOptions): Promise<ElectronPage>;
close(): Promise<void>;
evaluate: JSHandle<typeof import('electron')>['evaluate'];
evaluateHandle: JSHandle<typeof import('electron')>['evaluateHandle'];
}
export interface ElectronPage extends Page {
browserWindow: JSHandle<BrowserWindow>;
}
export * from 'playwright-core/electron-types';
export const electron: ElectronLauncher;
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,13 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
require('../base.fixture');
import './electron.fixture';

const path = require('path');
import path from 'path';
const electronName = process.platform === 'win32' ? 'electron.cmd' : 'electron';

const { CHROMIUM } = testOptions;

registerFixture('application', async ({playwright}, test) => {
const electronPath = path.join(__dirname, '..', '..', 'node_modules', '.bin', electronName);
const application = await playwright.electron.launch(electronPath, {
args: [path.join(__dirname, 'testApp.js')],
});
try {
await test(application);
} finally {
await application.close();
}
});

it.skip(!CHROMIUM)('should fire close event', async ({ playwright }) => {
const electronPath = path.join(__dirname, '..', '..', 'node_modules', '.bin', electronName);
Expand Down Expand Up @@ -108,16 +97,15 @@ it.skip(!CHROMIUM)('should support init script', async ({ application }) => {
await application.context().addInitScript('window.magic = 42;')
const page = await application.newBrowserWindow({ width: 800, height: 600 });
await page.goto('data:text/html,<script>window.copy = magic</script>');
expect(await page.evaluate(() => copy)).toBe(42);
expect(await page.evaluate(() => window['copy'])).toBe(42);
});

it.skip(!CHROMIUM)('should expose function', async ({ application }) => {
const result = new Promise(f => callback = f);
const t = Date.now();
await application.context().exposeFunction('add', (a, b) => a + b);
const page = await application.newBrowserWindow({ width: 800, height: 600 });
await page.goto('data:text/html,<script>window.result = add(20, 22);</script>');
expect(await page.evaluate(() => result)).toBe(42);
expect(await page.evaluate(() => window['result'])).toBe(42);
});

it.skip(!CHROMIUM)('should wait for first window', async ({ application }) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,50 +13,26 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
require('../base.fixture');

const path = require('path');
const electronName = process.platform === 'win32' ? 'electron.cmd' : 'electron';
import './electron.fixture';

const { CHROMIUM } = testOptions;

registerFixture('application', async ({playwright}, test) => {
const electronPath = path.join(__dirname, '..', '..', 'node_modules', '.bin', electronName);
const application = await playwright.electron.launch(electronPath, {
args: [path.join(__dirname, 'testApp.js')],
});
try {
await test(application);
} finally {
await application.close();
}
});

registerFixture('window', async ({application}, test) => {
const page = await application.newBrowserWindow({ width: 800, height: 600 });
try {
await test(page);
} finally {
await page.close();
}
});

it.skip(!CHROMIUM)('should click the button', async({window, server}) => {
await window.goto(server.PREFIX + '/input/button.html');
await window.click('button');
expect(await window.evaluate(() => result)).toBe('Clicked');
expect(await window.evaluate('result')).toBe('Clicked');
});

it.skip(!CHROMIUM)('should check the box', async({window}) => {
await window.setContent(`<input id='checkbox' type='checkbox'></input>`);
await window.check('input');
expect(await window.evaluate(() => checkbox.checked)).toBe(true);
expect(await window.evaluate('checkbox.checked')).toBe(true);
});

it.skip(!CHROMIUM)('should not check the checked box', async({window}) => {
await window.setContent(`<input id='checkbox' type='checkbox' checked></input>`);
await window.check('input');
expect(await window.evaluate(() => checkbox.checked)).toBe(true);
expect(await window.evaluate('checkbox.checked')).toBe(true);
});

it.skip(!CHROMIUM)('should type into a textarea', async({window, server}) => {
Expand Down
37 changes: 37 additions & 0 deletions test/electron/electron.fixture.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import '../base.fixture';
import {ElectronApplication, ElectronLauncher, ElectronPage} from '../../electron-types';
import path from 'path';

const electronName = process.platform === 'win32' ? 'electron.cmd' : 'electron';

declare global {
interface FixtureState {
application: ElectronApplication;
window: ElectronPage;
}
}

declare module '../../index' {
const electron: ElectronLauncher
}

registerFixture('application', async ({playwright}, test) => {
const electronPath = path.join(__dirname, '..', '..', 'node_modules', '.bin', electronName);
const application = await playwright.electron.launch(electronPath, {
args: [path.join(__dirname, 'testApp.js')],
});
try {
await test(application);
} finally {
await application.close();
}
});

registerFixture('window', async ({application}, test) => {
const page = await application.newBrowserWindow({ width: 800, height: 600 });
try {
await test(page);
} finally {
await page.close();
}
});

0 comments on commit 77e75b4

Please sign in to comment.