chore(test): convert tests to typescript (1) (#3307)

This commit is contained in:
Pavel Feldman 2020-08-06 08:27:00 -07:00 committed by GitHub
parent 8716a54670
commit 4b3fb6dcca
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 58 additions and 60 deletions

View File

@ -15,7 +15,9 @@
* limitations under the License. * limitations under the License.
*/ */
const {FFOX, CHROMIUM, WEBKIT, USES_HOOKS} = testOptions; export {};
const {USES_HOOKS} = testOptions;
it('should await navigation when clicking anchor', async({page, server}) => { it('should await navigation when clicking anchor', async({page, server}) => {
const messages = []; const messages = [];
@ -204,7 +206,7 @@ it('should work with goto following click', async({page, server}) => {
it.skip(USES_HOOKS)('should report navigation in the log when clicking anchor', async({page, server}) => { it.skip(USES_HOOKS)('should report navigation in the log when clicking anchor', async({page, server}) => {
await page.setContent(`<a href="${server.PREFIX + '/frames/one-frame.html'}">click me</a>`); await page.setContent(`<a href="${server.PREFIX + '/frames/one-frame.html'}">click me</a>`);
const __testHookAfterPointerAction = () => new Promise(f => setTimeout(f, 6000)); const __testHookAfterPointerAction = () => new Promise(f => setTimeout(f, 6000));
const error = await page.click('a', { timeout: 5000, __testHookAfterPointerAction }).catch(e => e); const error = await page.click('a', { timeout: 5000, __testHookAfterPointerAction } as any).catch(e => e);
expect(error.message).toContain('page.click: Timeout 5000ms exceeded.'); expect(error.message).toContain('page.click: Timeout 5000ms exceeded.');
expect(error.message).toContain('waiting for scheduled navigations to finish'); expect(error.message).toContain('waiting for scheduled navigations to finish');
expect(error.message).toContain(`navigated to "${server.PREFIX + '/frames/one-frame.html'}"`); expect(error.message).toContain(`navigated to "${server.PREFIX + '/frames/one-frame.html'}"`);

View File

@ -15,7 +15,7 @@
* limitations under the License. * limitations under the License.
*/ */
const {FFOX, CHROMIUM, WEBKIT, USES_HOOKS} = testOptions; export {};
it('clicking on links which do not commit navigation', async({page, server, httpsServer}) => { it('clicking on links which do not commit navigation', async({page, server, httpsServer}) => {
await page.goto(server.EMPTY_PAGE); await page.goto(server.EMPTY_PAGE);
@ -23,7 +23,7 @@ it('clicking on links which do not commit navigation', async({page, server, http
await page.click('a'); await page.click('a');
}); });
it('calling window.stop async', async({page, server, httpsServer}) => { it('calling window.stop async', async({page, server}) => {
server.setRoute('/empty.html', async (req, res) => {}); server.setRoute('/empty.html', async (req, res) => {});
await page.evaluate((url) => { await page.evaluate((url) => {
window.location.href = url; window.location.href = url;
@ -31,7 +31,7 @@ it('calling window.stop async', async({page, server, httpsServer}) => {
}, server.EMPTY_PAGE); }, server.EMPTY_PAGE);
}); });
it('calling window.stop sync', async({page, server, httpsServer}) => { it('calling window.stop sync', async({page, server}) => {
await page.evaluate((url) => { await page.evaluate((url) => {
window.location.href = url; window.location.href = url;
window.stop(); window.stop();
@ -62,6 +62,6 @@ it('opening a popup', async function({page, server}) {
await page.goto(server.EMPTY_PAGE); await page.goto(server.EMPTY_PAGE);
await Promise.all([ await Promise.all([
page.waitForEvent('popup'), page.waitForEvent('popup'),
page.evaluate(() => window._popup = window.open(window.location.href)), page.evaluate(() => window.open(window.location.href) && 1),
]); ]);
}); });

View File

@ -14,7 +14,8 @@
* limitations under the License. * limitations under the License.
*/ */
const {FFOX, CHROMIUM, WEBKIT} = testOptions; export {};
const {CHROMIUM} = testOptions;
it('should create new page', async function({browser}) { it('should create new page', async function({browser}) {
const page1 = await browser.newPage(); const page1 = await browser.newPage();

View File

@ -15,7 +15,8 @@
* limitations under the License. * limitations under the License.
*/ */
const {FFOX, CHROMIUM, WEBKIT, WIN} = testOptions; export {};
const {FFOX, CHROMIUM} = testOptions;
it('should work', async({context, page, server}) => { it('should work', async({context, page, server}) => {
await page.goto(server.EMPTY_PAGE); await page.goto(server.EMPTY_PAGE);
@ -119,7 +120,7 @@ it('should isolate persistent cookies', async({context, server, browser}) => {
}); });
it('should isolate send cookie header', async({server, context, browser}) => { it('should isolate send cookie header', async({server, context, browser}) => {
let cookie = []; let cookie = '';
server.setRoute('/empty.html', (req, res) => { server.setRoute('/empty.html', (req, res) => {
cookie = req.headers.cookie || ''; cookie = req.headers.cookie || '';
res.end(); res.end();

View File

@ -15,9 +15,9 @@
* limitations under the License. * limitations under the License.
*/ */
const utils = require('./utils'); import utils from './utils';
const {FFOX, CHROMIUM, WEBKIT, MAC, CHANNEL, HEADLESS} = testOptions;
const {devices} = require('..'); const {WEBKIT} = testOptions;
it('should create new context', async function({browser}) { it('should create new context', async function({browser}) {
expect(browser.contexts().length).toBe(0); expect(browser.contexts().length).toBe(0);

View File

@ -15,7 +15,7 @@
* limitations under the License. * limitations under the License.
*/ */
const {FFOX, CHROMIUM, WEBKIT, WIN} = testOptions; export {};
it('should clear cookies', async({context, page, server}) => { it('should clear cookies', async({context, page, server}) => {
await page.goto(server.EMPTY_PAGE); await page.goto(server.EMPTY_PAGE);

View File

@ -15,7 +15,8 @@
* limitations under the License. * limitations under the License.
*/ */
const {FFOX, CHROMIUM, WEBKIT, WIN} = testOptions; export {};
const {WEBKIT, WIN} = testOptions;
it('should return no cookies in pristine browser context', async({context, page, server}) => { it('should return no cookies in pristine browser context', async({context, page, server}) => {
expect(await context.cookies()).toEqual([]); expect(await context.cookies()).toEqual([]);

View File

@ -15,9 +15,8 @@
* limitations under the License. * limitations under the License.
*/ */
const utils = require('./utils'); export {};
const {FFOX, CHROMIUM, WEBKIT, MAC, CHANNEL, HEADLESS} = testOptions; const {CHROMIUM, HEADLESS} = testOptions;
const {devices} = require('..');
it.fail(CHROMIUM && !HEADLESS)('should fail without credentials', async({browser, server}) => { it.fail(CHROMIUM && !HEADLESS)('should fail without credentials', async({browser, server}) => {
server.setAuth('/empty.html', 'user', 'pass'); server.setAuth('/empty.html', 'user', 'pass');

View File

@ -15,9 +15,7 @@
* limitations under the License. * limitations under the License.
*/ */
const utils = require('./utils'); import * as utils from './utils';
const {FFOX, CHROMIUM, WEBKIT, MAC, CHANNEL, HEADLESS} = testOptions;
const {devices} = require('..');
it('should bypass CSP meta tag', async({browser, server}) => { it('should bypass CSP meta tag', async({browser, server}) => {
// Make sure CSP prohibits addScriptTag. // Make sure CSP prohibits addScriptTag.
@ -26,7 +24,7 @@ it('should bypass CSP meta tag', async({browser, server}) => {
const page = await context.newPage(); const page = await context.newPage();
await page.goto(server.PREFIX + '/csp.html'); await page.goto(server.PREFIX + '/csp.html');
await page.addScriptTag({content: 'window.__injected = 42;'}).catch(e => void e); await page.addScriptTag({content: 'window.__injected = 42;'}).catch(e => void e);
expect(await page.evaluate(() => window.__injected)).toBe(undefined); expect(await page.evaluate('window.__injected')).toBe(undefined);
await context.close(); await context.close();
} }
@ -36,7 +34,7 @@ it('should bypass CSP meta tag', async({browser, server}) => {
const page = await context.newPage(); const page = await context.newPage();
await page.goto(server.PREFIX + '/csp.html'); await page.goto(server.PREFIX + '/csp.html');
await page.addScriptTag({content: 'window.__injected = 42;'}); await page.addScriptTag({content: 'window.__injected = 42;'});
expect(await page.evaluate(() => window.__injected)).toBe(42); expect(await page.evaluate('window.__injected')).toBe(42);
await context.close(); await context.close();
} }
}); });
@ -50,7 +48,7 @@ it('should bypass CSP header', async({browser, server}) => {
const page = await context.newPage(); const page = await context.newPage();
await page.goto(server.EMPTY_PAGE); await page.goto(server.EMPTY_PAGE);
await page.addScriptTag({content: 'window.__injected = 42;'}).catch(e => void e); await page.addScriptTag({content: 'window.__injected = 42;'}).catch(e => void e);
expect(await page.evaluate(() => window.__injected)).toBe(undefined); expect(await page.evaluate('window.__injected')).toBe(undefined);
await context.close(); await context.close();
} }
@ -60,7 +58,7 @@ it('should bypass CSP header', async({browser, server}) => {
const page = await context.newPage(); const page = await context.newPage();
await page.goto(server.EMPTY_PAGE); await page.goto(server.EMPTY_PAGE);
await page.addScriptTag({content: 'window.__injected = 42;'}); await page.addScriptTag({content: 'window.__injected = 42;'});
expect(await page.evaluate(() => window.__injected)).toBe(42); expect(await page.evaluate('window.__injected')).toBe(42);
await context.close(); await context.close();
} }
}); });
@ -70,11 +68,11 @@ it('should bypass after cross-process navigation', async({browser, server}) => {
const page = await context.newPage(); const page = await context.newPage();
await page.goto(server.PREFIX + '/csp.html'); await page.goto(server.PREFIX + '/csp.html');
await page.addScriptTag({content: 'window.__injected = 42;'}); await page.addScriptTag({content: 'window.__injected = 42;'});
expect(await page.evaluate(() => window.__injected)).toBe(42); expect(await page.evaluate('window.__injected')).toBe(42);
await page.goto(server.CROSS_PROCESS_PREFIX + '/csp.html'); await page.goto(server.CROSS_PROCESS_PREFIX + '/csp.html');
await page.addScriptTag({content: 'window.__injected = 42;'}); await page.addScriptTag({content: 'window.__injected = 42;'});
expect(await page.evaluate(() => window.__injected)).toBe(42); expect(await page.evaluate('window.__injected')).toBe(42);
await context.close(); await context.close();
}); });
@ -86,7 +84,7 @@ it('should bypass CSP in iframes as well', async({browser, server}) => {
await page.goto(server.EMPTY_PAGE); await page.goto(server.EMPTY_PAGE);
const frame = await utils.attachFrame(page, 'frame1', server.PREFIX + '/csp.html'); const frame = await utils.attachFrame(page, 'frame1', server.PREFIX + '/csp.html');
await frame.addScriptTag({content: 'window.__injected = 42;'}).catch(e => void e); await frame.addScriptTag({content: 'window.__injected = 42;'}).catch(e => void e);
expect(await frame.evaluate(() => window.__injected)).toBe(undefined); expect(await frame.evaluate('window.__injected')).toBe(undefined);
await context.close(); await context.close();
} }
@ -97,7 +95,7 @@ it('should bypass CSP in iframes as well', async({browser, server}) => {
await page.goto(server.EMPTY_PAGE); await page.goto(server.EMPTY_PAGE);
const frame = await utils.attachFrame(page, 'frame1', server.PREFIX + '/csp.html'); const frame = await utils.attachFrame(page, 'frame1', server.PREFIX + '/csp.html');
await frame.addScriptTag({content: 'window.__injected = 42;'}).catch(e => void e); await frame.addScriptTag({content: 'window.__injected = 42;'}).catch(e => void e);
expect(await frame.evaluate(() => window.__injected)).toBe(42); expect(await frame.evaluate('window.__injected')).toBe(42);
await context.close(); await context.close();
} }
}); });

View File

@ -15,8 +15,8 @@
* limitations under the License. * limitations under the License.
*/ */
const utils = require('./utils'); export {};
const {CHROMIUM, FFOX, MAC, HEADLESS} = testOptions; const {FFOX} = testOptions;
it.skip(FFOX)('should work', async({playwright, browser, server}) => { it.skip(FFOX)('should work', async({playwright, browser, server}) => {
const iPhone = playwright.devices['iPhone 6']; const iPhone = playwright.devices['iPhone 6'];
@ -36,7 +36,7 @@ it.skip(FFOX)('should support clicking', async({playwright, browser, server}) =>
const button = await page.$('button'); const button = await page.$('button');
await page.evaluate(button => button.style.marginTop = '200px', button); await page.evaluate(button => button.style.marginTop = '200px', button);
await button.click(); await button.click();
expect(await page.evaluate(() => result)).toBe('Clicked'); expect(await page.evaluate('result')).toBe('Clicked');
await context.close(); await context.close();
}); });

View File

@ -15,7 +15,7 @@
* limitations under the License. * limitations under the License.
*/ */
const utils = require('./utils'); export {};
const {FFOX, CHROMIUM, WEBKIT, MAC, CHANNEL, HEADLESS} = testOptions; const {FFOX, CHROMIUM, WEBKIT, MAC, CHANNEL, HEADLESS} = testOptions;
const {devices} = require('..'); const {devices} = require('..');
@ -27,9 +27,7 @@ it('expose binding should work', async({browser}) => {
return a + b; return a + b;
}); });
const page = await context.newPage(); const page = await context.newPage();
const result = await page.evaluate(async function() { const result = await page.evaluate('add(5, 6)');
return add(5, 6);
});
expect(bindingSource.context).toBe(context); expect(bindingSource.context).toBe(context);
expect(bindingSource.page).toBe(page); expect(bindingSource.page).toBe(page);
expect(bindingSource.frame).toBe(page.mainFrame()); expect(bindingSource.frame).toBe(page.mainFrame());
@ -47,9 +45,7 @@ it('should work', async({browser, server}) => {
const handle = await frame.evaluateHandle(([a, b]) => a + b, [a, b]); const handle = await frame.evaluateHandle(([a, b]) => a + b, [a, b]);
return handle; return handle;
}); });
const result = await page.evaluate(async function() { const result = await page.evaluate('(async () => ({ mul: await mul(9, 4), add: await add(9, 4), sub: await sub(9, 4), addHandle: await addHandle(5, 6) }))()');
return { mul: await mul(9, 4), add: await add(9, 4), sub: await sub(9, 4), addHandle: await addHandle(5, 6) };
});
expect(result).toEqual({ mul: 36, add: 13, sub: 5, addHandle: 11 }); expect(result).toEqual({ mul: 36, add: 13, sub: 5, addHandle: 11 });
await context.close(); await context.close();
}); });
@ -75,9 +71,9 @@ it('should be callable from-inside addInitScript', async({browser, server}) => {
await context.exposeFunction('woof', function(arg) { await context.exposeFunction('woof', function(arg) {
args.push(arg); args.push(arg);
}); });
await context.addInitScript(() => woof('context')); await context.addInitScript('woof("context")');
const page = await context.newPage(); const page = await context.newPage();
await page.addInitScript(() => woof('page')); await page.addInitScript('woof("page")');
args = []; args = [];
await page.reload(); await page.reload();
expect(args).toEqual(['context', 'page']); expect(args).toEqual(['context', 'page']);

View File

@ -14,18 +14,19 @@
* limitations under the License. * limitations under the License.
*/ */
const fs = require('fs'); import fs from 'fs'
const path = require('path'); import path from 'path'
const {FFOX, CHROMIUM, WEBKIT, OUTPUT_DIR, HEADLESS} = testOptions; const {FFOX, CHROMIUM, WEBKIT, OUTPUT_DIR, HEADLESS} = testOptions;
// Printing to pdf is currently only supported in headless chromium. // Printing to pdf is currently only supported in headless chromium.
it.skip(!(HEADLESS && CHROMIUM))('should be able to save file', async({page, server}) => { it.skip(!(HEADLESS && CHROMIUM))('should be able to save file', async({page}) => {
const outputFile = path.join(OUTPUT_DIR, 'output.pdf'); const outputFile = path.join(OUTPUT_DIR, 'output.pdf');
await page.pdf({path: outputFile}); await page.pdf({path: outputFile});
expect(fs.readFileSync(outputFile).byteLength).toBeGreaterThan(0); expect(fs.readFileSync(outputFile).byteLength).toBeGreaterThan(0);
fs.unlinkSync(outputFile); fs.unlinkSync(outputFile);
}); });
it.skip(CHROMIUM)('should be able to save file', async({page, server}) => { it.skip(CHROMIUM)('should be able to save file', async({page}) => {
expect(page.pdf).toBe(undefined); expect(page.pdf).toBe(undefined);
}); });

31
test/types.d.ts vendored
View File

@ -47,20 +47,9 @@ type TestRunner<STATE> = {
interface TestSetup<STATE> { interface TestSetup<STATE> {
testRunner: TestRunner<STATE>; testRunner: TestRunner<STATE>;
product: 'Chromium'|'Firefox'|'WebKit'; product: 'Chromium'|'Firefox'|'WebKit';
FFOX: boolean;
WEBKIT: boolean;
CHROMIUM: boolean;
MAC: boolean;
LINUX: boolean;
WIN: boolean;
playwright: typeof import('../index');
browserType: import('../index').BrowserType<import('../index').Browser>;
selectors: import('../index').Selectors; selectors: import('../index').Selectors;
expect<T>(value: T): Expect<T>; expect<T>(value: T): Expect<T>;
defaultBrowserOptions: import('../index').LaunchOptions;
playwrightPath; playwrightPath;
headless: boolean;
ASSETS_DIR: string;
} }
type TestState = { type TestState = {
@ -70,8 +59,11 @@ type TestState = {
}; };
type BrowserState = TestState & { type BrowserState = TestState & {
playwright: typeof import('../index');
browserType: import('../index').BrowserType<import('../index').Browser>;
browser: import('../index').Browser; browser: import('../index').Browser;
browserServer: import('../index').BrowserServer; browserServer: import('../index').BrowserServer;
defaultBrowserOptions: import('../index').LaunchOptions;
}; };
type PageState = BrowserState & { type PageState = BrowserState & {
@ -81,10 +73,6 @@ type PageState = BrowserState & {
type ChromiumPageState = PageState & { type ChromiumPageState = PageState & {
browser: import('../index').ChromiumBrowser; browser: import('../index').ChromiumBrowser;
}; };
type TestSuite = (setup: TestSetup<TestState>) => void;
type BrowserTestSuite = (setup: TestSetup<BrowserState>) => void;
type PageTestSuite = (setup: TestSetup<PageState>) => void;
type ChromiumTestSuite = (setup: TestSetup<ChromiumPageState>) => void;
interface TestServer { interface TestServer {
@ -103,7 +91,6 @@ interface TestServer {
PREFIX: string; PREFIX: string;
CROSS_PROCESS_PREFIX: string; CROSS_PROCESS_PREFIX: string;
EMPTY_PAGE: string; EMPTY_PAGE: string;
} }
declare const describe: DescribeFunction; declare const describe: DescribeFunction;
@ -118,6 +105,18 @@ declare const xit: ItFunction<PageState>;
declare const browserType: import('../index').BrowserType<import('../index').Browser>; declare const browserType: import('../index').BrowserType<import('../index').Browser>;
// global variables in assets // global variables in assets
declare const testOptions: {
FFOX: boolean;
WEBKIT: boolean;
CHROMIUM: boolean;
MAC: boolean;
LINUX: boolean;
WIN: boolean;
HEADLESS: boolean;
OUTPUT_DIR: string;
USES_HOOKS: boolean;
CHANNEL: boolean;
};
// keyboard.html // keyboard.html
declare function getResult(): string; declare function getResult(): string;