Skip to content

Commit b4209e9

Browse files
authored
test: move user-data-dir tests into shared location (#603)
1 parent a5019ea commit b4209e9

File tree

4 files changed

+112
-95
lines changed

4 files changed

+112
-95
lines changed

src/server/wkPlaywright.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -143,10 +143,6 @@ export class WKPlaywright implements Playwright {
143143
throw new Error(missingText);
144144
webkitExecutable = executablePath;
145145
}
146-
webkitArguments.push('--inspector-pipe');
147-
if (options.headless !== false)
148-
webkitArguments.push('--headless');
149-
150146
let transport: PipeTransport | undefined = undefined;
151147

152148
const { launchedProcess, gracefullyClose } = await launchProcess({
@@ -198,12 +194,15 @@ export class WKPlaywright implements Playwright {
198194

199195
defaultArgs(options: WebKitArgOptions = {}): string[] {
200196
const {
197+
headless = true,
201198
args = [],
202199
userDataDir = null
203200
} = options;
204-
const webkitArguments = [...DEFAULT_ARGS];
201+
const webkitArguments = ['--inspector-pipe'];
205202
if (userDataDir)
206203
webkitArguments.push(`--user-data-dir=${userDataDir}`);
204+
if (headless)
205+
webkitArguments.push('--headless');
207206
webkitArguments.push(...args);
208207
return webkitArguments;
209208
}
@@ -256,7 +255,6 @@ export class WKPlaywright implements Playwright {
256255
const mkdtempAsync = platform.promisify(fs.mkdtemp);
257256

258257
const WEBKIT_PROFILE_PATH = path.join(os.tmpdir(), 'playwright_dev_profile-');
259-
const DEFAULT_ARGS: string[] = [];
260258

261259
let cachedMacVersion: string | undefined = undefined;
262260
function getMacVersion() {

test/chromium/launcher.spec.js

Lines changed: 0 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -32,53 +32,6 @@ module.exports.describe = function({testRunner, expect, defaultBrowserOptions, p
3232
const {beforeAll, beforeEach, afterAll, afterEach} = testRunner;
3333

3434
describe('CrPlaywright', function() {
35-
describe('Playwright.launch', function() {
36-
it('userDataDir option', async({server}) => {
37-
const userDataDir = await mkdtempAsync(TMP_FOLDER);
38-
const options = Object.assign({userDataDir}, defaultBrowserOptions);
39-
const browser = await playwright.launch(options);
40-
// Open a page to make sure its functional.
41-
await browser.defaultContext().newPage();
42-
expect(fs.readdirSync(userDataDir).length).toBeGreaterThan(0);
43-
await browser.close();
44-
expect(fs.readdirSync(userDataDir).length).toBeGreaterThan(0);
45-
// This might throw. See https://github.com/GoogleChrome/puppeteer/issues/2778
46-
await rmAsync(userDataDir).catch(e => {});
47-
});
48-
it('userDataDir argument', async({server}) => {
49-
const userDataDir = await mkdtempAsync(TMP_FOLDER);
50-
const options = Object.assign({}, defaultBrowserOptions);
51-
options.args = [
52-
...(defaultBrowserOptions.args || []),
53-
`--user-data-dir=${userDataDir}`
54-
];
55-
const browser = await playwright.launch(options);
56-
expect(fs.readdirSync(userDataDir).length).toBeGreaterThan(0);
57-
await browser.close();
58-
expect(fs.readdirSync(userDataDir).length).toBeGreaterThan(0);
59-
// This might throw. See https://github.com/GoogleChrome/puppeteer/issues/2778
60-
await rmAsync(userDataDir).catch(e => {});
61-
});
62-
it('should return the default arguments', async() => {
63-
expect(playwright.defaultArgs()).toContain('--no-first-run');
64-
expect(playwright.defaultArgs()).toContain('--headless');
65-
expect(playwright.defaultArgs({headless: false})).not.toContain('--headless');
66-
expect(playwright.defaultArgs({userDataDir: 'foo'})).toContain('--user-data-dir=foo');
67-
});
68-
it('should filter out ignored default arguments', async() => {
69-
// Make sure we launch with `--enable-automation` by default.
70-
const defaultArgs = playwright.defaultArgs(defaultBrowserOptions);
71-
const browserServer = await playwright.launchServer(Object.assign({}, defaultBrowserOptions, {
72-
// Ignore first and third default argument.
73-
ignoreDefaultArgs: [ defaultArgs[0], defaultArgs[2] ],
74-
}));
75-
const spawnargs = browserServer.process().spawnargs;
76-
expect(spawnargs.indexOf(defaultArgs[0])).toBe(-1);
77-
expect(spawnargs.indexOf(defaultArgs[1])).not.toBe(-1);
78-
expect(spawnargs.indexOf(defaultArgs[2])).toBe(-1);
79-
await browserServer.close();
80-
});
81-
});
8235
describe('Playwright.launch |browserURL| option', function() {
8336
it('should be able to connect using browserUrl, with and without trailing slash', async({server}) => {
8437
const originalBrowser = await playwright.launch(Object.assign({}, defaultBrowserOptions, {
@@ -120,41 +73,6 @@ module.exports.describe = function({testRunner, expect, defaultBrowserOptions, p
12073
expect(error.message).toContain('Failed to fetch browser webSocket url from');
12174
originalBrowser.close();
12275
});
123-
it('userDataDir option should restore state', async({server}) => {
124-
const userDataDir = await mkdtempAsync(TMP_FOLDER);
125-
const options = Object.assign({userDataDir}, defaultBrowserOptions);
126-
const browser = await playwright.launch(options);
127-
const page = await browser.defaultContext().newPage();
128-
await page.goto(server.EMPTY_PAGE);
129-
await page.evaluate(() => localStorage.hey = 'hello');
130-
await browser.close();
131-
132-
const browser2 = await playwright.launch(options);
133-
const page2 = await browser2.defaultContext().newPage();
134-
await page2.goto(server.EMPTY_PAGE);
135-
expect(await page2.evaluate(() => localStorage.hey)).toBe('hello');
136-
await browser2.close();
137-
// This might throw. See https://github.com/GoogleChrome/puppeteer/issues/2778
138-
await rmAsync(userDataDir).catch(e => {});
139-
});
140-
// This mysteriously fails on Windows on AppVeyor. See https://github.com/GoogleChrome/puppeteer/issues/4111
141-
it('userDataDir option should restore cookies', async({server}) => {
142-
const userDataDir = await mkdtempAsync(TMP_FOLDER);
143-
const options = Object.assign({userDataDir}, defaultBrowserOptions);
144-
const browser = await playwright.launch(options);
145-
const page = await browser.defaultContext().newPage();
146-
await page.goto(server.EMPTY_PAGE);
147-
await page.evaluate(() => document.cookie = 'doSomethingOnlyOnce=true; expires=Fri, 31 Dec 9999 23:59:59 GMT');
148-
await browser.close();
149-
150-
const browser2 = await playwright.launch(options);
151-
const page2 = await browser2.defaultContext().newPage();
152-
await page2.goto(server.EMPTY_PAGE);
153-
expect(await page2.evaluate(() => document.cookie)).toBe('doSomethingOnlyOnce=true');
154-
await browser2.close();
155-
// This might throw. See https://github.com/GoogleChrome/puppeteer/issues/2778
156-
await rmAsync(userDataDir).catch(e => {});
157-
});
15876
});
15977

16078
describe('Playwright.launch |pipe| option', function() {

test/launcher.spec.js

Lines changed: 108 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,17 @@
1515
* limitations under the License.
1616
*/
1717
const fs = require('fs');
18+
const os = require('os');
1819
const path = require('path');
20+
const util = require('util');
21+
1922
const utils = require('./utils');
23+
const rmAsync = util.promisify(require('rimraf'));
24+
const mkdtempAsync = util.promisify(fs.mkdtemp);
25+
26+
const TMP_FOLDER = path.join(os.tmpdir(), 'pptr_tmp_folder-');
2027

21-
module.exports.describe = function({testRunner, expect, defaultBrowserOptions, playwright, WEBKIT}) {
28+
module.exports.describe = function({testRunner, expect, defaultBrowserOptions, playwright, CHROMIUM, FFOX, WEBKIT}) {
2229
const {describe, xdescribe, fdescribe} = testRunner;
2330
const {it, fit, xit, dit} = testRunner;
2431
const {beforeAll, beforeEach, afterAll, afterEach} = testRunner;
@@ -181,4 +188,104 @@ module.exports.describe = function({testRunner, expect, defaultBrowserOptions, p
181188
await browserServer.close();
182189
});
183190
});
191+
192+
describe.skip(FFOX | WEBKIT)('Playwright.launch({userDataDir})', function() {
193+
it('userDataDir option', async({server}) => {
194+
const userDataDir = await mkdtempAsync(TMP_FOLDER);
195+
const options = Object.assign({userDataDir}, defaultBrowserOptions);
196+
const browser = await playwright.launch(options);
197+
// Open a page to make sure its functional.
198+
await browser.defaultContext().newPage();
199+
expect(fs.readdirSync(userDataDir).length).toBeGreaterThan(0);
200+
await browser.close();
201+
expect(fs.readdirSync(userDataDir).length).toBeGreaterThan(0);
202+
// This might throw. See https://github.com/GoogleChrome/puppeteer/issues/2778
203+
await rmAsync(userDataDir).catch(e => {});
204+
});
205+
it('userDataDir argument', async({server}) => {
206+
const userDataDir = await mkdtempAsync(TMP_FOLDER);
207+
const options = Object.assign({}, defaultBrowserOptions);
208+
options.args = [
209+
...(defaultBrowserOptions.args || []),
210+
`--user-data-dir=${userDataDir}`
211+
];
212+
const browser = await playwright.launch(options);
213+
// Open a page to make sure its functional.
214+
await browser.defaultContext().newPage();
215+
expect(fs.readdirSync(userDataDir).length).toBeGreaterThan(0);
216+
await browser.close();
217+
expect(fs.readdirSync(userDataDir).length).toBeGreaterThan(0);
218+
// This might throw. See https://github.com/GoogleChrome/puppeteer/issues/2778
219+
await rmAsync(userDataDir).catch(e => {});
220+
});
221+
it('should return the default arguments', async() => {
222+
if (CHROMIUM)
223+
expect(playwright.defaultArgs()).toContain('--no-first-run');
224+
expect(playwright.defaultArgs()).toContain('--headless');
225+
expect(playwright.defaultArgs({headless: false})).not.toContain('--headless');
226+
expect(playwright.defaultArgs({userDataDir: 'foo'})).toContain('--user-data-dir=foo');
227+
});
228+
it('should filter out ignored default arguments', async() => {
229+
// Make sure we launch with `--enable-automation` by default.
230+
const defaultArgs = playwright.defaultArgs(defaultBrowserOptions);
231+
const browserServer = await playwright.launchServer(Object.assign({}, defaultBrowserOptions, {
232+
// Ignore first and third default argument.
233+
ignoreDefaultArgs: [ defaultArgs[0], defaultArgs[2] ],
234+
}));
235+
const spawnargs = browserServer.process().spawnargs;
236+
expect(spawnargs.indexOf(defaultArgs[0])).toBe(-1);
237+
expect(spawnargs.indexOf(defaultArgs[1])).not.toBe(-1);
238+
expect(spawnargs.indexOf(defaultArgs[2])).toBe(-1);
239+
await browserServer.close();
240+
});
241+
it('userDataDir option should restore state', async({server}) => {
242+
const userDataDir = await mkdtempAsync(TMP_FOLDER);
243+
const options = Object.assign({userDataDir}, defaultBrowserOptions);
244+
const browser = await playwright.launch(options);
245+
const page = await browser.defaultContext().newPage();
246+
await page.goto(server.EMPTY_PAGE);
247+
await page.evaluate(() => localStorage.hey = 'hello');
248+
await browser.close();
249+
250+
const browser2 = await playwright.launch(options);
251+
const page2 = await browser2.defaultContext().newPage();
252+
await page2.goto(server.EMPTY_PAGE);
253+
expect(await page2.evaluate(() => localStorage.hey)).toBe('hello');
254+
await browser2.close();
255+
256+
const browser3 = await playwright.launch(defaultBrowserOptions);
257+
const page3 = await browser3.defaultContext().newPage();
258+
await page3.goto(server.EMPTY_PAGE);
259+
expect(await page3.evaluate(() => localStorage.hey)).not.toBe('hello');
260+
await browser3.close();
261+
262+
// This might throw. See https://github.com/GoogleChrome/puppeteer/issues/2778
263+
await rmAsync(userDataDir).catch(e => {});
264+
});
265+
// This mysteriously fails on Windows on AppVeyor. See https://github.com/GoogleChrome/puppeteer/issues/4111
266+
it('userDataDir option should restore cookies', async({server}) => {
267+
const userDataDir = await mkdtempAsync(TMP_FOLDER);
268+
const options = Object.assign({userDataDir}, defaultBrowserOptions);
269+
const browser = await playwright.launch(options);
270+
const page = await browser.defaultContext().newPage();
271+
await page.goto(server.EMPTY_PAGE);
272+
await page.evaluate(() => document.cookie = 'doSomethingOnlyOnce=true; expires=Fri, 31 Dec 9999 23:59:59 GMT');
273+
await browser.close();
274+
275+
const browser2 = await playwright.launch(options);
276+
const page2 = await browser2.defaultContext().newPage();
277+
await page2.goto(server.EMPTY_PAGE);
278+
expect(await page2.evaluate(() => document.cookie)).toBe('doSomethingOnlyOnce=true');
279+
await browser2.close();
280+
281+
const browser3 = await playwright.launch(defaultBrowserOptions);
282+
const page3 = await browser3.defaultContext().newPage();
283+
await page3.goto(server.EMPTY_PAGE);
284+
expect(await page3.evaluate(() => localStorage.hey)).not.toBe('doSomethingOnlyOnce=true');
285+
await browser3.close();
286+
287+
// This might throw. See https://github.com/GoogleChrome/puppeteer/issues/2778
288+
await rmAsync(userDataDir).catch(e => {});
289+
});
290+
});
184291
};

test/webkit/launcher.spec.js

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,6 @@ module.exports.describe = function ({ testRunner, expect, playwright, defaultBro
2121
const {beforeAll, beforeEach, afterAll, afterEach} = testRunner;
2222

2323
describe('WKPlaywright', function() {
24-
describe('Playwright.launch', function() {
25-
it('should return the default arguments', async() => {
26-
expect(playwright.defaultArgs().length).toBe(0);
27-
});
28-
});
29-
3024
describe('Playwright.launch |pipe| option', function() {
3125
it('should have websocket by default', async() => {
3226
const options = Object.assign({pipe: false}, defaultBrowserOptions);

0 commit comments

Comments
 (0)