mirror of
https://github.com/puppeteer/puppeteer
synced 2024-06-14 14:02:48 +00:00
feat: expose frame's execution contexts (#3048)
This patch exposes frame's execution contexts, making it possible to debug extension's content scripts. This is a resurrected #2812.
This commit is contained in:
3
test/assets/simple-extension/content-script.js
Normal file
3
test/assets/simple-extension/content-script.js
Normal file
@@ -0,0 +1,3 @@
|
||||
console.log('hey from the content-script');
|
||||
self.thisIsTheContentScript = true;
|
||||
|
||||
@@ -1,12 +1,14 @@
|
||||
{
|
||||
"name": "Simple extension",
|
||||
"version": "0.1",
|
||||
"app": {
|
||||
"background": {
|
||||
"scripts": ["index.js"]
|
||||
}
|
||||
"background": {
|
||||
"scripts": ["index.js"]
|
||||
},
|
||||
"permissions": ["background"],
|
||||
|
||||
"content_scripts": [{
|
||||
"matches": ["<all_urls>"],
|
||||
"css": [],
|
||||
"js": ["content-script.js"]
|
||||
}],
|
||||
"permissions": ["background", "activeTab"],
|
||||
"manifest_version": 2
|
||||
}
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
|
||||
const path = require('path');
|
||||
const os = require('os');
|
||||
const {waitEvent} = require('./utils.js');
|
||||
|
||||
const TMP_FOLDER = path.join(os.tmpdir(), 'pptr_tmp_folder-');
|
||||
|
||||
@@ -115,6 +116,27 @@ module.exports.addTests = function({testRunner, expect, PROJECT_ROOT, defaultBro
|
||||
]);
|
||||
await browser.close();
|
||||
});
|
||||
it('should report content script execution contexts', async({server}) => {
|
||||
const browserWithExtension = await puppeteer.launch(extensionOptions);
|
||||
const page = await browserWithExtension.newPage();
|
||||
const [extensionContext, msg] = await Promise.all([
|
||||
waitEvent(page, 'executioncontextcreated', context => !context.isDefault()),
|
||||
waitEvent(page, 'console'),
|
||||
page.goto(server.EMPTY_PAGE)
|
||||
]);
|
||||
expect(msg.text()).toBe('hey from the content-script');
|
||||
expect(page.mainFrame().executionContexts().length).toBe(2);
|
||||
expect(extensionContext.frame()).toBe(page.mainFrame());
|
||||
expect(extensionContext.name()).toBe('Simple extension');
|
||||
expect(await extensionContext.evaluate('thisIsTheContentScript')).toBe(true);
|
||||
|
||||
const [destroyedContext] = await Promise.all([
|
||||
waitEvent(page, 'executioncontextdestroyed', context => !context.isDefault()),
|
||||
page.reload()
|
||||
]);
|
||||
expect(destroyedContext).toBe(extensionContext);
|
||||
await browserWithExtension.close();
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
@@ -77,6 +77,11 @@ module.exports.addTests = function({testRunner, expect, puppeteer, DeviceDescrip
|
||||
const result = await page.evaluate(() => 7 * 3);
|
||||
expect(result).toBe(21);
|
||||
});
|
||||
it('should have nice default execution context', async({page, server}) => {
|
||||
const executionContext = await page.mainFrame().executionContext();
|
||||
expect(executionContext.name()).toBe('');
|
||||
expect(executionContext.isDefault()).toBe(true);
|
||||
});
|
||||
it('should throw when evaluation triggers reload', async({page, server}) => {
|
||||
let error = null;
|
||||
await page.evaluate(() => {
|
||||
|
||||
@@ -78,7 +78,14 @@ const utils = module.exports = {
|
||||
* @param {string} eventName
|
||||
* @return {!Promise<!Object>}
|
||||
*/
|
||||
waitEvent: function(emitter, eventName) {
|
||||
return new Promise(fulfill => emitter.once(eventName, fulfill));
|
||||
waitEvent: function(emitter, eventName, predicate = () => true) {
|
||||
return new Promise(fulfill => {
|
||||
emitter.on(eventName, function listener(event) {
|
||||
if (!predicate(event))
|
||||
return;
|
||||
emitter.removeListener(eventName, listener);
|
||||
fulfill(event);
|
||||
});
|
||||
});
|
||||
},
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user