/** * Copyright 2018 Google Inc. All rights reserved. * Modifications copyright (c) Microsoft Corporation. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ const utils = require('./utils'); module.exports.describe = function({testRunner, expect, playwright, CHROMIUM, WEBKIT}) { const {describe, xdescribe, fdescribe} = testRunner; const {it, fit, xit, dit} = testRunner; const {beforeAll, beforeEach, afterAll, afterEach} = testRunner; describe('BrowserContext', function() { it('should create new context', async function({browser, newContext}) { expect(browser.browserContexts().length).toBe(0); const context = await newContext(); expect(browser.browserContexts().length).toBe(1); expect(browser.browserContexts().indexOf(context) !== -1).toBe(true); await context.close(); expect(browser.browserContexts().length).toBe(0); }); it.skip(CHROMIUM)('popup should inherit user agent', async function({newContext, server}) { const context = await newContext({ userAgent: 'hey' }); const page = await context.newPage(); await page.goto(server.EMPTY_PAGE); const evaluatePromise = page.evaluate(url => window.open(url), server.PREFIX + '/dummy.html'); const popupPromise = page.waitForEvent('popup'); const request = await server.waitForRequest('/dummy.html'); await evaluatePromise; await popupPromise; expect(request.headers['user-agent']).toBe('hey'); }); it('window.open should use parent tab context', async function({newContext, server}) { const context = await newContext(); const page = await context.newPage(); await page.goto(server.EMPTY_PAGE); const [popupTarget] = await Promise.all([ utils.waitEvent(page, 'popup'), page.evaluate(url => window.open(url), server.EMPTY_PAGE) ]); expect(popupTarget.browserContext()).toBe(context); }); it('should isolate localStorage and cookies', async function({browser, newContext, server}) { // Create two incognito contexts. const context1 = await newContext(); const context2 = await newContext(); expect((await context1.pages()).length).toBe(0); expect((await context2.pages()).length).toBe(0); // Create a page in first incognito context. const page1 = await context1.newPage(); await page1.goto(server.EMPTY_PAGE); await page1.evaluate(() => { localStorage.setItem('name', 'page1'); document.cookie = 'name=page1'; }); expect((await context1.pages()).length).toBe(1); expect((await context2.pages()).length).toBe(0); // Create a page in second incognito context. const page2 = await context2.newPage(); await page2.goto(server.EMPTY_PAGE); await page2.evaluate(() => { localStorage.setItem('name', 'page2'); document.cookie = 'name=page2'; }); expect((await context1.pages()).length).toBe(1); expect((await context2.pages()).length).toBe(1); expect((await context1.pages())[0]).toBe(page1); expect((await context2.pages())[0]).toBe(page2); // Make sure pages don't share localstorage or cookies. expect(await page1.evaluate(() => localStorage.getItem('name'))).toBe('page1'); expect(await page1.evaluate(() => document.cookie)).toBe('name=page1'); expect(await page2.evaluate(() => localStorage.getItem('name'))).toBe('page2'); expect(await page2.evaluate(() => document.cookie)).toBe('name=page2'); // Cleanup contexts. await Promise.all([ context1.close(), context2.close() ]); expect(browser.browserContexts().length).toBe(0); }); it('should propagate default viewport to the page', async({ newPage }) => { const page = await newPage({ viewport: { width: 456, height: 789 } }); expect(page.viewport().width).toBe(456); expect(page.viewport().height).toBe(789); expect(await page.evaluate('window.innerWidth')).toBe(456); expect(await page.evaluate('window.innerHeight')).toBe(789); }); it('should take fullPage screenshots when default viewport is null', async({server, newPage}) => { const page = await newPage({ viewport: null }); await page.goto(server.PREFIX + '/grid.html'); const sizeBefore = await page.evaluate(() => ({ width: document.body.offsetWidth, height: document.body.offsetHeight })); const screenshot = await page.screenshot({ fullPage: true }); expect(screenshot).toBeInstanceOf(Buffer); const sizeAfter = await page.evaluate(() => ({ width: document.body.offsetWidth, height: document.body.offsetHeight })); expect(sizeBefore.width).toBe(sizeAfter.width); expect(sizeBefore.height).toBe(sizeAfter.height); }); it('should restore default viewport after fullPage screenshot', async({ newPage }) => { const page = await newPage({ viewport: { width: 456, height: 789 } }); expect(page.viewport().width).toBe(456); expect(page.viewport().height).toBe(789); expect(await page.evaluate('window.innerWidth')).toBe(456); expect(await page.evaluate('window.innerHeight')).toBe(789); const screenshot = await page.screenshot({ fullPage: true }); expect(screenshot).toBeInstanceOf(Buffer); expect(page.viewport().width).toBe(456); expect(page.viewport().height).toBe(789); expect(await page.evaluate('window.innerWidth')).toBe(456); expect(await page.evaluate('window.innerHeight')).toBe(789); }); it('should make a copy of default viewport', async({ newContext }) => { const viewport = { width: 456, height: 789 }; const context = await newContext({ viewport }); viewport.width = 567; const page = await context.newPage(); expect(page.viewport().width).toBe(456); expect(page.viewport().height).toBe(789); expect(await page.evaluate('window.innerWidth')).toBe(456); expect(await page.evaluate('window.innerHeight')).toBe(789); }); it('should take element screenshot when default viewport is null and restore back', async({server, newPage}) => { const page = await newPage({ viewport: null }); await page.setContent(`