Skip to content

Commit

Permalink
test: migrate some helpers to fixtures, use testOutputDir (#3926)
Browse files Browse the repository at this point in the history
  • Loading branch information
dgozman committed Sep 18, 2020
1 parent 9de39b1 commit 4e2d75d
Show file tree
Hide file tree
Showing 38 changed files with 297 additions and 520 deletions.
8 changes: 3 additions & 5 deletions test/browsercontext-basic.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@
* limitations under the License.
*/

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

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

it('should create new context', async function({browser}) {
expect(browser.contexts().length).toBe(0);
Expand Down Expand Up @@ -90,7 +88,7 @@ it('should isolate localStorage and cookies', async function({browser, server})
it('should propagate default viewport to the page', async ({ browser }) => {
const context = await browser.newContext({ viewport: { width: 456, height: 789 } });
const page = await context.newPage();
await utils.verifyViewport(page, 456, 789);
await verifyViewport(page, 456, 789);
await context.close();
});

Expand All @@ -99,7 +97,7 @@ it('should make a copy of default viewport', async ({ browser }) => {
const context = await browser.newContext({ viewport });
viewport.width = 567;
const page = await context.newPage();
await utils.verifyViewport(page, 456, 789);
await verifyViewport(page, 456, 789);
await context.close();
});

Expand Down
8 changes: 3 additions & 5 deletions test/browsercontext-csp.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { it, expect } from './playwright.fixtures';

import * as utils from './utils';
import { it, expect, attachFrame } from './playwright.fixtures';

it('should bypass CSP meta tag', async ({browser, server}) => {
// Make sure CSP prohibits addScriptTag.
Expand Down Expand Up @@ -83,7 +81,7 @@ it('should bypass CSP in iframes as well', async ({browser, server}) => {
const context = await browser.newContext();
const page = await context.newPage();
await page.goto(server.EMPTY_PAGE);
const frame = await utils.attachFrame(page, 'frame1', server.PREFIX + '/csp.html');
const frame = await 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);
await context.close();
Expand All @@ -94,7 +92,7 @@ it('should bypass CSP in iframes as well', async ({browser, server}) => {
const context = await browser.newContext({ bypassCSP: true });
const page = await context.newPage();
await page.goto(server.EMPTY_PAGE);
const frame = await utils.attachFrame(page, 'frame1', server.PREFIX + '/csp.html');
const frame = await 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);
await context.close();
Expand Down
6 changes: 2 additions & 4 deletions test/browsercontext-user-agent.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { it, expect } from './playwright.fixtures';

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

it('should work', async ({browser, server}) => {
{
Expand Down Expand Up @@ -49,7 +47,7 @@ it('should work for subframes', async ({browser, server}) => {
const page = await context.newPage();
const [request] = await Promise.all([
server.waitForRequest('/empty.html'),
utils.attachFrame(page, 'frame1', server.EMPTY_PAGE),
attachFrame(page, 'frame1', server.EMPTY_PAGE),
]);
expect(request.headers['user-agent']).toBe('foobar');
await context.close();
Expand Down
10 changes: 4 additions & 6 deletions test/browsercontext-viewport.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,16 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { it, expect } from './playwright.fixtures';

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

it('should get the proper default viewport size', async ({page, server}) => {
await utils.verifyViewport(page, 1280, 720);
await verifyViewport(page, 1280, 720);
});

it('should set the proper viewport size', async ({page, server}) => {
await utils.verifyViewport(page, 1280, 720);
await verifyViewport(page, 1280, 720);
await page.setViewportSize({width: 123, height: 456});
await utils.verifyViewport(page, 123, 456);
await verifyViewport(page, 123, 456);
});

it('should return correct outerWidth and outerHeight', async ({page}) => {
Expand Down
7 changes: 3 additions & 4 deletions test/browsertype-connect.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
*/

import { options } from './playwright.fixtures';
import utils from './utils';
import { serverFixtures } from './remoteServer.fixture';
const { it, expect, describe } = serverFixtures;

Expand Down Expand Up @@ -175,13 +174,13 @@ describe('connect', suite => {
}
});
// Register one engine before connecting.
await utils.registerEngine(playwright, 'mycss1', mycss);
await playwright.selectors.register('mycss1', mycss);

const browser1 = await browserType.connect({ wsEndpoint: remoteServer.wsEndpoint() });
const context1 = await browser1.newContext();

// Register another engine after creating context.
await utils.registerEngine(playwright, 'mycss2', mycss);
await playwright.selectors.register('mycss2', mycss);

const page1 = await context1.newPage();
await page1.setContent(`<div>hello</div>`);
Expand All @@ -192,7 +191,7 @@ describe('connect', suite => {
const browser2 = await browserType.connect({ wsEndpoint: remoteServer.wsEndpoint() });

// Register third engine after second connect.
await utils.registerEngine(playwright, 'mycss3', mycss);
await playwright.selectors.register('mycss3', mycss);

const page2 = await browser2.newPage();
await page2.setContent(`<div>hello</div>`);
Expand Down
7 changes: 2 additions & 5 deletions test/chromium/launcher.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@
import { it, expect, options } from '../playwright.fixtures';

import path from 'path';
import utils from '../utils';
import type { ChromiumBrowser, ChromiumBrowserContext } from '../..';
const { makeUserDataDir, removeUserDataDir } = utils;

it('should throw with remote-debugging-pipe argument', (test, parameters) => {
test.skip(options.WIRE || !options.CHROMIUM(parameters));
Expand Down Expand Up @@ -58,8 +56,8 @@ it('should open devtools when "devtools: true" option is given', (test, paramete

it('should return background pages', (test, parameters) => {
test.skip(!options.CHROMIUM(parameters));
}, async ({browserType, defaultBrowserOptions}) => {
const userDataDir = await makeUserDataDir();
}, async ({browserType, defaultBrowserOptions, createUserDataDir}) => {
const userDataDir = await createUserDataDir();
const extensionPath = path.join(__dirname, '..', 'assets', 'simple-extension');
const extensionOptions = {...defaultBrowserOptions,
headless: false,
Expand All @@ -77,7 +75,6 @@ it('should return background pages', (test, parameters) => {
expect(context.backgroundPages()).toContain(backgroundPage);
expect(context.pages()).not.toContain(backgroundPage);
await context.close();
await removeUserDataDir(userDataDir);
});

it('should not create pages automatically', (test, parameters) => {
Expand Down
11 changes: 4 additions & 7 deletions test/chromium/tracing.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,8 @@ const fixtures = playwrightFixtures.declareTestFixtures<TestState>();
const { it, expect, describe, defineTestFixture } = fixtures;


defineTestFixture('outputTraceFile', async ({tmpDir}, test) => {
const outputTraceFile = path.join(tmpDir, `trace.json`);
await test(outputTraceFile);
if (fs.existsSync(outputTraceFile))
fs.unlinkSync(outputTraceFile);
defineTestFixture('outputTraceFile', async ({testOutputDir}, test) => {
await test(path.join(testOutputDir, `trace.json`));
});

describe('oopif', (suite, parameters) => {
Expand All @@ -43,8 +40,8 @@ describe('oopif', (suite, parameters) => {
expect(fs.existsSync(outputTraceFile)).toBe(true);
});

it('should create directories as needed', async ({browser, page, server, tmpDir}) => {
const filePath = path.join(tmpDir, 'these', 'are', 'directories');
it('should create directories as needed', async ({browser, page, server, testOutputDir}) => {
const filePath = path.join(testOutputDir, 'these', 'are', 'directories', 'trace.json');
await (browser as ChromiumBrowser).startTracing(page, {screenshots: true, path: filePath});
await page.goto(server.PREFIX + '/grid.html');
await (browser as ChromiumBrowser).stopTracing();
Expand Down
9 changes: 4 additions & 5 deletions test/click.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@
* limitations under the License.
*/

import { it, expect, options } from './playwright.fixtures';
import utils from './utils';
import { it, expect, options, attachFrame } from './playwright.fixtures';

async function giveItAChanceToClick(page) {
for (let i = 0; i < 5; i++)
Expand Down Expand Up @@ -315,7 +314,7 @@ it('should click links which cause navigation', async ({page, server}) => {
it('should click the button inside an iframe', async ({page, server}) => {
await page.goto(server.EMPTY_PAGE);
await page.setContent('<div style="width:100px;height:100px">spacer</div>');
await utils.attachFrame(page, 'button-test', server.PREFIX + '/input/button.html');
await attachFrame(page, 'button-test', server.PREFIX + '/input/button.html');
const frame = page.frames()[1];
const button = await frame.$('button');
await button.click();
Expand All @@ -331,7 +330,7 @@ it('should click the button with fixed position inside an iframe', (test, parame
await page.goto(server.EMPTY_PAGE);
await page.setViewportSize({width: 500, height: 500});
await page.setContent('<div style="width:100px;height:2000px">spacer</div>');
await utils.attachFrame(page, 'button-test', server.CROSS_PROCESS_PREFIX + '/input/button.html');
await attachFrame(page, 'button-test', server.CROSS_PROCESS_PREFIX + '/input/button.html');
const frame = page.frames()[1];
await frame.$eval('button', button => button.style.setProperty('position', 'fixed'));
await frame.click('button');
Expand All @@ -343,7 +342,7 @@ it('should click the button with deviceScaleFactor set', async ({browser, server
const page = await context.newPage();
expect(await page.evaluate(() => window.devicePixelRatio)).toBe(5);
await page.setContent('<div style="width:100px;height:100px">spacer</div>');
await utils.attachFrame(page, 'button-test', server.PREFIX + '/input/button.html');
await attachFrame(page, 'button-test', server.PREFIX + '/input/button.html');
const frame = page.frames()[1];
const button = await frame.$('button');
await button.click();
Expand Down
7 changes: 3 additions & 4 deletions test/defaultbrowsercontext-1.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@
* limitations under the License.
*/

import { it, expect } from './playwright.fixtures';
import { it, expect, verifyViewport } from './playwright.fixtures';
import fs from 'fs';
import utils from './utils';

it('context.cookies() should work', async ({server, launchPersistent}) => {
const {page} = await launchPersistent();
Expand Down Expand Up @@ -119,9 +118,9 @@ it('should(not) block third party cookies', async ({server, launchPersistent, is

it('should support viewport option', async ({launchPersistent}) => {
const {page, context} = await launchPersistent({viewport: { width: 456, height: 789 }});
await utils.verifyViewport(page, 456, 789);
await verifyViewport(page, 456, 789);
const page2 = await context.newPage();
await utils.verifyViewport(page2, 456, 789);
await verifyViewport(page2, 456, 789);
});

it('should support deviceScaleFactor option', async ({launchPersistent}) => {
Expand Down
55 changes: 22 additions & 33 deletions test/defaultbrowsercontext-2.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@

import { it, expect, options } from './playwright.fixtures';
import fs from 'fs';
import utils from './utils';
const { removeUserDataDir, makeUserDataDir } = utils;

it('should support hasTouch option', async ({server, launchPersistent}) => {
const {page} = await launchPersistent({hasTouch: true});
Expand Down Expand Up @@ -81,20 +79,18 @@ it('should support extraHTTPHeaders option', (test, parameters) => {

it('should accept userDataDir', (test, parameters) => {
test.flaky(options.CHROMIUM(parameters));
}, async ({launchPersistent, tmpDir}) => {
const {context} = await launchPersistent();
// Note: we need an open page to make sure its functional.
expect(fs.readdirSync(tmpDir).length).toBeGreaterThan(0);
}, async ({createUserDataDir, browserType, defaultBrowserOptions}) => {
const userDataDir = await createUserDataDir();
const context = await browserType.launchPersistentContext(userDataDir, defaultBrowserOptions);
expect(fs.readdirSync(userDataDir).length).toBeGreaterThan(0);
await context.close();
expect(fs.readdirSync(tmpDir).length).toBeGreaterThan(0);
// This might throw. See https://github.com/GoogleChrome/puppeteer/issues/2778
await removeUserDataDir(tmpDir);
expect(fs.readdirSync(userDataDir).length).toBeGreaterThan(0);
});

it('should restore state from userDataDir', (test, parameters) => {
test.slow();
}, async ({browserType, defaultBrowserOptions, server, launchPersistent}) => {
const userDataDir = await makeUserDataDir();
}, async ({browserType, defaultBrowserOptions, server, createUserDataDir}) => {
const userDataDir = await createUserDataDir();
const browserContext = await browserType.launchPersistentContext(userDataDir, defaultBrowserOptions);
const page = await browserContext.newPage();
await page.goto(server.EMPTY_PAGE);
Expand All @@ -107,23 +103,19 @@ it('should restore state from userDataDir', (test, parameters) => {
expect(await page2.evaluate(() => localStorage.hey)).toBe('hello');
await browserContext2.close();

const userDataDir2 = await makeUserDataDir();
const userDataDir2 = await createUserDataDir();
const browserContext3 = await browserType.launchPersistentContext(userDataDir2, defaultBrowserOptions);
const page3 = await browserContext3.newPage();
await page3.goto(server.EMPTY_PAGE);
expect(await page3.evaluate(() => localStorage.hey)).not.toBe('hello');
await browserContext3.close();

// This might throw. See https://github.com/GoogleChrome/puppeteer/issues/2778
await removeUserDataDir(userDataDir);
await removeUserDataDir(userDataDir2);
});

it('should restore cookies from userDataDir', (test, parameters) => {
test.slow();
test.flaky(options.CHROMIUM(parameters));
}, async ({browserType, defaultBrowserOptions, server, launchPersistent}) => {
const userDataDir = await makeUserDataDir();
}, async ({browserType, defaultBrowserOptions, server, createUserDataDir}) => {
const userDataDir = await createUserDataDir();
const browserContext = await browserType.launchPersistentContext(userDataDir, defaultBrowserOptions);
const page = await browserContext.newPage();
await page.goto(server.EMPTY_PAGE);
Expand All @@ -140,16 +132,12 @@ it('should restore cookies from userDataDir', (test, parameters) => {
expect(await page2.evaluate(() => document.cookie)).toBe('doSomethingOnlyOnce=true');
await browserContext2.close();

const userDataDir2 = await makeUserDataDir();
const userDataDir2 = await createUserDataDir();
const browserContext3 = await browserType.launchPersistentContext(userDataDir2, defaultBrowserOptions);
const page3 = await browserContext3.newPage();
await page3.goto(server.EMPTY_PAGE);
expect(await page3.evaluate(() => document.cookie)).not.toBe('doSomethingOnlyOnce=true');
await browserContext3.close();

// This might throw. See https://github.com/GoogleChrome/puppeteer/issues/2778
await removeUserDataDir(userDataDir);
await removeUserDataDir(userDataDir2);
});

it('should have default URL when launching browser', async ({launchPersistent}) => {
Expand All @@ -160,22 +148,23 @@ it('should have default URL when launching browser', async ({launchPersistent})

it('should throw if page argument is passed', (test, parameters) => {
test.skip(options.FIREFOX(parameters));
}, async ({browserType, defaultBrowserOptions, server, tmpDir}) => {
}, async ({browserType, defaultBrowserOptions, server, createUserDataDir}) => {
const options = {...defaultBrowserOptions, args: [server.EMPTY_PAGE] };
const error = await browserType.launchPersistentContext(tmpDir, options).catch(e => e);
const error = await browserType.launchPersistentContext(await createUserDataDir(), options).catch(e => e);
expect(error.message).toContain('can not specify page');
});

it('should have passed URL when launching with ignoreDefaultArgs: true', (test, parameters) => {
test.skip(options.WIRE);
}, async ({browserType, defaultBrowserOptions, server, tmpDir, toImpl}) => {
const args = toImpl(browserType)._defaultArgs(defaultBrowserOptions, 'persistent', tmpDir, 0).filter(a => a !== 'about:blank');
}, async ({browserType, defaultBrowserOptions, server, createUserDataDir, toImpl}) => {
const userDataDir = await createUserDataDir();
const args = toImpl(browserType)._defaultArgs(defaultBrowserOptions, 'persistent', userDataDir, 0).filter(a => a !== 'about:blank');
const options = {
...defaultBrowserOptions,
args: [...args, server.EMPTY_PAGE],
ignoreDefaultArgs: true,
};
const browserContext = await browserType.launchPersistentContext(tmpDir, options);
const browserContext = await browserType.launchPersistentContext(userDataDir, options);
if (!browserContext.pages().length)
await browserContext.waitForEvent('page');
await browserContext.pages()[0].waitForLoadState();
Expand All @@ -186,18 +175,18 @@ it('should have passed URL when launching with ignoreDefaultArgs: true', (test,

it('should handle timeout', (test, parameters) => {
test.skip(options.WIRE);
}, async ({browserType, defaultBrowserOptions, tmpDir}) => {
}, async ({browserType, defaultBrowserOptions, createUserDataDir}) => {
const options = { ...defaultBrowserOptions, timeout: 5000, __testHookBeforeCreateBrowser: () => new Promise(f => setTimeout(f, 6000)) };
const error = await browserType.launchPersistentContext(tmpDir, options).catch(e => e);
const error = await browserType.launchPersistentContext(await createUserDataDir(), options).catch(e => e);
expect(error.message).toContain(`browserType.launchPersistentContext: Timeout 5000ms exceeded.`);
});

it('should handle exception', (test, parameters) => {
test.skip(options.WIRE);
}, async ({browserType, defaultBrowserOptions, tmpDir}) => {
}, async ({browserType, defaultBrowserOptions, createUserDataDir}) => {
const e = new Error('Dummy');
const options = { ...defaultBrowserOptions, __testHookBeforeCreateBrowser: () => { throw e; } };
const error = await browserType.launchPersistentContext(tmpDir, options).catch(e => e);
const error = await browserType.launchPersistentContext(await createUserDataDir(), options).catch(e => e);
expect(error.message).toContain('Dummy');
});

Expand Down Expand Up @@ -240,7 +229,7 @@ it('should respect selectors', async ({playwright, launchPersistent}) => {
return Array.from(root.querySelectorAll(selector));
}
});
await utils.registerEngine(playwright, 'defaultContextCSS', defaultContextCSS);
await playwright.selectors.register('defaultContextCSS', defaultContextCSS);

await page.setContent(`<div>hello</div>`);
expect(await page.innerHTML('css=div')).toBe('hello');
Expand Down
Loading

0 comments on commit 4e2d75d

Please sign in to comment.