Skip to content

Commit

Permalink
feat: allow to create isolated context
Browse files Browse the repository at this point in the history
Implementes puppeteer#2671
  • Loading branch information
furstenheim committed Jun 30, 2018
1 parent fb7c4e0 commit e632cae
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
21 changes: 21 additions & 0 deletions lib/Page.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const {NetworkManager} = require('./NetworkManager');
const NavigatorWatcher = require('./NavigatorWatcher');
const Dialog = require('./Dialog');
const EmulationManager = require('./EmulationManager');
const {ExecutionContext} = require('./ExecutionContext');
const {FrameManager} = require('./FrameManager');
const {Keyboard, Mouse, Touchscreen} = require('./Input');
const Tracing = require('./Tracing');
Expand Down Expand Up @@ -269,6 +270,26 @@ class Page extends EventEmitter {
return context.evaluateHandle(pageFunction, ...args);
}

/**
* @param {string} contextIdentifier
* @returns {!Promise.<!Puppeteer.ExecutionContext>}
*/
async createNewIsolatedContext(contextIdentifier) {
const mainFrame = this.mainFrame();
const isolatedWorldInfo = await this._client.send(
'Page.createIsolatedWorld',
{frameId: mainFrame._id, worldName: contextIdentifier}
);
const executionContextId = isolatedWorldInfo.executionContextId;
const JsHandleFactory = this._frameManager.createJSHandle.bind(this._frameManager, executionContextId);
const executionContextDescription = {
id: executionContextId,
origin: 'new-isolated-world',
name: contextIdentifier
};
return new ExecutionContext(this._client, executionContextDescription, JsHandleFactory, mainFrame);
}

/**
* @param {!Puppeteer.JSHandle} prototypeHandle
* @return {!Promise<!Puppeteer.JSHandle>}
Expand Down
40 changes: 40 additions & 0 deletions test/page.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,46 @@ module.exports.addTests = function({testRunner, expect, puppeteer, DeviceDescrip
});
});

describe('Page.createNewIsolatedContext', function() {
it('should work', async({page, server}) => {
await page.evaluate(function() {
window.constantVariable = new Proxy({}, {
get: function() {
throw new Error('Should not get variable');
},
set: function() {
throw new Error('Should not set variable');
}
});
const p = document.createElement('p');
p.id = 'test-paragraph';
p.appendChild(document.createTextNode('Test node'));
document.body.appendChild(p);
});
await page.evaluate(function() {
return window.constantVariable;
}).then(function() {
throw new Error('Test should fail');
}, function(err) {
expect(err.message).toContain('Should not get variable');
});

const isolatedContext = await page.createNewIsolatedContext('test-isolated');

// Check that isolated context is indeed in a different context
const value = await isolatedContext.evaluate(function() {
return window.constantVariable;
});
expect(value).toBe(undefined);
// Nevertheless, it shares the DOM
const testText = await isolatedContext.evaluate(function() {
const testP = document.getElementById('test-paragraph');
return testP.textContent;
});
expect(testText).toBe('Test node');
});
});

describe('ExecutionContext.queryObjects', function() {
it('should work', async({page, server}) => {
// Instantiate an object
Expand Down

0 comments on commit e632cae

Please sign in to comment.