2018-03-29 17:42:23 +00:00
|
|
|
/**
|
|
|
|
* Copyright 2018 Google Inc. All rights reserved.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2020-07-13 09:22:26 +00:00
|
|
|
import utils from './utils.js';
|
2020-05-07 10:54:55 +00:00
|
|
|
const { waitEvent } = utils;
|
2020-06-23 05:18:46 +00:00
|
|
|
import expect from 'expect';
|
|
|
|
import {
|
2020-05-07 10:54:55 +00:00
|
|
|
getTestState,
|
|
|
|
setupTestBrowserHooks,
|
|
|
|
setupTestPageAndContextHooks,
|
2020-06-23 05:18:46 +00:00
|
|
|
itFailsFirefox,
|
2020-07-13 09:22:26 +00:00
|
|
|
} from './mocha-utils'; // eslint-disable-line import/extensions
|
|
|
|
import { Target } from '../lib/cjs/common/Target.js';
|
2018-03-29 17:42:23 +00:00
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
describe('Target', function () {
|
2020-04-09 05:56:25 +00:00
|
|
|
setupTestBrowserHooks();
|
|
|
|
setupTestPageAndContextHooks();
|
2018-03-29 17:42:23 +00:00
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
it('Browser.targets should return all of the targets', async () => {
|
|
|
|
const { browser } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
|
|
|
// The pages will be the testing page and the original newtab page
|
|
|
|
const targets = browser.targets();
|
2020-05-07 10:54:55 +00:00
|
|
|
expect(
|
|
|
|
targets.some(
|
|
|
|
(target) => target.type() === 'page' && target.url() === 'about:blank'
|
|
|
|
)
|
|
|
|
).toBeTruthy();
|
|
|
|
expect(targets.some((target) => target.type() === 'browser')).toBeTruthy();
|
2020-04-09 05:56:25 +00:00
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('Browser.pages should return all of the pages', async () => {
|
|
|
|
const { page, context } = getTestState();
|
2018-03-29 17:42:23 +00:00
|
|
|
|
2020-04-09 05:56:25 +00:00
|
|
|
// The pages will be the testing page
|
|
|
|
const allPages = await context.pages();
|
|
|
|
expect(allPages.length).toBe(1);
|
|
|
|
expect(allPages).toContain(page);
|
|
|
|
expect(allPages[0]).not.toBe(allPages[1]);
|
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should contain browser target', async () => {
|
|
|
|
const { browser } = getTestState();
|
2018-03-29 17:42:23 +00:00
|
|
|
|
2020-04-09 05:56:25 +00:00
|
|
|
const targets = browser.targets();
|
2020-05-07 10:54:55 +00:00
|
|
|
const browserTarget = targets.find((target) => target.type() === 'browser');
|
2020-04-09 05:56:25 +00:00
|
|
|
expect(browserTarget).toBeTruthy();
|
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should be able to use the default page in the browser', async () => {
|
|
|
|
const { page, browser } = getTestState();
|
2018-03-29 17:42:23 +00:00
|
|
|
|
2020-04-09 05:56:25 +00:00
|
|
|
// The pages will be the testing page and the original newtab page
|
|
|
|
const allPages = await browser.pages();
|
2020-05-07 10:54:55 +00:00
|
|
|
const originalPage = allPages.find((p) => p !== page);
|
|
|
|
expect(
|
|
|
|
await originalPage.evaluate(() => ['Hello', 'world'].join(' '))
|
|
|
|
).toBe('Hello world');
|
2020-04-09 05:56:25 +00:00
|
|
|
expect(await originalPage.$('body')).toBeTruthy();
|
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
itFailsFirefox(
|
|
|
|
'should report when a new page is created and closed',
|
|
|
|
async () => {
|
|
|
|
const { page, server, context } = getTestState();
|
2019-05-10 00:29:18 +00:00
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
const [otherPage] = await Promise.all([
|
|
|
|
context
|
|
|
|
.waitForTarget(
|
|
|
|
(target) =>
|
|
|
|
target.url() === server.CROSS_PROCESS_PREFIX + '/empty.html'
|
|
|
|
)
|
|
|
|
.then((target) => target.page()),
|
|
|
|
page.evaluate(
|
2020-07-10 10:52:13 +00:00
|
|
|
(url: string) => window.open(url),
|
2020-05-07 10:54:55 +00:00
|
|
|
server.CROSS_PROCESS_PREFIX + '/empty.html'
|
|
|
|
),
|
|
|
|
]);
|
|
|
|
expect(otherPage.url()).toContain(server.CROSS_PROCESS_PREFIX);
|
|
|
|
expect(await otherPage.evaluate(() => ['Hello', 'world'].join(' '))).toBe(
|
|
|
|
'Hello world'
|
|
|
|
);
|
|
|
|
expect(await otherPage.$('body')).toBeTruthy();
|
2018-03-29 17:42:23 +00:00
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
let allPages = await context.pages();
|
|
|
|
expect(allPages).toContain(page);
|
|
|
|
expect(allPages).toContain(otherPage);
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
const closePagePromise = new Promise((fulfill) =>
|
|
|
|
context.once('targetdestroyed', (target) => fulfill(target.page()))
|
|
|
|
);
|
|
|
|
await otherPage.close();
|
|
|
|
expect(await closePagePromise).toBe(otherPage);
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
allPages = await Promise.all(
|
|
|
|
context.targets().map((target) => target.page())
|
|
|
|
);
|
|
|
|
expect(allPages).toContain(page);
|
|
|
|
expect(allPages).not.toContain(otherPage);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
itFailsFirefox(
|
|
|
|
'should report when a service worker is created and destroyed',
|
|
|
|
async () => {
|
|
|
|
const { page, server, context } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
await page.goto(server.EMPTY_PAGE);
|
2020-06-23 05:18:46 +00:00
|
|
|
const createdTarget = new Promise<Target>((fulfill) =>
|
2020-05-07 10:54:55 +00:00
|
|
|
context.once('targetcreated', (target) => fulfill(target))
|
|
|
|
);
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
await page.goto(server.PREFIX + '/serviceworkers/empty/sw.html');
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
expect((await createdTarget).type()).toBe('service_worker');
|
|
|
|
expect((await createdTarget).url()).toBe(
|
|
|
|
server.PREFIX + '/serviceworkers/empty/sw.js'
|
|
|
|
);
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
const destroyedTarget = new Promise((fulfill) =>
|
|
|
|
context.once('targetdestroyed', (target) => fulfill(target))
|
|
|
|
);
|
|
|
|
await page.evaluate(() =>
|
2020-06-23 05:18:46 +00:00
|
|
|
globalThis.registrationPromise.then((registration) =>
|
2020-05-07 10:54:55 +00:00
|
|
|
registration.unregister()
|
|
|
|
)
|
|
|
|
);
|
|
|
|
expect(await destroyedTarget).toBe(await createdTarget);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
itFailsFirefox('should create a worker from a service worker', async () => {
|
|
|
|
const { page, server, context } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
|
|
|
await page.goto(server.PREFIX + '/serviceworkers/empty/sw.html');
|
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
const target = await context.waitForTarget(
|
|
|
|
(target) => target.type() === 'service_worker'
|
|
|
|
);
|
2020-04-09 05:56:25 +00:00
|
|
|
const worker = await target.worker();
|
2020-05-07 10:54:55 +00:00
|
|
|
expect(await worker.evaluate(() => self.toString())).toBe(
|
|
|
|
'[object ServiceWorkerGlobalScope]'
|
|
|
|
);
|
2020-04-09 05:56:25 +00:00
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
itFailsFirefox('should create a worker from a shared worker', async () => {
|
|
|
|
const { page, server, context } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
|
|
|
await page.goto(server.EMPTY_PAGE);
|
|
|
|
await page.evaluate(() => {
|
|
|
|
new SharedWorker('data:text/javascript,console.log("hi")');
|
2018-06-01 00:06:29 +00:00
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
const target = await context.waitForTarget(
|
|
|
|
(target) => target.type() === 'shared_worker'
|
|
|
|
);
|
2020-04-09 05:56:25 +00:00
|
|
|
const worker = await target.worker();
|
2020-05-07 10:54:55 +00:00
|
|
|
expect(await worker.evaluate(() => self.toString())).toBe(
|
|
|
|
'[object SharedWorkerGlobalScope]'
|
|
|
|
);
|
2020-04-09 05:56:25 +00:00
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
itFailsFirefox('should report when a target url changes', async () => {
|
|
|
|
const { page, server, context } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
|
|
|
await page.goto(server.EMPTY_PAGE);
|
2020-06-23 05:18:46 +00:00
|
|
|
let changedTarget = new Promise<Target>((fulfill) =>
|
2020-05-07 10:54:55 +00:00
|
|
|
context.once('targetchanged', (target) => fulfill(target))
|
|
|
|
);
|
2020-04-09 05:56:25 +00:00
|
|
|
await page.goto(server.CROSS_PROCESS_PREFIX + '/');
|
|
|
|
expect((await changedTarget).url()).toBe(server.CROSS_PROCESS_PREFIX + '/');
|
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
changedTarget = new Promise((fulfill) =>
|
|
|
|
context.once('targetchanged', (target) => fulfill(target))
|
|
|
|
);
|
2020-04-09 05:56:25 +00:00
|
|
|
await page.goto(server.EMPTY_PAGE);
|
|
|
|
expect((await changedTarget).url()).toBe(server.EMPTY_PAGE);
|
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
itFailsFirefox('should not report uninitialized pages', async () => {
|
|
|
|
const { context } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
|
|
|
let targetChanged = false;
|
2020-05-07 10:54:55 +00:00
|
|
|
const listener = () => (targetChanged = true);
|
2020-04-09 05:56:25 +00:00
|
|
|
context.on('targetchanged', listener);
|
2020-06-23 05:18:46 +00:00
|
|
|
const targetPromise = new Promise<Target>((fulfill) =>
|
2020-05-07 10:54:55 +00:00
|
|
|
context.once('targetcreated', (target) => fulfill(target))
|
|
|
|
);
|
2020-04-09 05:56:25 +00:00
|
|
|
const newPagePromise = context.newPage();
|
|
|
|
const target = await targetPromise;
|
|
|
|
expect(target.url()).toBe('about:blank');
|
|
|
|
|
|
|
|
const newPage = await newPagePromise;
|
2020-06-23 05:18:46 +00:00
|
|
|
const targetPromise2 = new Promise<Target>((fulfill) =>
|
2020-05-07 10:54:55 +00:00
|
|
|
context.once('targetcreated', (target) => fulfill(target))
|
|
|
|
);
|
2020-04-09 05:56:25 +00:00
|
|
|
const evaluatePromise = newPage.evaluate(() => window.open('about:blank'));
|
|
|
|
const target2 = await targetPromise2;
|
|
|
|
expect(target2.url()).toBe('about:blank');
|
|
|
|
await evaluatePromise;
|
|
|
|
await newPage.close();
|
2020-06-23 05:18:46 +00:00
|
|
|
expect(targetChanged).toBe(false);
|
2020-04-09 05:56:25 +00:00
|
|
|
context.removeListener('targetchanged', listener);
|
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
itFailsFirefox(
|
|
|
|
'should not crash while redirecting if original request was missed',
|
|
|
|
async () => {
|
|
|
|
const { page, server, context } = getTestState();
|
|
|
|
|
|
|
|
let serverResponse = null;
|
|
|
|
server.setRoute('/one-style.css', (req, res) => (serverResponse = res));
|
|
|
|
// Open a new page. Use window.open to connect to the page later.
|
|
|
|
await Promise.all([
|
|
|
|
page.evaluate(
|
2020-07-10 10:52:13 +00:00
|
|
|
(url: string) => window.open(url),
|
2020-05-07 10:54:55 +00:00
|
|
|
server.PREFIX + '/one-style.html'
|
|
|
|
),
|
|
|
|
server.waitForRequest('/one-style.css'),
|
|
|
|
]);
|
|
|
|
// Connect to the opened page.
|
|
|
|
const target = await context.waitForTarget((target) =>
|
|
|
|
target.url().includes('one-style.html')
|
|
|
|
);
|
|
|
|
const newPage = await target.page();
|
|
|
|
// Issue a redirect.
|
|
|
|
serverResponse.writeHead(302, { location: '/injectedstyle.css' });
|
|
|
|
serverResponse.end();
|
|
|
|
// Wait for the new page to load.
|
|
|
|
await waitEvent(newPage, 'load');
|
|
|
|
// Cleanup.
|
|
|
|
await newPage.close();
|
|
|
|
}
|
|
|
|
);
|
|
|
|
itFailsFirefox('should have an opener', async () => {
|
|
|
|
const { page, server, context } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
|
|
|
await page.goto(server.EMPTY_PAGE);
|
|
|
|
const [createdTarget] = await Promise.all([
|
2020-06-23 05:18:46 +00:00
|
|
|
new Promise<Target>((fulfill) =>
|
2020-05-07 10:54:55 +00:00
|
|
|
context.once('targetcreated', (target) => fulfill(target))
|
|
|
|
),
|
|
|
|
page.goto(server.PREFIX + '/popup/window-open.html'),
|
2020-04-09 05:56:25 +00:00
|
|
|
]);
|
2020-05-07 10:54:55 +00:00
|
|
|
expect((await createdTarget.page()).url()).toBe(
|
|
|
|
server.PREFIX + '/popup/popup.html'
|
|
|
|
);
|
2020-04-09 05:56:25 +00:00
|
|
|
expect(createdTarget.opener()).toBe(page.target());
|
|
|
|
expect(page.target().opener()).toBe(null);
|
2018-03-29 17:42:23 +00:00
|
|
|
});
|
2019-02-03 01:49:12 +00:00
|
|
|
|
|
|
|
describe('Browser.waitForTarget', () => {
|
2020-05-07 10:54:55 +00:00
|
|
|
itFailsFirefox('should wait for a target', async () => {
|
|
|
|
const { browser, server } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2019-02-03 01:49:12 +00:00
|
|
|
let resolved = false;
|
2020-05-07 10:54:55 +00:00
|
|
|
const targetPromise = browser.waitForTarget(
|
|
|
|
(target) => target.url() === server.EMPTY_PAGE
|
|
|
|
);
|
|
|
|
targetPromise.then(() => (resolved = true));
|
2019-02-03 01:49:12 +00:00
|
|
|
const page = await browser.newPage();
|
|
|
|
expect(resolved).toBe(false);
|
|
|
|
await page.goto(server.EMPTY_PAGE);
|
|
|
|
const target = await targetPromise;
|
|
|
|
expect(await target.page()).toBe(page);
|
|
|
|
await page.close();
|
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should timeout waiting for a non-existent target', async () => {
|
|
|
|
const { browser, server, puppeteer } = getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2019-02-03 01:49:12 +00:00
|
|
|
let error = null;
|
2020-05-07 10:54:55 +00:00
|
|
|
await browser
|
|
|
|
.waitForTarget((target) => target.url() === server.EMPTY_PAGE, {
|
|
|
|
timeout: 1,
|
|
|
|
})
|
|
|
|
.catch((error_) => (error = error_));
|
2019-04-19 22:33:06 +00:00
|
|
|
expect(error).toBeInstanceOf(puppeteer.errors.TimeoutError);
|
2019-02-03 01:49:12 +00:00
|
|
|
});
|
|
|
|
});
|
2020-04-09 05:56:25 +00:00
|
|
|
});
|