Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.
TypeScript automationchromechromiume2e-testingelectronend-to-end-testingfirefoxjavascriptplaywrighttesttest-automationtestingtesting-toolswebwebkit
chore(test): convert tests to typescript (1) (#3307)
Pavel Feldman4b3fb6dcca9e
diff --git a/test/autowaiting-basic.spec.js b/test/autowaiting-basic.spec.tssimilarity index 98%rename from test/autowaiting-basic.spec.jsrename to test/autowaiting-basic.spec.tsindex 9e38cf1d8f..5b5b8a7134 100644--- a/test/autowaiting-basic.spec.js+++ b/test/autowaiting-basic.spec.ts@@ -15,7 +15,9 @@* 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}) => {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}) => {await page.setContent(`<a href="${server.PREFIX + '/frames/one-frame.html'}">click me</a>`);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('waiting for scheduled navigations to finish');expect(error.message).toContain(`navigated to "${server.PREFIX + '/frames/one-frame.html'}"`);diff --git a/test/autowaiting-no-hang.spec.js b/test/autowaiting-no-hang.spec.tssimilarity index 88%rename from test/autowaiting-no-hang.spec.jsrename to test/autowaiting-no-hang.spec.tsindex d604ee8ee9..3f9226cad8 100644--- a/test/autowaiting-no-hang.spec.js+++ b/test/autowaiting-no-hang.spec.ts@@ -15,7 +15,7 @@* 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}) => {await page.goto(server.EMPTY_PAGE);@@ -23,7 +23,7 @@ it('clicking on links which do not commit navigation', async({page, server, httpawait 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) => {});await page.evaluate((url) => {window.location.href = url;@@ -31,7 +31,7 @@ it('calling window.stop async', async({page, server, httpsServer}) => {}, server.EMPTY_PAGE);});-it('calling window.stop sync', async({page, server, httpsServer}) => {+it('calling window.stop sync', async({page, server}) => {await page.evaluate((url) => {window.location.href = url;window.stop();@@ -62,6 +62,6 @@ it('opening a popup', async function({page, server}) {await page.goto(server.EMPTY_PAGE);await Promise.all([page.waitForEvent('popup'),- page.evaluate(() => window._popup = window.open(window.location.href)),+ page.evaluate(() => window.open(window.location.href) && 1),]);});diff --git a/test/browser.spec.js b/test/browser.spec.tssimilarity index 97%rename from test/browser.spec.jsrename to test/browser.spec.tsindex 4f2406caa9..3fb1294f52 100644--- a/test/browser.spec.js+++ b/test/browser.spec.ts@@ -14,7 +14,8 @@* limitations under the License.*/-const {FFOX, CHROMIUM, WEBKIT} = testOptions;+export {};+const {CHROMIUM} = testOptions;it('should create new page', async function({browser}) {const page1 = await browser.newPage();diff --git a/test/browsercontext-add-cookies.spec.js b/test/browsercontext-add-cookies.spec.tssimilarity index 99%rename from test/browsercontext-add-cookies.spec.jsrename to test/browsercontext-add-cookies.spec.tsindex b2b0c028c6..23977cf15c 100644--- a/test/browsercontext-add-cookies.spec.js+++ b/test/browsercontext-add-cookies.spec.ts@@ -15,7 +15,8 @@* limitations under the License.*/-const {FFOX, CHROMIUM, WEBKIT, WIN} = testOptions;+export {};+const {FFOX, CHROMIUM} = testOptions;it('should work', async({context, page, server}) => {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}) => {- let cookie = [];+ let cookie = '';server.setRoute('/empty.html', (req, res) => {cookie = req.headers.cookie || '';res.end();diff --git a/test/browsercontext-basic.spec.js b/test/browsercontext-basic.spec.tssimilarity index 98%rename from test/browsercontext-basic.spec.jsrename to test/browsercontext-basic.spec.tsindex 9592db628b..78b45f793b 100644--- a/test/browsercontext-basic.spec.js+++ b/test/browsercontext-basic.spec.ts@@ -15,9 +15,9 @@* limitations under the License.*/-const utils = require('./utils');-const {FFOX, CHROMIUM, WEBKIT, MAC, CHANNEL, HEADLESS} = testOptions;-const {devices} = require('..');+import utils from './utils';++const {WEBKIT} = testOptions;it('should create new context', async function({browser}) {expect(browser.contexts().length).toBe(0);diff --git a/test/browsercontext-clearcookies.spec.js b/test/browsercontext-clearcookies.spec.tssimilarity index 97%rename from test/browsercontext-clearcookies.spec.jsrename to test/browsercontext-clearcookies.spec.tsindex 1476542481..c38fcf5e86 100644--- a/test/browsercontext-clearcookies.spec.js+++ b/test/browsercontext-clearcookies.spec.ts@@ -15,7 +15,7 @@* limitations under the License.*/-const {FFOX, CHROMIUM, WEBKIT, WIN} = testOptions;+export {};it('should clear cookies', async({context, page, server}) => {await page.goto(server.EMPTY_PAGE);diff --git a/test/browsercontext-cookies.spec.js b/test/browsercontext-cookies.spec.tssimilarity index 98%rename from test/browsercontext-cookies.spec.jsrename to test/browsercontext-cookies.spec.tsindex 4a57510ba0..90bf06f3f0 100644--- a/test/browsercontext-cookies.spec.js+++ b/test/browsercontext-cookies.spec.ts@@ -15,7 +15,8 @@* 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}) => {expect(await context.cookies()).toEqual([]);diff --git a/test/browsercontext-credentials.spec.js b/test/browsercontext-credentials.spec.tssimilarity index 95%rename from test/browsercontext-credentials.spec.jsrename to test/browsercontext-credentials.spec.tsindex 90f11bee19..a6daab9e68 100644--- a/test/browsercontext-credentials.spec.js+++ b/test/browsercontext-credentials.spec.ts@@ -15,9 +15,8 @@* limitations under the License.*/-const utils = require('./utils');-const {FFOX, CHROMIUM, WEBKIT, MAC, CHANNEL, HEADLESS} = testOptions;-const {devices} = require('..');+export {};+const {CHROMIUM, HEADLESS} = testOptions;it.fail(CHROMIUM && !HEADLESS)('should fail without credentials', async({browser, server}) => {server.setAuth('/empty.html', 'user', 'pass');diff --git a/test/browsercontext-csp.spec.js b/test/browsercontext-csp.spec.tssimilarity index 83%rename from test/browsercontext-csp.spec.jsrename to test/browsercontext-csp.spec.tsindex 86351244c2..b25b848ab6 100644--- a/test/browsercontext-csp.spec.js+++ b/test/browsercontext-csp.spec.ts@@ -15,9 +15,7 @@* limitations under the License.*/-const utils = require('./utils');-const {FFOX, CHROMIUM, WEBKIT, MAC, CHANNEL, HEADLESS} = testOptions;-const {devices} = require('..');+import * as utils from './utils';it('should bypass CSP meta tag', async({browser, server}) => {// Make sure CSP prohibits addScriptTag.@@ -26,7 +24,7 @@ it('should bypass CSP meta tag', async({browser, server}) => {const page = await context.newPage();await page.goto(server.PREFIX + '/csp.html');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();}@@ -36,7 +34,7 @@ it('should bypass CSP meta tag', async({browser, server}) => {const page = await context.newPage();await page.goto(server.PREFIX + '/csp.html');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();}});@@ -50,7 +48,7 @@ it('should bypass CSP header', async({browser, server}) => {const page = await context.newPage();await page.goto(server.EMPTY_PAGE);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();}@@ -60,7 +58,7 @@ it('should bypass CSP header', async({browser, server}) => {const page = await context.newPage();await page.goto(server.EMPTY_PAGE);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();}});@@ -70,11 +68,11 @@ it('should bypass after cross-process navigation', async({browser, server}) => {const page = await context.newPage();await page.goto(server.PREFIX + '/csp.html');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.addScriptTag({content: 'window.__injected = 42;'});- expect(await page.evaluate(() => window.__injected)).toBe(42);+ expect(await page.evaluate('window.__injected')).toBe(42);await context.close();});@@ -86,7 +84,7 @@ it('should bypass CSP in iframes as well', async({browser, server}) => {await page.goto(server.EMPTY_PAGE);const frame = await utils.attachFrame(page, 'frame1', server.PREFIX + '/csp.html');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();}@@ -97,7 +95,7 @@ it('should bypass CSP in iframes as well', async({browser, server}) => {await page.goto(server.EMPTY_PAGE);const frame = await utils.attachFrame(page, 'frame1', server.PREFIX + '/csp.html');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();}});diff --git a/test/browsercontext-device.spec.js b/test/browsercontext-device.spec.tssimilarity index 93%rename from test/browsercontext-device.spec.jsrename to test/browsercontext-device.spec.tsindex 7eadc0dd93..48ed4acaea 100644--- a/test/browsercontext-device.spec.js+++ b/test/browsercontext-device.spec.ts@@ -15,8 +15,8 @@* limitations under the License.*/-const utils = require('./utils');-const {CHROMIUM, FFOX, MAC, HEADLESS} = testOptions;+export {};+const {FFOX} = testOptions;it.skip(FFOX)('should work', async({playwright, browser, server}) => {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');await page.evaluate(button => button.style.marginTop = '200px', button);await button.click();- expect(await page.evaluate(() => result)).toBe('Clicked');+ expect(await page.evaluate('result')).toBe('Clicked');await context.close();});diff --git a/test/browsercontext-expose-function.spec.js b/test/browsercontext-expose-function.spec.tssimilarity index 88%rename from test/browsercontext-expose-function.spec.jsrename to test/browsercontext-expose-function.spec.tsindex 6812efed66..f16a68742a 100644--- a/test/browsercontext-expose-function.spec.js+++ b/test/browsercontext-expose-function.spec.ts@@ -15,7 +15,7 @@* limitations under the License.*/-const utils = require('./utils');+export {};const {FFOX, CHROMIUM, WEBKIT, MAC, CHANNEL, HEADLESS} = testOptions;const {devices} = require('..');@@ -27,9 +27,7 @@ it('expose binding should work', async({browser}) => {return a + b;});const page = await context.newPage();- const result = await page.evaluate(async function() {- return add(5, 6);- });+ const result = await page.evaluate('add(5, 6)');expect(bindingSource.context).toBe(context);expect(bindingSource.page).toBe(page);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]);return handle;});- const result = await page.evaluate(async function() {- return { mul: await mul(9, 4), add: await add(9, 4), sub: await sub(9, 4), addHandle: await addHandle(5, 6) };- });+ 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) }))()');expect(result).toEqual({ mul: 36, add: 13, sub: 5, addHandle: 11 });await context.close();});@@ -75,9 +71,9 @@ it('should be callable from-inside addInitScript', async({browser, server}) => {await context.exposeFunction('woof', function(arg) {args.push(arg);});- await context.addInitScript(() => woof('context'));+ await context.addInitScript('woof("context")');const page = await context.newPage();- await page.addInitScript(() => woof('page'));+ await page.addInitScript('woof("page")');args = [];await page.reload();expect(args).toEqual(['context', 'page']);diff --git a/test/pdf.spec.js b/test/pdf.spec.tssimilarity index 86%rename from test/pdf.spec.jsrename to test/pdf.spec.tsindex 5258f4cbcc..27fe54c3ff 100644--- a/test/pdf.spec.js+++ b/test/pdf.spec.ts@@ -14,18 +14,19 @@* limitations under the License.*/-const fs = require('fs');-const path = require('path');+import fs from 'fs'+import path from 'path'+const {FFOX, CHROMIUM, WEBKIT, OUTPUT_DIR, HEADLESS} = testOptions;// 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');await page.pdf({path: outputFile});expect(fs.readFileSync(outputFile).byteLength).toBeGreaterThan(0);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);});diff --git a/test/types.d.ts b/test/types.d.tsindex 9331bf9f40..c8f1a9ec57 100644--- a/test/types.d.ts+++ b/test/types.d.ts@@ -47,20 +47,9 @@ type TestRunner<STATE> = {interface TestSetup<STATE> {testRunner: TestRunner<STATE>;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;expect<T>(value: T): Expect<T>;- defaultBrowserOptions: import('../index').LaunchOptions;playwrightPath;- headless: boolean;- ASSETS_DIR: string;}type TestState = {@@ -70,8 +59,11 @@ type TestState = {};type BrowserState = TestState & {+ playwright: typeof import('../index');+ browserType: import('../index').BrowserType<import('../index').Browser>;browser: import('../index').Browser;browserServer: import('../index').BrowserServer;+ defaultBrowserOptions: import('../index').LaunchOptions;};type PageState = BrowserState & {@@ -81,10 +73,6 @@ type PageState = BrowserState & {type ChromiumPageState = PageState & {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 {@@ -103,7 +91,6 @@ interface TestServer {PREFIX: string;CROSS_PROCESS_PREFIX: string;EMPTY_PAGE: string;-}declare const describe: DescribeFunction;@@ -118,6 +105,18 @@ declare const xit: ItFunction<PageState>;declare const browserType: import('../index').BrowserType<import('../index').Browser>;// 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.htmldeclare function getResult(): string;