2018-06-01 20:48:34 +00:00
|
|
|
/**
|
2024-01-03 10:11:33 +00:00
|
|
|
* @license
|
|
|
|
* Copyright 2018 Google Inc.
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
2018-06-01 20:48:34 +00:00
|
|
|
*/
|
|
|
|
|
2023-03-14 17:30:41 +00:00
|
|
|
import {mkdtemp} from 'fs/promises';
|
2022-06-15 10:09:22 +00:00
|
|
|
import os from 'os';
|
|
|
|
import path from 'path';
|
2023-02-15 23:09:31 +00:00
|
|
|
|
|
|
|
import expect from 'expect';
|
2023-09-26 16:24:24 +00:00
|
|
|
import type {PuppeteerLaunchOptions} from 'puppeteer-core/internal/node/PuppeteerNode.js';
|
2023-04-21 07:45:04 +00:00
|
|
|
import {rmSync} from 'puppeteer-core/internal/node/util/fs.js';
|
2023-02-15 23:09:31 +00:00
|
|
|
|
2023-07-18 18:18:22 +00:00
|
|
|
import {getTestState, isHeadless, launch} from './mocha-utils.js';
|
2019-10-16 15:00:20 +00:00
|
|
|
|
2018-06-01 20:48:34 +00:00
|
|
|
const TMP_FOLDER = path.join(os.tmpdir(), 'pptr_tmp_folder-');
|
|
|
|
|
2023-07-18 18:18:22 +00:00
|
|
|
(!isHeadless ? describe : describe.skip)('headful tests', function () {
|
2020-04-09 05:56:25 +00:00
|
|
|
/* These tests fire up an actual browser so let's
|
|
|
|
* allow a higher timeout
|
|
|
|
*/
|
2023-06-16 07:16:04 +00:00
|
|
|
this.timeout(20_000);
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2022-06-15 10:09:22 +00:00
|
|
|
let headfulOptions: PuppeteerLaunchOptions | undefined;
|
2022-06-22 13:25:44 +00:00
|
|
|
let headlessOptions: PuppeteerLaunchOptions & {headless: boolean};
|
2024-01-04 06:51:47 +00:00
|
|
|
|
2023-06-12 08:44:18 +00:00
|
|
|
const browsers: Array<() => Promise<void>> = [];
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2023-06-21 19:41:09 +00:00
|
|
|
beforeEach(async () => {
|
2024-01-04 06:51:47 +00:00
|
|
|
const {defaultBrowserOptions} = await getTestState({
|
2023-06-21 19:41:09 +00:00
|
|
|
skipLaunch: true,
|
|
|
|
});
|
2020-04-09 05:56:25 +00:00
|
|
|
headfulOptions = Object.assign({}, defaultBrowserOptions, {
|
2020-05-07 10:54:55 +00:00
|
|
|
headless: false,
|
2020-04-09 05:56:25 +00:00
|
|
|
});
|
|
|
|
headlessOptions = Object.assign({}, defaultBrowserOptions, {
|
2020-05-07 10:54:55 +00:00
|
|
|
headless: true,
|
2020-04-09 05:56:25 +00:00
|
|
|
});
|
2018-06-01 20:48:34 +00:00
|
|
|
});
|
|
|
|
|
2023-06-12 08:44:18 +00:00
|
|
|
async function launchBrowser(options: any) {
|
|
|
|
const {browser, close} = await launch(options, {createContext: false});
|
|
|
|
browsers.push(close);
|
2021-05-26 09:17:56 +00:00
|
|
|
return browser;
|
|
|
|
}
|
|
|
|
|
2023-06-12 08:44:18 +00:00
|
|
|
afterEach(async () => {
|
|
|
|
await Promise.all(
|
|
|
|
browsers.map((close, index) => {
|
|
|
|
delete browsers[index];
|
|
|
|
return close();
|
|
|
|
})
|
|
|
|
);
|
2021-05-26 09:17:56 +00:00
|
|
|
});
|
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
describe('HEADFUL', function () {
|
2022-09-08 10:32:39 +00:00
|
|
|
it('headless should be able to read cookies written by headful', async () => {
|
|
|
|
/* Needs investigation into why but this fails consistently on Windows CI. */
|
2023-06-21 19:41:09 +00:00
|
|
|
const {server} = await getTestState({skipLaunch: true});
|
2020-05-07 10:54:55 +00:00
|
|
|
|
2023-03-14 17:30:41 +00:00
|
|
|
const userDataDir = await mkdtemp(TMP_FOLDER);
|
2022-09-08 10:32:39 +00:00
|
|
|
// Write a cookie in headful chrome
|
|
|
|
const headfulBrowser = await launchBrowser(
|
|
|
|
Object.assign({userDataDir}, headfulOptions)
|
|
|
|
);
|
|
|
|
const headfulPage = await headfulBrowser.newPage();
|
|
|
|
await headfulPage.goto(server.EMPTY_PAGE);
|
|
|
|
await headfulPage.evaluate(() => {
|
|
|
|
return (document.cookie =
|
|
|
|
'foo=true; expires=Fri, 31 Dec 9999 23:59:59 GMT');
|
|
|
|
});
|
|
|
|
await headfulBrowser.close();
|
|
|
|
// Read the cookie from headless chrome
|
|
|
|
const headlessBrowser = await launchBrowser(
|
|
|
|
Object.assign({userDataDir}, headlessOptions)
|
|
|
|
);
|
|
|
|
const headlessPage = await headlessBrowser.newPage();
|
|
|
|
await headlessPage.goto(server.EMPTY_PAGE);
|
|
|
|
const cookie = await headlessPage.evaluate(() => {
|
|
|
|
return document.cookie;
|
|
|
|
});
|
|
|
|
await headlessBrowser.close();
|
|
|
|
// This might throw. See https://github.com/puppeteer/puppeteer/issues/2778
|
2023-03-21 10:39:15 +00:00
|
|
|
try {
|
|
|
|
rmSync(userDataDir);
|
|
|
|
} catch {}
|
2022-09-08 10:32:39 +00:00
|
|
|
expect(cookie).toBe('foo=true');
|
|
|
|
});
|
2021-09-23 12:37:35 +00:00
|
|
|
});
|
2020-04-09 05:56:25 +00:00
|
|
|
});
|