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.
|
|
|
|
*/
|
|
|
|
|
2022-06-22 13:25:44 +00:00
|
|
|
import {ServerResponse} from 'http';
|
2023-02-15 23:09:31 +00:00
|
|
|
|
|
|
|
import expect from 'expect';
|
2022-10-06 14:21:24 +00:00
|
|
|
import {TimeoutError} from 'puppeteer';
|
2022-10-05 12:17:03 +00:00
|
|
|
import {Page} from 'puppeteer-core/internal/api/Page.js';
|
|
|
|
import {Target} from 'puppeteer-core/internal/common/Target.js';
|
2023-02-15 23:09:31 +00:00
|
|
|
|
2023-07-03 12:01:29 +00:00
|
|
|
import {getTestState, setupTestBrowserHooks} from './mocha-utils.js';
|
2023-04-25 13:02:25 +00:00
|
|
|
import {waitEvent} from './utils.js';
|
2018-03-29 17:42:23 +00:00
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
describe('Target', function () {
|
2023-07-03 12:01:29 +00:00
|
|
|
setupTestBrowserHooks();
|
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
it('Browser.targets should return all of the targets', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {browser} = await 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(
|
2022-06-22 13:25:44 +00:00
|
|
|
targets.some(target => {
|
2022-06-15 10:09:22 +00:00
|
|
|
return target.type() === 'page' && target.url() === 'about:blank';
|
|
|
|
})
|
|
|
|
).toBeTruthy();
|
|
|
|
expect(
|
2022-06-22 13:25:44 +00:00
|
|
|
targets.some(target => {
|
2022-06-15 10:09:22 +00:00
|
|
|
return target.type() === 'browser';
|
|
|
|
})
|
2020-05-07 10:54:55 +00:00
|
|
|
).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 () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page, context} = await 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();
|
2023-04-25 13:02:25 +00:00
|
|
|
expect(allPages).toHaveLength(1);
|
2020-04-09 05:56:25 +00:00
|
|
|
expect(allPages).toContain(page);
|
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should contain browser target', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {browser} = await getTestState();
|
2018-03-29 17:42:23 +00:00
|
|
|
|
2020-04-09 05:56:25 +00:00
|
|
|
const targets = browser.targets();
|
2022-06-22 13:25:44 +00:00
|
|
|
const browserTarget = targets.find(target => {
|
2022-06-15 10:09:22 +00:00
|
|
|
return 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 () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page, browser} = await 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();
|
2022-06-22 13:25:44 +00:00
|
|
|
const originalPage = allPages.find(p => {
|
2022-06-15 10:09:22 +00:00
|
|
|
return p !== page;
|
|
|
|
})!;
|
2020-05-07 10:54:55 +00:00
|
|
|
expect(
|
2022-06-15 10:09:22 +00:00
|
|
|
await originalPage.evaluate(() => {
|
|
|
|
return ['Hello', 'world'].join(' ');
|
|
|
|
})
|
2020-05-07 10:54:55 +00:00
|
|
|
).toBe('Hello world');
|
2020-04-09 05:56:25 +00:00
|
|
|
expect(await originalPage.$('body')).toBeTruthy();
|
|
|
|
});
|
2022-09-08 10:32:39 +00:00
|
|
|
it('should be able to use async waitForTarget', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page, server, context} = await getTestState();
|
2022-02-18 11:05:29 +00:00
|
|
|
|
|
|
|
const [otherPage] = await Promise.all([
|
|
|
|
context
|
2022-06-22 13:25:44 +00:00
|
|
|
.waitForTarget(target => {
|
|
|
|
return target.page().then(page => {
|
2022-06-15 10:09:22 +00:00
|
|
|
return page!.url() === server.CROSS_PROCESS_PREFIX + '/empty.html';
|
|
|
|
});
|
|
|
|
})
|
2022-06-22 13:25:44 +00:00
|
|
|
.then(target => {
|
2022-06-15 10:09:22 +00:00
|
|
|
return target.page();
|
|
|
|
}),
|
|
|
|
page.evaluate((url: string) => {
|
|
|
|
return window.open(url);
|
|
|
|
}, server.CROSS_PROCESS_PREFIX + '/empty.html'),
|
2022-02-18 11:05:29 +00:00
|
|
|
]);
|
2022-06-15 10:09:22 +00:00
|
|
|
expect(otherPage!.url()).toEqual(
|
2022-02-18 11:05:29 +00:00
|
|
|
server.CROSS_PROCESS_PREFIX + '/empty.html'
|
|
|
|
);
|
|
|
|
expect(page).not.toEqual(otherPage);
|
|
|
|
});
|
2022-09-08 10:32:39 +00:00
|
|
|
it('should report when a new page is created and closed', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page, server, context} = await getTestState();
|
2019-05-10 00:29:18 +00:00
|
|
|
|
2022-09-08 10:32:39 +00:00
|
|
|
const [otherPage] = await Promise.all([
|
|
|
|
context
|
|
|
|
.waitForTarget(target => {
|
|
|
|
return target.url() === server.CROSS_PROCESS_PREFIX + '/empty.html';
|
2022-06-15 10:09:22 +00:00
|
|
|
})
|
2022-09-08 10:32:39 +00:00
|
|
|
.then(target => {
|
|
|
|
return target.page();
|
|
|
|
}),
|
|
|
|
page.evaluate((url: string) => {
|
|
|
|
return window.open(url);
|
|
|
|
}, server.CROSS_PROCESS_PREFIX + '/empty.html'),
|
|
|
|
]);
|
|
|
|
expect(otherPage!.url()).toContain(server.CROSS_PROCESS_PREFIX);
|
|
|
|
expect(
|
|
|
|
await otherPage!.evaluate(() => {
|
|
|
|
return ['Hello', 'world'].join(' ');
|
|
|
|
})
|
|
|
|
).toBe('Hello world');
|
|
|
|
expect(await otherPage!.$('body')).toBeTruthy();
|
2018-03-29 17:42:23 +00:00
|
|
|
|
2022-09-08 10:32:39 +00:00
|
|
|
let allPages = await context.pages();
|
|
|
|
expect(allPages).toContain(page);
|
|
|
|
expect(allPages).toContain(otherPage);
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2023-04-25 13:02:25 +00:00
|
|
|
const [closedTarget] = await Promise.all([
|
|
|
|
waitEvent<Target>(context, 'targetdestroyed'),
|
|
|
|
otherPage!.close(),
|
|
|
|
]);
|
|
|
|
expect(await closedTarget.page()).toBe(otherPage);
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2022-09-08 10:32:39 +00:00
|
|
|
allPages = (await Promise.all(
|
|
|
|
context.targets().map(target => {
|
|
|
|
return target.page();
|
|
|
|
})
|
|
|
|
)) as Page[];
|
|
|
|
expect(allPages).toContain(page);
|
|
|
|
expect(allPages).not.toContain(otherPage);
|
|
|
|
});
|
|
|
|
it('should report when a service worker is created and destroyed', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page, server, context} = await getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2022-09-08 10:32:39 +00:00
|
|
|
await page.goto(server.EMPTY_PAGE);
|
2023-04-25 13:02:25 +00:00
|
|
|
const createdTarget = waitEvent(context, 'targetcreated');
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2022-09-08 10:32:39 +00:00
|
|
|
await page.goto(server.PREFIX + '/serviceworkers/empty/sw.html');
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2022-09-08 10:32:39 +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
|
|
|
|
2023-04-25 13:02:25 +00:00
|
|
|
const destroyedTarget = waitEvent(context, 'targetdestroyed');
|
2022-09-08 10:32:39 +00:00
|
|
|
await page.evaluate(() => {
|
|
|
|
return (
|
|
|
|
globalThis as unknown as {
|
|
|
|
registrationPromise: Promise<{unregister: () => void}>;
|
|
|
|
}
|
|
|
|
).registrationPromise.then((registration: any) => {
|
|
|
|
return registration.unregister();
|
2022-06-15 10:09:22 +00:00
|
|
|
});
|
2022-09-08 10:32:39 +00:00
|
|
|
});
|
|
|
|
expect(await destroyedTarget).toBe(await createdTarget);
|
|
|
|
});
|
|
|
|
it('should create a worker from a service worker', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page, server, context} = await getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
|
|
|
await page.goto(server.PREFIX + '/serviceworkers/empty/sw.html');
|
|
|
|
|
2022-06-22 13:25:44 +00:00
|
|
|
const target = await context.waitForTarget(target => {
|
2022-06-15 10:09:22 +00:00
|
|
|
return target.type() === 'service_worker';
|
|
|
|
});
|
|
|
|
const worker = (await target.worker())!;
|
|
|
|
expect(
|
|
|
|
await worker.evaluate(() => {
|
|
|
|
return self.toString();
|
|
|
|
})
|
|
|
|
).toBe('[object ServiceWorkerGlobalScope]');
|
2020-04-09 05:56:25 +00:00
|
|
|
});
|
2022-09-08 10:32:39 +00:00
|
|
|
it('should create a worker from a shared worker', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page, server, context} = await 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
|
|
|
});
|
2022-06-22 13:25:44 +00:00
|
|
|
const target = await context.waitForTarget(target => {
|
2022-06-15 10:09:22 +00:00
|
|
|
return target.type() === 'shared_worker';
|
|
|
|
});
|
|
|
|
const worker = (await target.worker())!;
|
|
|
|
expect(
|
|
|
|
await worker.evaluate(() => {
|
|
|
|
return self.toString();
|
|
|
|
})
|
|
|
|
).toBe('[object SharedWorkerGlobalScope]');
|
2020-04-09 05:56:25 +00:00
|
|
|
});
|
2022-09-08 10:32:39 +00:00
|
|
|
it('should report when a target url changes', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page, server, context} = await getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
|
|
|
await page.goto(server.EMPTY_PAGE);
|
2023-04-25 13:02:25 +00:00
|
|
|
let changedTarget = waitEvent(context, 'targetchanged');
|
2020-04-09 05:56:25 +00:00
|
|
|
await page.goto(server.CROSS_PROCESS_PREFIX + '/');
|
|
|
|
expect((await changedTarget).url()).toBe(server.CROSS_PROCESS_PREFIX + '/');
|
|
|
|
|
2023-04-25 13:02:25 +00:00
|
|
|
changedTarget = waitEvent(context, 'targetchanged');
|
2020-04-09 05:56:25 +00:00
|
|
|
await page.goto(server.EMPTY_PAGE);
|
|
|
|
expect((await changedTarget).url()).toBe(server.EMPTY_PAGE);
|
|
|
|
});
|
2022-09-08 10:32:39 +00:00
|
|
|
it('should not report uninitialized pages', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {context} = await getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
|
|
|
let targetChanged = false;
|
2022-06-15 10:09:22 +00:00
|
|
|
const listener = () => {
|
|
|
|
return (targetChanged = true);
|
|
|
|
};
|
2020-04-09 05:56:25 +00:00
|
|
|
context.on('targetchanged', listener);
|
2023-04-25 13:02:25 +00:00
|
|
|
const targetPromise = waitEvent<Target>(context, 'targetcreated');
|
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;
|
2023-04-25 13:02:25 +00:00
|
|
|
const targetPromise2 = waitEvent<Target>(context, 'targetcreated');
|
2022-06-15 10:09:22 +00:00
|
|
|
const evaluatePromise = newPage.evaluate(() => {
|
|
|
|
return window.open('about:blank');
|
|
|
|
});
|
2020-04-09 05:56:25 +00:00
|
|
|
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);
|
2023-04-25 13:02:25 +00:00
|
|
|
context.off('targetchanged', listener);
|
2020-04-09 05:56:25 +00:00
|
|
|
});
|
2022-09-08 10:32:39 +00:00
|
|
|
it('should not crash while redirecting if original request was missed', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page, server, context} = await getTestState();
|
2020-05-07 10:54:55 +00:00
|
|
|
|
2022-09-08 10:32:39 +00:00
|
|
|
let serverResponse!: ServerResponse;
|
|
|
|
server.setRoute('/one-style.css', (_req, res) => {
|
|
|
|
return (serverResponse = res);
|
|
|
|
});
|
|
|
|
// Open a new page. Use window.open to connect to the page later.
|
|
|
|
await Promise.all([
|
|
|
|
page.evaluate((url: string) => {
|
|
|
|
return window.open(url);
|
|
|
|
}, server.PREFIX + '/one-style.html'),
|
|
|
|
server.waitForRequest('/one-style.css'),
|
|
|
|
]);
|
|
|
|
// Connect to the opened page.
|
|
|
|
const target = await context.waitForTarget(target => {
|
|
|
|
return 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();
|
|
|
|
});
|
|
|
|
it('should have an opener', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page, server, context} = await getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
|
|
|
await page.goto(server.EMPTY_PAGE);
|
|
|
|
const [createdTarget] = await Promise.all([
|
2023-04-25 13:02:25 +00:00
|
|
|
waitEvent<Target>(context, 'targetcreated'),
|
2020-05-07 10:54:55 +00:00
|
|
|
page.goto(server.PREFIX + '/popup/window-open.html'),
|
2020-04-09 05:56:25 +00:00
|
|
|
]);
|
2022-06-15 10:09:22 +00:00
|
|
|
expect((await createdTarget.page())!.url()).toBe(
|
2020-05-07 10:54:55 +00:00
|
|
|
server.PREFIX + '/popup/popup.html'
|
|
|
|
);
|
2020-04-09 05:56:25 +00:00
|
|
|
expect(createdTarget.opener()).toBe(page.target());
|
2022-05-31 14:34:16 +00:00
|
|
|
expect(page.target().opener()).toBeUndefined();
|
2018-03-29 17:42:23 +00:00
|
|
|
});
|
2019-02-03 01:49:12 +00:00
|
|
|
|
|
|
|
describe('Browser.waitForTarget', () => {
|
2022-09-08 10:32:39 +00:00
|
|
|
it('should wait for a target', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {browser, server} = await getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2019-02-03 01:49:12 +00:00
|
|
|
let resolved = false;
|
2022-06-22 13:25:44 +00:00
|
|
|
const targetPromise = browser.waitForTarget(target => {
|
2022-06-15 10:09:22 +00:00
|
|
|
return target.url() === server.EMPTY_PAGE;
|
|
|
|
});
|
2020-12-07 07:18:32 +00:00
|
|
|
targetPromise
|
2022-06-15 10:09:22 +00:00
|
|
|
.then(() => {
|
|
|
|
return (resolved = true);
|
|
|
|
})
|
2022-06-22 13:25:44 +00:00
|
|
|
.catch(error => {
|
2020-12-07 07:18:32 +00:00
|
|
|
resolved = true;
|
2022-10-06 14:21:24 +00:00
|
|
|
if (error instanceof TimeoutError) {
|
2020-12-07 07:18:32 +00:00
|
|
|
console.error(error);
|
2022-06-14 11:55:35 +00:00
|
|
|
} else {
|
|
|
|
throw error;
|
|
|
|
}
|
2020-12-07 07:18:32 +00:00
|
|
|
});
|
2019-02-03 01:49:12 +00:00
|
|
|
const page = await browser.newPage();
|
|
|
|
expect(resolved).toBe(false);
|
|
|
|
await page.goto(server.EMPTY_PAGE);
|
2020-12-07 07:18:32 +00:00
|
|
|
try {
|
|
|
|
const target = await targetPromise;
|
|
|
|
expect(await target.page()).toBe(page);
|
|
|
|
} catch (error) {
|
2022-10-06 14:21:24 +00:00
|
|
|
if (error instanceof TimeoutError) {
|
2020-12-07 07:18:32 +00:00
|
|
|
console.error(error);
|
2022-06-14 11:55:35 +00:00
|
|
|
} else {
|
|
|
|
throw error;
|
|
|
|
}
|
2020-12-07 07:18:32 +00:00
|
|
|
}
|
2019-02-03 01:49:12 +00:00
|
|
|
await page.close();
|
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should timeout waiting for a non-existent target', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {browser, server} = await getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2022-06-15 10:09:22 +00:00
|
|
|
let error!: Error;
|
2020-05-07 10:54:55 +00:00
|
|
|
await browser
|
2022-06-15 10:09:22 +00:00
|
|
|
.waitForTarget(
|
2022-06-22 13:25:44 +00:00
|
|
|
target => {
|
2022-06-15 10:09:22 +00:00
|
|
|
return target.url() === server.EMPTY_PAGE;
|
|
|
|
},
|
|
|
|
{
|
|
|
|
timeout: 1,
|
|
|
|
}
|
|
|
|
)
|
2022-06-22 13:25:44 +00:00
|
|
|
.catch(error_ => {
|
2022-06-15 10:09:22 +00:00
|
|
|
return (error = error_);
|
|
|
|
});
|
2022-10-06 14:21:24 +00:00
|
|
|
expect(error).toBeInstanceOf(TimeoutError);
|
2019-02-03 01:49:12 +00:00
|
|
|
});
|
|
|
|
});
|
2020-04-09 05:56:25 +00:00
|
|
|
});
|