2020-04-09 05:56:25 +00:00
|
|
|
/**
|
2024-01-03 10:11:33 +00:00
|
|
|
* @license
|
|
|
|
* Copyright 2020 Google Inc.
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
2020-04-09 05:56:25 +00:00
|
|
|
*/
|
|
|
|
|
2020-06-23 05:18:46 +00:00
|
|
|
import expect from 'expect';
|
2023-12-15 12:08:28 +00:00
|
|
|
import type {WebWorker} from 'puppeteer-core/internal/api/WebWorker.js';
|
2023-09-26 16:24:24 +00:00
|
|
|
import type {ConsoleMessage} from 'puppeteer-core/internal/common/ConsoleMessage.js';
|
2023-02-15 23:09:31 +00:00
|
|
|
|
2023-07-03 12:01:29 +00:00
|
|
|
import {getTestState, setupTestBrowserHooks} from './mocha-utils.js';
|
2022-06-22 13:25:44 +00:00
|
|
|
import {waitEvent} from './utils.js';
|
2018-06-07 01:16:17 +00:00
|
|
|
|
2022-09-08 10:32:39 +00:00
|
|
|
describe('Workers', function () {
|
2023-07-03 12:01:29 +00:00
|
|
|
setupTestBrowserHooks();
|
|
|
|
|
2020-05-07 10:54:55 +00:00
|
|
|
it('Page.workers', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page, server} = await getTestState();
|
2018-06-07 01:16:17 +00:00
|
|
|
|
2020-04-09 05:56:25 +00:00
|
|
|
await Promise.all([
|
2023-04-25 13:02:25 +00:00
|
|
|
waitEvent(page, 'workercreated'),
|
2020-05-07 10:54:55 +00:00
|
|
|
page.goto(server.PREFIX + '/worker/worker.html'),
|
|
|
|
]);
|
2022-06-15 10:09:22 +00:00
|
|
|
const worker = page.workers()[0]!;
|
|
|
|
expect(worker?.url()).toContain('worker.js');
|
2018-06-07 01:16:17 +00:00
|
|
|
|
2022-06-15 10:09:22 +00:00
|
|
|
expect(
|
|
|
|
await worker?.evaluate(() => {
|
|
|
|
return (globalThis as any).workerFunction();
|
|
|
|
})
|
|
|
|
).toBe('worker function result');
|
2018-06-07 01:16:17 +00:00
|
|
|
|
2020-04-09 05:56:25 +00:00
|
|
|
await page.goto(server.EMPTY_PAGE);
|
2023-04-25 13:02:25 +00:00
|
|
|
expect(page.workers()).toHaveLength(0);
|
2020-04-09 05:56:25 +00:00
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should emit created and destroyed events', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page} = await getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2023-04-25 13:02:25 +00:00
|
|
|
const workerCreatedPromise = waitEvent<WebWorker>(page, 'workercreated');
|
2023-08-30 10:02:59 +00:00
|
|
|
using workerObj = await page.evaluateHandle(() => {
|
2022-06-15 10:09:22 +00:00
|
|
|
return new Worker('data:text/javascript,1');
|
|
|
|
});
|
2020-04-09 05:56:25 +00:00
|
|
|
const worker = await workerCreatedPromise;
|
2023-08-30 10:02:59 +00:00
|
|
|
using workerThisObj = await worker.evaluateHandle(() => {
|
2022-06-15 10:09:22 +00:00
|
|
|
return this;
|
|
|
|
});
|
2023-04-25 13:02:25 +00:00
|
|
|
const workerDestroyedPromise = waitEvent(page, 'workerdestroyed');
|
2022-06-15 10:09:22 +00:00
|
|
|
await page.evaluate((workerObj: Worker) => {
|
|
|
|
return workerObj.terminate();
|
|
|
|
}, workerObj);
|
2020-04-09 05:56:25 +00:00
|
|
|
expect(await workerDestroyedPromise).toBe(worker);
|
2022-06-22 13:25:44 +00:00
|
|
|
const error = await workerThisObj.getProperty('self').catch(error => {
|
2022-06-15 10:09:22 +00:00
|
|
|
return error;
|
|
|
|
});
|
2024-02-13 15:09:33 +00:00
|
|
|
expect(error.message).atLeastOneToContain([
|
|
|
|
'Most likely the worker has been closed.',
|
|
|
|
'Realm already destroyed.',
|
|
|
|
]);
|
2020-04-09 05:56:25 +00:00
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should report console logs', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page} = await getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
|
|
|
const [message] = await Promise.all([
|
|
|
|
waitEvent(page, 'console'),
|
2022-06-15 10:09:22 +00:00
|
|
|
page.evaluate(() => {
|
|
|
|
return new Worker(`data:text/javascript,console.log(1)`);
|
|
|
|
}),
|
2020-04-09 05:56:25 +00:00
|
|
|
]);
|
|
|
|
expect(message.text()).toBe('1');
|
|
|
|
expect(message.location()).toEqual({
|
2021-02-02 07:39:29 +00:00
|
|
|
url: '',
|
2020-04-09 05:56:25 +00:00
|
|
|
lineNumber: 0,
|
|
|
|
columnNumber: 8,
|
2018-06-25 22:10:36 +00:00
|
|
|
});
|
2018-06-07 01:16:17 +00:00
|
|
|
});
|
2024-02-13 15:09:33 +00:00
|
|
|
it('should work with console logs', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page} = await getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2023-04-25 13:02:25 +00:00
|
|
|
const logPromise = waitEvent<ConsoleMessage>(page, 'console');
|
2022-06-15 10:09:22 +00:00
|
|
|
await page.evaluate(() => {
|
|
|
|
return new Worker(`data:text/javascript,console.log(1,2,3,this)`);
|
|
|
|
});
|
2020-04-09 05:56:25 +00:00
|
|
|
const log = await logPromise;
|
|
|
|
expect(log.text()).toBe('1 2 3 JSHandle@object');
|
2023-04-25 13:02:25 +00:00
|
|
|
expect(log.args()).toHaveLength(4);
|
2020-04-09 05:56:25 +00:00
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should have an execution context', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page} = await getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2023-04-25 13:02:25 +00:00
|
|
|
const workerCreatedPromise = waitEvent<WebWorker>(page, 'workercreated');
|
2022-06-15 10:09:22 +00:00
|
|
|
await page.evaluate(() => {
|
|
|
|
return new Worker(`data:text/javascript,console.log(1)`);
|
|
|
|
});
|
2020-04-09 05:56:25 +00:00
|
|
|
const worker = await workerCreatedPromise;
|
2023-09-01 12:12:29 +00:00
|
|
|
expect(await worker.evaluate('1+1')).toBe(2);
|
2020-04-09 05:56:25 +00:00
|
|
|
});
|
2020-05-07 10:54:55 +00:00
|
|
|
it('should report errors', async () => {
|
2023-06-21 19:41:09 +00:00
|
|
|
const {page} = await getTestState();
|
2020-04-09 05:56:25 +00:00
|
|
|
|
2023-04-25 13:02:25 +00:00
|
|
|
const errorPromise = waitEvent<Error>(page, 'pageerror');
|
2022-06-15 10:09:22 +00:00
|
|
|
await page.evaluate(() => {
|
|
|
|
return new Worker(
|
|
|
|
`data:text/javascript, throw new Error('this is my error');`
|
|
|
|
);
|
|
|
|
});
|
2020-04-09 05:56:25 +00:00
|
|
|
const errorLog = await errorPromise;
|
|
|
|
expect(errorLog.message).toContain('this is my error');
|
|
|
|
});
|
2024-02-13 12:40:59 +00:00
|
|
|
|
|
|
|
it('can be closed', async () => {
|
|
|
|
const {page, server} = await getTestState();
|
|
|
|
|
|
|
|
await Promise.all([
|
|
|
|
waitEvent(page, 'workercreated'),
|
|
|
|
page.goto(server.PREFIX + '/worker/worker.html'),
|
|
|
|
]);
|
|
|
|
const worker = page.workers()[0]!;
|
|
|
|
expect(worker?.url()).toContain('worker.js');
|
|
|
|
|
|
|
|
await Promise.all([waitEvent(page, 'workerdestroyed'), worker?.close()]);
|
|
|
|
});
|
2020-04-09 05:56:25 +00:00
|
|
|
});
|