mirror of
https://github.com/puppeteer/puppeteer
synced 2024-06-14 14:02:48 +00:00
feat(Page): introduce workers (#2560)
This adds `page.workers()`, and two events `workercreated` and `workerdestroyed`. It also forwards logs from a worker into the page `console` event. Only dedicated workers are supported for now, ServiceWorkers will probably work differently because they aren't necessarily associated with a single page. Fixes #2350.
This commit is contained in:
committed by
Andrey Lushnikov
parent
b474f2ce87
commit
93fe2b57d6
14
test/assets/worker/worker.html
Normal file
14
test/assets/worker/worker.html
Normal file
@@ -0,0 +1,14 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>Worker test</title>
|
||||
</head>
|
||||
<body>
|
||||
<script>
|
||||
var worker = new Worker('worker.js');
|
||||
worker.onmessage = function(message) {
|
||||
console.log(message.data);
|
||||
};
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
16
test/assets/worker/worker.js
Normal file
16
test/assets/worker/worker.js
Normal file
@@ -0,0 +1,16 @@
|
||||
console.log('hello from the worker');
|
||||
|
||||
function workerFunction() {
|
||||
return 'worker function result';
|
||||
}
|
||||
|
||||
self.addEventListener('message', event => {
|
||||
console.log('got this data: ' + event.data);
|
||||
});
|
||||
|
||||
(async function() {
|
||||
while (true) {
|
||||
self.postMessage(workerFunction.toString());
|
||||
await new Promise(x => setTimeout(x, 100));
|
||||
}
|
||||
})();
|
||||
@@ -1611,6 +1611,33 @@ module.exports.addTests = function({testRunner, expect, puppeteer, DeviceDescrip
|
||||
await closedPromise;
|
||||
});
|
||||
});
|
||||
describe('Workers', function() {
|
||||
it('Page.workers', async function({page, server}) {
|
||||
await page.goto(server.PREFIX + '/worker/worker.html');
|
||||
await page.waitForFunction(() => !!worker);
|
||||
const worker = page.workers()[0];
|
||||
expect(worker.url()).toContain('worker.js');
|
||||
const executionContext = await worker.executionContext();
|
||||
expect(await executionContext.evaluate(() => self.workerFunction())).toBe('worker function result');
|
||||
|
||||
await page.goto(server.EMPTY_PAGE);
|
||||
expect(page.workers()).toEqual([]);
|
||||
});
|
||||
it('should emit created and destroyed events', async function({page}) {
|
||||
const workerCreatedPromise = new Promise(x => page.once('workercreated', x));
|
||||
const workerObj = await page.evaluateHandle(() => new Worker('data:text/javascript,1'));
|
||||
const worker = await workerCreatedPromise;
|
||||
const workerDestroyedPromise = new Promise(x => page.once('workerdestroyed', x));
|
||||
await page.evaluate(workerObj => workerObj.terminate(), workerObj);
|
||||
expect(await workerDestroyedPromise).toBe(worker);
|
||||
});
|
||||
it('should report console logs', async function({page}) {
|
||||
const logPromise = new Promise(x => page.on('console', x));
|
||||
await page.evaluate(() => new Worker(`data:text/javascript,console.log(1)`));
|
||||
const log = await logPromise;
|
||||
expect(log.text()).toBe('1');
|
||||
});
|
||||
});
|
||||
|
||||
describe('Page.browser', function() {
|
||||
it('should return the correct browser instance', async function({ page, browser }) {
|
||||
|
||||
Reference in New Issue
Block a user