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
/**
* Copyright 2018 Google Inc. All rights reserved.
* Modifications 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 { contextTest as it, expect } from '../config/browserTest';
it('EF-022 expose binding should work', async ({ context }) => {
let bindingSource: any;
await context.exposeBinding('add', (source, a, b) => {
bindingSource = source;
return a + b;
});
const page = await context.newPage();
const result = await page.evaluate('add(5, 6)');
expect(bindingSource.context).toBe(context);
expect(bindingSource.page).toBe(page);
expect(bindingSource.frame).toBe(page.mainFrame());
expect(result).toEqual(11);
});
it('EF-023 should work', async ({ context, server }) => {
await context.exposeFunction('add', (a: number, b: number) => a + b);
const page = await context.newPage();
await page.exposeFunction('mul', (a: number, b: number) => a * b);
await context.exposeFunction('sub', (a: number, b: number) => a - b);
await context.exposeBinding('addHandle', async ({ frame }, a, b) => {
const handle = await frame.evaluateHandle(([a, b]) => a + b, [a, b]);
return handle;
});
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 });
});
it('EF-024 should dispose', async ({ context, server }) => {
const binding = await context.exposeFunction('compute', function(a, b) {
return a * b;
});
const page = await context.newPage();
const result = await page.evaluate(async function() {
return await window['compute'](9, 4);
});
expect(result).toBe(36);
await binding.dispose();
const e = await page.evaluate(async function() {
return await window['compute'](9, 4);
}).catch(e => e);
expect(e.message).toContain('is not a function');
});
it('EF-025 should throw for duplicate registrations', async ({ context, server }) => {
await context.exposeFunction('foo', () => {});
await context.exposeFunction('bar', () => {});
let error = await context.exposeFunction('foo', () => {}).catch(e => e);
expect(error.message).toContain('Function "foo" has been already registered');
const page = await context.newPage();
error = await page.exposeFunction('foo', () => {}).catch(e => e);
expect(error.message).toContain('Function "foo" has been already registered in the browser context');
await page.exposeFunction('baz', () => {});
error = await context.exposeFunction('baz', () => {}).catch(e => e);
expect(error.message).toContain('Function "baz" has been already registered in one of the pages');
});
it('EF-026 should be callable from-inside addInitScript', async ({ context, server }) => {
let args: string[] = [];
await context.exposeFunction('woof', function(arg: string) {
args.push(arg);
});
await context.addInitScript('window["woof"]("context")');
const page = await context.newPage();
await page.evaluate('undefined');
await expect.poll(() => args).toEqual(['context']);
args = [];
await page.addInitScript('window["woof"]("page")');
await page.reload();
await expect.poll(() => args).toEqual(['context', 'page']);
});
it('EF-027 should work with CSP', async ({ page, context, server }) => {
server.setCSP('/empty.html', 'default-src "self"');
await page.goto(server.EMPTY_PAGE);
let called = false;
await context.exposeBinding('hi', () => called = true);
await page.evaluate(() => (window as any).hi());
expect(called).toBe(true);
});
it('EF-028 should call binding from pagehide handler when navigating away', {
annotation: { type: 'issue', description: 'https://github.com/microsoft/playwright/issues/43064' },
}, async ({ context, page, server, browserName }) => {
it.fixme(browserName === 'chromium', 'Chromium drops CDP events from the old document after the frame host swap');
const calls: string[] = [];
await context.exposeBinding('reportFromPage', (_source, message: string) => { calls.push(message); });
const html = (body: string) => `<!doctype html>${body}<script>addEventListener('pagehide', () => window.reportFromPage('pagehide of ' + location.pathname));</script>`;
server.setRoute('/a.html', (req, res) => {
res.writeHead(200, { 'content-type': 'text/html' });
res.end(html(`<a id="next" href="/b.html">next</a>`));
});
server.setRoute('/b.html', (req, res) => {
res.writeHead(200, { 'content-type': 'text/html' });
res.end(html(`<p>b</p>`));
});
await page.goto(server.PREFIX + '/a.html');
await page.click('#next');
await page.waitForURL('**/b.html');
await expect.poll(() => calls).toContain('pagehide of /a.html');
});