Skip to content

Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.

chore(test): convert some more tests to typescript (#3329)

Joel Einbinder83ac3f43f307
diff --git a/test/__snapshots__/chromium-css-coverage.spec.js.snap b/test/__snapshots__/chromium-css-coverage.spec.ts.snap
similarity index 100%
rename from test/__snapshots__/chromium-css-coverage.spec.js.snap
rename to test/__snapshots__/chromium-css-coverage.spec.ts.snap
diff --git a/test/autowaiting-basic.spec.ts b/test/autowaiting-basic.spec.ts
index 5b5b8a7134..9ba4755b07 100644
--- a/test/autowaiting-basic.spec.ts
+++ b/test/autowaiting-basic.spec.ts
@@ -15,7 +15,7 @@
* limitations under the License.
*/
-export {};
+import utils from './utils';
const {USES_HOOKS} = testOptions;
diff --git a/test/autowaiting-no-hang.spec.ts b/test/autowaiting-no-hang.spec.ts
index 3f9226cad8..db39a32673 100644
--- a/test/autowaiting-no-hang.spec.ts
+++ b/test/autowaiting-no-hang.spec.ts
@@ -15,7 +15,7 @@
* limitations under the License.
*/
-export {};
+import utils from './utils';
it('clicking on links which do not commit navigation', async({page, server, httpsServer}) => {
await page.goto(server.EMPTY_PAGE);
diff --git a/test/browser.spec.ts b/test/browser.spec.ts
index 3fb1294f52..2ebe07eba7 100644
--- a/test/browser.spec.ts
+++ b/test/browser.spec.ts
@@ -14,7 +14,7 @@
* limitations under the License.
*/
-export {};
+import utils from './utils';
const {CHROMIUM} = testOptions;
it('should create new page', async function({browser}) {
diff --git a/test/browsercontext-add-cookies.spec.ts b/test/browsercontext-add-cookies.spec.ts
index 23977cf15c..b8ff9755f1 100644
--- a/test/browsercontext-add-cookies.spec.ts
+++ b/test/browsercontext-add-cookies.spec.ts
@@ -15,7 +15,7 @@
* limitations under the License.
*/
-export {};
+import utils from './utils';
const {FFOX, CHROMIUM} = testOptions;
it('should work', async({context, page, server}) => {
diff --git a/test/browsercontext-clearcookies.spec.ts b/test/browsercontext-clearcookies.spec.ts
index c38fcf5e86..1f82cfbb47 100644
--- a/test/browsercontext-clearcookies.spec.ts
+++ b/test/browsercontext-clearcookies.spec.ts
@@ -15,7 +15,7 @@
* limitations under the License.
*/
-export {};
+import utils from './utils';
it('should clear cookies', async({context, page, server}) => {
await page.goto(server.EMPTY_PAGE);
diff --git a/test/browsercontext-cookies.spec.ts b/test/browsercontext-cookies.spec.ts
index 90bf06f3f0..3f1598c173 100644
--- a/test/browsercontext-cookies.spec.ts
+++ b/test/browsercontext-cookies.spec.ts
@@ -15,7 +15,7 @@
* limitations under the License.
*/
-export {};
+import utils from './utils';
const {WEBKIT, WIN} = testOptions;
it('should return no cookies in pristine browser context', async({context, page, server}) => {
diff --git a/test/browsercontext-credentials.spec.ts b/test/browsercontext-credentials.spec.ts
index a6daab9e68..ae157fbfd8 100644
--- a/test/browsercontext-credentials.spec.ts
+++ b/test/browsercontext-credentials.spec.ts
@@ -15,7 +15,7 @@
* limitations under the License.
*/
-export {};
+import utils from './utils';
const {CHROMIUM, HEADLESS} = testOptions;
it.fail(CHROMIUM && !HEADLESS)('should fail without credentials', async({browser, server}) => {
diff --git a/test/browsercontext-device.spec.ts b/test/browsercontext-device.spec.ts
index 48ed4acaea..d4b7f9456c 100644
--- a/test/browsercontext-device.spec.ts
+++ b/test/browsercontext-device.spec.ts
@@ -15,7 +15,7 @@
* limitations under the License.
*/
-export {};
+import utils from './utils';
const {FFOX} = testOptions;
it.skip(FFOX)('should work', async({playwright, browser, server}) => {
diff --git a/test/browsercontext-expose-function.spec.ts b/test/browsercontext-expose-function.spec.ts
index f16a68742a..d509bb0c9b 100644
--- a/test/browsercontext-expose-function.spec.ts
+++ b/test/browsercontext-expose-function.spec.ts
@@ -15,7 +15,7 @@
* limitations under the License.
*/
-export {};
+import utils from './utils';
const {FFOX, CHROMIUM, WEBKIT, MAC, CHANNEL, HEADLESS} = testOptions;
const {devices} = require('..');
diff --git a/test/browsercontext-locale.spec.js b/test/browsercontext-locale.spec.ts
similarity index 92%
rename from test/browsercontext-locale.spec.js
rename to test/browsercontext-locale.spec.ts
index abfbce0ca8..4efedcfbe3 100644
--- a/test/browsercontext-locale.spec.js
+++ b/test/browsercontext-locale.spec.ts
@@ -15,7 +15,7 @@
* limitations under the License.
*/
-const utils = require('./utils');
+import utils from './utils';
const {CHROMIUM, FFOX, MAC, HEADLESS} = testOptions;
it('should affect accept-language header', async({browser, server}) => {
@@ -25,7 +25,7 @@ it('should affect accept-language header', async({browser, server}) => {
server.waitForRequest('/empty.html'),
page.goto(server.EMPTY_PAGE),
]);
- expect(request.headers['accept-language'].substr(0, 5)).toBe('fr-CH');
+ expect((request.headers['accept-language'] as string).substr(0, 5)).toBe('fr-CH');
await context.close();
});
@@ -79,10 +79,10 @@ it('should format number in popups', async({browser, server}) => {
const [popup] = await Promise.all([
page.waitForEvent('popup'),
- page.evaluate(url => window._popup = window.open(url), server.PREFIX + '/formatted-number.html'),
+ page.evaluate(url => window.open(url), server.PREFIX + '/formatted-number.html'),
]);
await popup.waitForLoadState('domcontentloaded');
- const result = await popup.evaluate(() => window.result);
+ const result = await popup.evaluate('window.result');
expect(result).toBe('1 000 000,5');
await context.close();
});
@@ -93,10 +93,10 @@ it('should affect navigator.language in popups', async({browser, server}) => {
await page.goto(server.EMPTY_PAGE);
const [popup] = await Promise.all([
page.waitForEvent('popup'),
- page.evaluate(url => window._popup = window.open(url), server.PREFIX + '/formatted-number.html'),
+ page.evaluate(url => window.open(url), server.PREFIX + '/formatted-number.html'),
]);
await popup.waitForLoadState('domcontentloaded');
- const result = await popup.evaluate(() => window.initialNavigatorLanguage);
+ const result = await popup.evaluate('window.initialNavigatorLanguage');
expect(result).toBe('fr-CH');
await context.close();
});
diff --git a/test/browsercontext-page-event.spec.js b/test/browsercontext-page-event.spec.ts
similarity index 99%
rename from test/browsercontext-page-event.spec.js
rename to test/browsercontext-page-event.spec.ts
index 4188f93041..012d1474c2 100644
--- a/test/browsercontext-page-event.spec.js
+++ b/test/browsercontext-page-event.spec.ts
@@ -14,8 +14,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-
-const utils = require('./utils');
+import utils from './utils';
const {FFOX, CHROMIUM, WEBKIT, MAC, CHANNEL, HEADLESS} = testOptions;
const {devices} = require('..');
diff --git a/test/browsercontext-route.spec.js b/test/browsercontext-route.spec.ts
similarity index 99%
rename from test/browsercontext-route.spec.js
rename to test/browsercontext-route.spec.ts
index d81f30d943..fa6451382e 100644
--- a/test/browsercontext-route.spec.js
+++ b/test/browsercontext-route.spec.ts
@@ -15,7 +15,7 @@
* limitations under the License.
*/
-const utils = require('./utils');
+import utils from './utils';
const {FFOX, CHROMIUM, WEBKIT, MAC, CHANNEL, HEADLESS} = testOptions;
const {devices} = require('..');
diff --git a/test/browsercontext-timezone-id.spec.js b/test/browsercontext-timezone-id.spec.ts
similarity index 99%
rename from test/browsercontext-timezone-id.spec.js
rename to test/browsercontext-timezone-id.spec.ts
index 5e8b36445c..3a37fffed1 100644
--- a/test/browsercontext-timezone-id.spec.js
+++ b/test/browsercontext-timezone-id.spec.ts
@@ -15,7 +15,7 @@
* limitations under the License.
*/
-const utils = require('./utils');
+import utils from './utils';
const {CHROMIUM, FFOX, MAC, HEADLESS} = testOptions;
it('should work', async ({ browser }) => {
diff --git a/test/browsercontext-user-agent.spec.js b/test/browsercontext-user-agent.spec.ts
similarity index 98%
rename from test/browsercontext-user-agent.spec.js
rename to test/browsercontext-user-agent.spec.ts
index 291b794dc9..6891e6a662 100644
--- a/test/browsercontext-user-agent.spec.js
+++ b/test/browsercontext-user-agent.spec.ts
@@ -15,7 +15,7 @@
* limitations under the License.
*/
-const utils = require('./utils');
+import utils from './utils';
const {FFOX, CHROMIUM, WEBKIT, MAC, CHANNEL, HEADLESS} = testOptions;
const {devices} = require('..');
diff --git a/test/browsercontext-viewport-mobile.spec.js b/test/browsercontext-viewport-mobile.spec.ts
similarity index 97%
rename from test/browsercontext-viewport-mobile.spec.js
rename to test/browsercontext-viewport-mobile.spec.ts
index 504808c520..328a8b2177 100644
--- a/test/browsercontext-viewport-mobile.spec.js
+++ b/test/browsercontext-viewport-mobile.spec.ts
@@ -15,7 +15,7 @@
* limitations under the License.
*/
-const utils = require('./utils');
+import utils from './utils';
const {CHROMIUM, FFOX, MAC, HEADLESS} = testOptions;
it.skip(FFOX)('should support mobile emulation', async({playwright, browser, server}) => {
@@ -66,7 +66,7 @@ it.skip(FFOX)('should detect touch when applying viewport with touches', async({
const page = await context.newPage();
await page.goto(server.EMPTY_PAGE);
await page.addScriptTag({url: server.PREFIX + '/modernizr.js'});
- expect(await page.evaluate(() => Modernizr.touchevents)).toBe(true);
+ expect(await page.evaluate(() => window['Modernizr'].touchevents)).toBe(true);
await context.close();
});
@@ -99,8 +99,8 @@ it.skip(FFOX)('should fire orientationchange event', async({browser, server}) =>
const page = await context.newPage();
await page.goto(server.PREFIX + '/mobile.html');
await page.evaluate(() => {
- window.counter = 0;
- window.addEventListener('orientationchange', () => console.log(++window.counter));
+ let counter = 0;
+ window.addEventListener('orientationchange', () => console.log(++counter));
});
const event1 = page.waitForEvent('console');
diff --git a/test/browsercontext-viewport.spec.js b/test/browsercontext-viewport.spec.ts
similarity index 99%
rename from test/browsercontext-viewport.spec.js
rename to test/browsercontext-viewport.spec.ts
index f499a31bf0..3dc0ca6836 100644
--- a/test/browsercontext-viewport.spec.js
+++ b/test/browsercontext-viewport.spec.ts
@@ -15,7 +15,7 @@
* limitations under the License.
*/
-const utils = require('./utils');
+import utils from './utils';
const {CHROMIUM, FFOX, MAC, HEADLESS} = testOptions;
it('should get the proper default viewport size', async({page, server}) => {
diff --git a/test/browsertype-basic.spec.js b/test/browsertype-basic.spec.ts
similarity index 93%
rename from test/browsertype-basic.spec.js
rename to test/browsertype-basic.spec.ts
index 1207710f47..d05bc87de3 100644
--- a/test/browsertype-basic.spec.js
+++ b/test/browsertype-basic.spec.ts
@@ -15,9 +15,9 @@
* limitations under the License.
*/
-const path = require('path');
-const fs = require('fs');
-const utils = require('./utils');
+import path from 'path';
+import fs from 'fs';
+import utils from './utils';
const {FFOX, CHROMIUM, WEBKIT, WIN, USES_HOOKS, CHANNEL} = testOptions;
it('browserType.executablePath should work', async({browserType}) => {
diff --git a/test/browsertype-connect.spec.js b/test/browsertype-connect.spec.ts
similarity index 89%
rename from test/browsertype-connect.spec.js
rename to test/browsertype-connect.spec.ts
index 28f3968f64..cb378d7e6a 100644
--- a/test/browsertype-connect.spec.js
+++ b/test/browsertype-connect.spec.ts
@@ -15,9 +15,7 @@
* limitations under the License.
*/
-const path = require('path');
-const fs = require('fs');
-const utils = require('./utils');
+import utils from './utils';
const {FFOX, CHROMIUM, WEBKIT, WIN, USES_HOOKS, CHANNEL} = testOptions;
it.slow()('should be able to reconnect to a browser', async({browserType, defaultBrowserOptions, server}) => {
@@ -36,15 +34,15 @@ it.slow()('should be able to reconnect to a browser', async({browserType, defaul
await page.goto(server.EMPTY_PAGE);
await browser.close();
}
- await browserServer._checkLeaks();
+ await (browserServer as any)._checkLeaks();
await browserServer.close();
});
it.fail(USES_HOOKS || (CHROMIUM && WIN)).slow()('should handle exceptions during connect', async({browserType, defaultBrowserOptions, server}) => {
const browserServer = await browserType.launchServer(defaultBrowserOptions);
const __testHookBeforeCreateBrowser = () => { throw new Error('Dummy') };
- const error = await browserType.connect({ wsEndpoint: browserServer.wsEndpoint(), __testHookBeforeCreateBrowser }).catch(e => e);
- await browserServer._checkLeaks();
+ const error = await browserType.connect({ wsEndpoint: browserServer.wsEndpoint(), __testHookBeforeCreateBrowser } as any).catch(e => e);
+ await (browserServer as any)._checkLeaks();
await browserServer.close();
expect(error.message).toContain('Dummy');
});
@@ -55,7 +53,7 @@ it('should set the browser connected state', async ({browserType, defaultBrowser
expect(remote.isConnected()).toBe(true);
await remote.close();
expect(remote.isConnected()).toBe(false);
- await browserServer._checkLeaks();
+ await (browserServer as any)._checkLeaks();
await browserServer.close();
});
@@ -68,6 +66,6 @@ it('should throw when used after isConnected returns false', async({browserType,
new Promise(f => remote.once('disconnected', f)),
]);
expect(remote.isConnected()).toBe(false);
- const error = await page.evaluate('1 + 1').catch(e => e);
+ const error = await page.evaluate('1 + 1').catch(e => e) as Error;
expect(error.message).toContain('has been closed');
});
diff --git a/test/browsertype-launch.spec.js b/test/browsertype-launch.spec.ts
similarity index 97%
rename from test/browsertype-launch.spec.js
rename to test/browsertype-launch.spec.ts
index 98f93bec8e..3da77183e3 100644
--- a/test/browsertype-launch.spec.js
+++ b/test/browsertype-launch.spec.ts
@@ -15,9 +15,9 @@
* limitations under the License.
*/
-const path = require('path');
-const fs = require('fs');
-const utils = require('./utils');
+import path from 'path';
+import fs from 'fs';
+import utils from './utils';
const {FFOX, CHROMIUM, WEBKIT, WIN, USES_HOOKS, CHANNEL} = testOptions;
it('should reject all promises when browser is closed', async({browserType, defaultBrowserOptions}) => {
@@ -83,7 +83,7 @@ it.skip(USES_HOOKS)('should report launch log', async({browserType, defaultBrows
});
it.slow()('should accept objects as options', async({browserType, defaultBrowserOptions}) => {
- const browser = await browserType.launch({ ...defaultBrowserOptions, process });
+ const browser = await browserType.launch({ ...defaultBrowserOptions, process } as any);
await browser.close();
});
diff --git a/test/capabilities.spec.js b/test/capabilities.spec.ts
similarity index 92%
rename from test/capabilities.spec.js
rename to test/capabilities.spec.ts
index 605cfdbb04..72052c04b1 100644
--- a/test/capabilities.spec.js
+++ b/test/capabilities.spec.ts
@@ -14,14 +14,14 @@
* limitations under the License.
*/
-const path = require('path');
-const url = require('url');
+import path from 'path';
+import url from 'url';
const {FFOX, CHROMIUM, WEBKIT, WIN, LINUX, ASSETS_DIR} = testOptions;
it.fail(WEBKIT && WIN)('Web Assembly should work', async function({page, server}) {
await page.goto(server.PREFIX + '/wasm/table2.html');
- expect(await page.evaluate(() => loadTable())).toBe('42, 83');
+ expect(await page.evaluate('loadTable()')).toBe('42, 83');
});
it('WebSocket should work', async({page, server}) => {
@@ -47,7 +47,7 @@ it('should respect CSP', async({page, server}) => {
});
await page.goto(server.EMPTY_PAGE);
- expect(await page.evaluate(() => window.testStatus)).toBe('SUCCESS');
+ expect(await page.evaluate(() => window['testStatus'])).toBe('SUCCESS');
});
it.fail(WEBKIT && WIN)('should play video', async({page}) => {
diff --git a/test/channels.spec.js b/test/channels.spec.ts
similarity index 96%
rename from test/channels.spec.js
rename to test/channels.spec.ts
index 9da7afd6d4..a92d00c0f7 100644
--- a/test/channels.spec.js
+++ b/test/channels.spec.ts
@@ -15,10 +15,12 @@
* limitations under the License.
*/
+import utils from './utils';
+import { ChromiumBrowser } from '../types/types';
const { FFOX, CHROMIUM, WEBKIT, WIN, CHANNEL } = testOptions;
it.skip(!CHANNEL)('should work', async({browser}) => {
- expect(!!browser._connection).toBeTruthy();
+ expect(!!browser['_connection']).toBeTruthy();
});
it.skip(!CHANNEL)('should scope context handles', async({browserType, browser, server}) => {
@@ -81,7 +83,7 @@ it.skip(!CHANNEL || !CHROMIUM)('should scope CDPSession handles', async({browser
};
await expectScopeState(browserType, GOLDEN_PRECONDITION);
- const session = await browser.newBrowserCDPSession();
+ const session = await (browser as ChromiumBrowser).newBrowserCDPSession();
await expectScopeState(browserType, {
_guid: '',
objects: [
diff --git a/test/check.spec.js b/test/check.spec.ts
similarity index 77%
rename from test/check.spec.js
rename to test/check.spec.ts
index 65cab858f7..e3059ae651 100644
--- a/test/check.spec.js
+++ b/test/check.spec.ts
@@ -18,43 +18,43 @@
it('should check the box', async({page}) => {
await page.setContent(`<input id='checkbox' type='checkbox'></input>`);
await page.check('input');
- expect(await page.evaluate(() => checkbox.checked)).toBe(true);
+ expect(await page.evaluate(() => window['checkbox'].checked)).toBe(true);
});
it('should not check the checked box', async({page}) => {
await page.setContent(`<input id='checkbox' type='checkbox' checked></input>`);
await page.check('input');
- expect(await page.evaluate(() => checkbox.checked)).toBe(true);
+ expect(await page.evaluate(() => window['checkbox'].checked)).toBe(true);
});
it('should uncheck the box', async({page}) => {
await page.setContent(`<input id='checkbox' type='checkbox' checked></input>`);
await page.uncheck('input');
- expect(await page.evaluate(() => checkbox.checked)).toBe(false);
+ expect(await page.evaluate(() => window['checkbox'].checked)).toBe(false);
});
it('should not uncheck the unchecked box', async({page}) => {
await page.setContent(`<input id='checkbox' type='checkbox'></input>`);
await page.uncheck('input');
- expect(await page.evaluate(() => checkbox.checked)).toBe(false);
+ expect(await page.evaluate(() => window['checkbox'].checked)).toBe(false);
});
it('should check the box by label', async({page}) => {
await page.setContent(`<label for='checkbox'><input id='checkbox' type='checkbox'></input></label>`);
await page.check('label');
- expect(await page.evaluate(() => checkbox.checked)).toBe(true);
+ expect(await page.evaluate(() => window['checkbox'].checked)).toBe(true);
});
it('should check the box outside label', async({page}) => {
await page.setContent(`<label for='checkbox'>Text</label><div><input id='checkbox' type='checkbox'></input></div>`);
await page.check('label');
- expect(await page.evaluate(() => checkbox.checked)).toBe(true);
+ expect(await page.evaluate(() => window['checkbox'].checked)).toBe(true);
});
it('should check the box inside label w/o id', async({page}) => {
await page.setContent(`<label>Text<span><input id='checkbox' type='checkbox'></input></span></label>`);
await page.check('label');
- expect(await page.evaluate(() => checkbox.checked)).toBe(true);
+ expect(await page.evaluate(() => window['checkbox'].checked)).toBe(true);
});
it('should check radio', async({page}) => {
@@ -63,7 +63,7 @@ it('should check radio', async({page}) => {
<input id='two' type='radio'>two</input>
<input type='radio'>three</input>`);
await page.check('#two');
- expect(await page.evaluate(() => two.checked)).toBe(true);
+ expect(await page.evaluate(() => window['two'].checked)).toBe(true);
});
it('should check the box by aria role', async({page}) => {
@@ -72,5 +72,5 @@ it('should check the box by aria role', async({page}) => {
checkbox.addEventListener('click', () => checkbox.setAttribute('aria-checked', 'true'));
</script>`);
await page.check('div');
- expect(await page.evaluate(() => checkbox.getAttribute('aria-checked'))).toBe('true');
+ expect(await page.evaluate(() => window['checkbox'].getAttribute('aria-checked'))).toBe('true');
});
diff --git a/test/chromium-css-coverage.spec.js b/test/chromium-css-coverage.spec.ts
similarity index 99%
rename from test/chromium-css-coverage.spec.js
rename to test/chromium-css-coverage.spec.ts
index 42677d0649..fac6c3ce42 100644
--- a/test/chromium-css-coverage.spec.js
+++ b/test/chromium-css-coverage.spec.ts
@@ -14,6 +14,7 @@
* limitations under the License.
*/
+import utils from './utils';
const {FFOX, CHROMIUM, WEBKIT} = testOptions;
it.skip(!CHROMIUM)('should work', async function({browserType, page, server}) {
diff --git a/test/types.d.ts b/test/types.d.ts
index c8f1a9ec57..9c43704f74 100644
--- a/test/types.d.ts
+++ b/test/types.d.ts
@@ -2,25 +2,6 @@ type ServerResponse = import('http').ServerResponse;
type IncomingMessage = import('http').IncomingMessage;
type Falsy = false|''|0|null|undefined;
-interface Expect<T> {
- toBe(other: T, message?: string): void;
- toBeFalsy(message?: string): void;
- toBeTruthy(message?: string): void;
- toContain(other: any, message?: string): void;
- toEqual(other: T, message?: string): void;
- toBeNull(message?: string): void;
- toBeInstanceOf(other: Function, message?: string): void;
-
- toBeGreaterThan(other: number, message?: string): void;
- toBeGreaterThanOrEqual(other: number, message?: string): void;
- toBeLessThan(other: number, message?: string): void;
- toBeLessThanOrEqual(other: number, message?: string): void;
- toBeCloseTo(other: number, precision: number, message?: string): void;
-
- toBeGolden(golden: {goldenPath: string, outputPath: string, goldenName: string}): void;
-
- not: Expect<T>;
-}
type DescribeFunction = ((name: string, inner: () => void) => void) & {fail(condition: boolean): DescribeFunction};
@@ -48,7 +29,6 @@ interface TestSetup<STATE> {
testRunner: TestRunner<STATE>;
product: 'Chromium'|'Firefox'|'WebKit';
selectors: import('../index').Selectors;
- expect<T>(value: T): Expect<T>;
playwrightPath;
}
@@ -96,7 +76,7 @@ interface TestServer {
declare const describe: DescribeFunction;
declare const fdescribe: DescribeFunction;
declare const xdescribe: DescribeFunction;
-declare function expect<T>(value: T): Expect<T>;
+declare const expect: typeof import('expect');
declare const it: ItFunction<PageState>;
declare const fit: ItFunction<PageState>;
declare const dit: ItFunction<PageState>;
@@ -116,6 +96,7 @@ declare const testOptions: {
OUTPUT_DIR: string;
USES_HOOKS: boolean;
CHANNEL: boolean;
+ ASSETS_DIR: string;
};
// keyboard.html