test: add another test for browserContext.addInitScript (#2285)

This commit is contained in:
Joel Einbinder 2020-06-18 12:27:48 -07:00 committed by GitHub
parent 63924d9a26
commit b88fabab13
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 4 deletions

View file

@ -145,13 +145,11 @@ export class FFBrowser extends BrowserBase {
export class FFBrowserContext extends BrowserContextBase {
readonly _browser: FFBrowser;
readonly _browserContextId: string | null;
private readonly _evaluateOnNewDocumentSources: string[];
constructor(browser: FFBrowser, browserContextId: string | null, options: BrowserContextOptions) {
super(browser, options);
this._browser = browser;
this._browserContextId = browserContextId;
this._evaluateOnNewDocumentSources = [];
this._authenticateProxyViaHeader();
}
@ -304,7 +302,6 @@ export class FFBrowserContext extends BrowserContextBase {
async addInitScript(script: Function | string | { path?: string, content?: string }, arg?: any) {
const source = await helper.evaluationScript(script, arg);
this._evaluateOnNewDocumentSources.push(source);
await this._browser._connection.send('Browser.addScriptToEvaluateOnNewDocument', { browserContextId: this._browserContextId || undefined, script: source });
}

View file

@ -174,7 +174,7 @@ describe('window.open', function() {
expect(intercepted).toBe(true);
await context.close();
});
it('should apply addInitScript from browser context', async function({browser, server}) {
it('BrowserContext.addInitScript should apply to an in-process popup', async function({browser, server}) {
const context = await browser.newContext();
await context.addInitScript(() => window.injected = 123);
const page = await context.newPage();
@ -186,6 +186,21 @@ describe('window.open', function() {
await context.close();
expect(injected).toBe(123);
});
it('BrowserContext.addInitScript should apply to a cross-process popup', async function({browser, server}) {
const context = await browser.newContext();
await context.addInitScript(() => window.injected = 123);
const page = await context.newPage();
await page.goto(server.EMPTY_PAGE);
const [popup] = await Promise.all([
page.waitForEvent('popup'),
page.evaluate(url => window.open(url), server.CROSS_PROCESS_PREFIX + '/title.html'),
]);
expect(await popup.evaluate('injected')).toBe(123);
await popup.reload();
expect(await popup.evaluate('injected')).toBe(123);
await context.close();
});
it('should expose function from browser context', async function({browser, server}) {
const context = await browser.newContext();
await context.exposeFunction('add', (a, b) => a + b);