Skip to content

Commit

Permalink
test: organize tests to not depend on context (#5878)
Browse files Browse the repository at this point in the history
  • Loading branch information
pavelfeldman authored Mar 18, 2021
1 parent 5c753b7 commit 16bf462
Show file tree
Hide file tree
Showing 11 changed files with 214 additions and 204 deletions.
87 changes: 30 additions & 57 deletions test/page-basic.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,45 +17,34 @@

import { it, expect } from './fixtures';

it('should reject all promises when page is closed', async ({context}) => {
const newPage = await context.newPage();
it('should reject all promises when page is closed', async ({page}) => {
let error = null;
await Promise.all([
newPage.evaluate(() => new Promise(r => {})).catch(e => error = e),
newPage.close(),
page.evaluate(() => new Promise(r => {})).catch(e => error = e),
page.close(),
]);
expect(error.message).toContain('Protocol error');
});

it('should not be visible in context.pages', async ({context}) => {
const newPage = await context.newPage();
expect(context.pages()).toContain(newPage);
await newPage.close();
expect(context.pages()).not.toContain(newPage);
});

it('should set the page close state', async ({context}) => {
const newPage = await context.newPage();
expect(newPage.isClosed()).toBe(false);
await newPage.close();
expect(newPage.isClosed()).toBe(true);
it('should set the page close state', async ({page}) => {
expect(page.isClosed()).toBe(false);
await page.close();
expect(page.isClosed()).toBe(true);
});

it('should pass page to close event', async ({context}) => {
const newPage = await context.newPage();
it('should pass page to close event', async ({page}) => {
const [closedPage] = await Promise.all([
newPage.waitForEvent('close'),
newPage.close()
page.waitForEvent('close'),
page.close()
]);
expect(closedPage).toBe(newPage);
expect(closedPage).toBe(page);
});

it('should terminate network waiters', async ({context, server}) => {
const newPage = await context.newPage();
it('should terminate network waiters', async ({page, server}) => {
const results = await Promise.all([
newPage.waitForRequest(server.EMPTY_PAGE).catch(e => e),
newPage.waitForResponse(server.EMPTY_PAGE).catch(e => e),
newPage.close()
page.waitForRequest(server.EMPTY_PAGE).catch(e => e),
page.waitForResponse(server.EMPTY_PAGE).catch(e => e),
page.close()
]);
for (let i = 0; i < 2; i++) {
const message = results[i].message;
Expand All @@ -64,16 +53,15 @@ it('should terminate network waiters', async ({context, server}) => {
}
});

it('should be callable twice', async ({context}) => {
const newPage = await context.newPage();
it('should be callable twice', async ({page}) => {
await Promise.all([
newPage.close(),
newPage.close(),
page.close(),
page.close(),
]);
await newPage.close();
await page.close();
});

it('should fire load when expected', async ({page, server}) => {
it('should fire load when expected', async ({page}) => {
await Promise.all([
page.goto('about:blank'),
page.waitForEvent('load'),
Expand Down Expand Up @@ -109,29 +97,29 @@ it('should return null if parent page has been closed', async ({page}) => {
expect(opener).toBe(null);
});

it('should fire domcontentloaded when expected', async ({page, server}) => {
it('should fire domcontentloaded when expected', async ({page}) => {
const navigatedPromise = page.goto('about:blank');
await page.waitForEvent('domcontentloaded');
await navigatedPromise;
});

it('should pass self as argument to domcontentloaded event', async ({page, server}) => {
it('should pass self as argument to domcontentloaded event', async ({page}) => {
const [eventArg] = await Promise.all([
new Promise(f => page.on('domcontentloaded', f)),
page.goto('about:blank')
]);
expect(eventArg).toBe(page);
});

it('should pass self as argument to load event', async ({page, server}) => {
it('should pass self as argument to load event', async ({page}) => {
const [eventArg] = await Promise.all([
new Promise(f => page.on('load', f)),
page.goto('about:blank')
]);
expect(eventArg).toBe(page);
});

it('should fail with error upon disconnect', async ({page, server}) => {
it('should fail with error upon disconnect', async ({page}) => {
let error;
const waitForPromise = page.waitForEvent('download').catch(e => error = e);
await page.close();
Expand Down Expand Up @@ -159,7 +147,7 @@ it('page.title should return the page title', async ({page, server}) => {
expect(await page.title()).toBe('Woof-Woof');
});

it('page.close should work with window.close', async function({ page, context, server }) {
it('page.close should work with window.close', async function({ page }) {
const newPagePromise = page.waitForEvent('popup');
await page.evaluate(() => window['newPage'] = window.open('about:blank'));
const newPage = await newPagePromise;
Expand All @@ -168,18 +156,13 @@ it('page.close should work with window.close', async function({ page, context, s
await closedPromise;
});

it('page.close should work with page.close', async function({ page, context, server }) {
const newPage = await context.newPage();
const closedPromise = new Promise(x => newPage.on('close', x));
await newPage.close();
it('page.close should work with page.close', async function({ page }) {
const closedPromise = new Promise(x => page.on('close', x));
await page.close();
await closedPromise;
});

it('page.context should return the correct instance', async function({page, context}) {
expect(page.context()).toBe(context);
});

it('page.frame should respect name', async function({page, server}) {
it('page.frame should respect name', async function({page}) {
await page.setContent(`<iframe name=target></iframe>`);
expect(page.frame({ name: 'bogus' })).toBe(null);
const frame = page.frame({ name: 'target' });
Expand Down Expand Up @@ -233,7 +216,7 @@ it('page.press should work', async ({page, server}) => {
expect(await page.evaluate(() => document.querySelector('textarea').value)).toBe('a');
});

it('page.press should work for Enter', async ({page, server}) => {
it('page.press should work for Enter', async ({page}) => {
await page.setContent(`<input onkeypress="console.log('press')"></input>`);
const messages = [];
page.on('console', message => messages.push(message));
Expand All @@ -247,13 +230,3 @@ it('frame.press should work', async ({page, server}) => {
await frame.press('textarea', 'a');
expect(await frame.evaluate(() => document.querySelector('textarea').value)).toBe('a');
});

it('frame.focus should work multiple times', async ({ context, server }) => {
const page1 = await context.newPage();
const page2 = await context.newPage();
for (const page of [page1, page2]) {
await page.setContent(`<button id="foo" onfocus="window.gotFocus=true"></button>`);
await page.focus('#foo');
expect(await page.evaluate(() => !!window['gotFocus'])).toBe(true);
}
});
8 changes: 4 additions & 4 deletions test/page-click-react.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ declare const renderComponent;
declare const e;
declare const MyButton;

it('should report that selector does not match anymore', (test, parameters) => {
it('should report that selector does not match anymore', test => {
test.fail(true);
}, async ({page, server}) => {
await page.goto(server.PREFIX + '/react.html');
Expand All @@ -41,7 +41,7 @@ it('should report that selector does not match anymore', (test, parameters) => {
expect(error.message).toContain('element does not match the selector anymore');
});

it('should not retarget the handle when element is recycled', (test, parameters) => {
it('should not retarget the handle when element is recycled', test => {
test.fixme(true);
}, async ({page, server}) => {
await page.goto(server.PREFIX + '/react.html');
Expand Down Expand Up @@ -70,7 +70,7 @@ it('should timeout when click opens alert', async ({page, server}) => {
await dialog.dismiss();
});

it('should retarget when element is recycled during hit testing', (test, parameters) => {
it('should retarget when element is recycled during hit testing', test => {
test.fixme(true);
}, async ({page, server}) => {
await page.goto(server.PREFIX + '/react.html');
Expand All @@ -87,7 +87,7 @@ it('should retarget when element is recycled during hit testing', (test, paramet
expect(await page.evaluate('window.button2')).toBe(undefined);
});

it('should retarget when element is recycled before enabled check', (test, parameters) => {
it('should retarget when element is recycled before enabled check', test => {
test.fixme(true);
}, async ({page, server}) => {
await page.goto(server.PREFIX + '/react.html');
Expand Down
2 changes: 1 addition & 1 deletion test/page-click-timeout-1.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ it('should avoid side effects after timeout', (test, { mode }) => {
expect(error.message).toContain('page.click: Timeout 2000ms exceeded.');
});

it('should timeout waiting for button to be enabled', async ({page, server}) => {
it('should timeout waiting for button to be enabled', async ({page}) => {
await page.setContent('<button onclick="javascript:window.__CLICKED=true;" disabled><span>Click target</span></button>');
const error = await page.click('text=Click target', { timeout: 3000 }).catch(e => e);
expect(await page.evaluate('window.__CLICKED')).toBe(undefined);
Expand Down
Loading

0 comments on commit 16bf462

Please sign in to comment.