|
| 1 | +/** |
| 2 | + * Copyright 2018 Google Inc. All rights reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +const path = require('path'); |
| 18 | +const os = require('os'); |
| 19 | +const fs = require('fs'); |
| 20 | +const util = require('util'); |
| 21 | + |
| 22 | +const rmAsync = util.promisify(require('rimraf')); |
| 23 | +const mkdtempAsync = util.promisify(fs.mkdtemp); |
| 24 | + |
| 25 | +const TMP_FOLDER = path.join(os.tmpdir(), 'pw_tmp_folder-'); |
| 26 | + |
| 27 | +module.exports.describe = function({testRunner, expect, playwright, defaultBrowserOptions, FFOX, CHROMIUM, WEBKIT, WIN}) { |
| 28 | + const {describe, xdescribe, fdescribe} = testRunner; |
| 29 | + const {it, fit, xit, dit} = testRunner; |
| 30 | + const {beforeAll, beforeEach, afterAll, afterEach} = testRunner; |
| 31 | + |
| 32 | + const headfulOptions = Object.assign({}, defaultBrowserOptions, { |
| 33 | + headless: false |
| 34 | + }); |
| 35 | + const headlessOptions = Object.assign({}, defaultBrowserOptions, { |
| 36 | + headless: true |
| 37 | + }); |
| 38 | + |
| 39 | + describe('Headful', function() { |
| 40 | + it('should have default url when launching browser', async function() { |
| 41 | + const browser = await playwright.launch(headfulOptions); |
| 42 | + const pages = (await browser.defaultContext().pages()).map(page => page.url()); |
| 43 | + expect(pages).toEqual(['about:blank']); |
| 44 | + await browser.close(); |
| 45 | + }); |
| 46 | + // see https://github.com/microsoft/playwright/issues/717 |
| 47 | + it.skip((WIN && CHROMIUM) || FFOX)('headless should be able to read cookies written by headful', async({server}) => { |
| 48 | + const userDataDir = await mkdtempAsync(TMP_FOLDER); |
| 49 | + // Write a cookie in headful chrome |
| 50 | + const headfulBrowser = await playwright.launch(Object.assign({userDataDir}, headfulOptions)); |
| 51 | + const headfulPage = await headfulBrowser.defaultContext().newPage(); |
| 52 | + await headfulPage.goto(server.EMPTY_PAGE); |
| 53 | + await headfulPage.evaluate(() => document.cookie = 'foo=true; expires=Fri, 31 Dec 9999 23:59:59 GMT'); |
| 54 | + await headfulBrowser.close(); |
| 55 | + // Read the cookie from headless chrome |
| 56 | + const headlessBrowser = await playwright.launch(Object.assign({userDataDir}, headlessOptions)); |
| 57 | + const headlessPage = await headlessBrowser.defaultContext().newPage(); |
| 58 | + await headlessPage.goto(server.EMPTY_PAGE); |
| 59 | + const cookie = await headlessPage.evaluate(() => document.cookie); |
| 60 | + await headlessBrowser.close(); |
| 61 | + // This might throw. See https://github.com/GoogleChrome/puppeteer/issues/2778 |
| 62 | + await rmAsync(userDataDir).catch(e => {}); |
| 63 | + expect(cookie).toBe('foo=true'); |
| 64 | + }); |
| 65 | + it.skip(FFOX)('should close browser with beforeunload page', async({server}) => { |
| 66 | + const browser = await playwright.launch(headfulOptions); |
| 67 | + const page = await browser.defaultContext().newPage(); |
| 68 | + await page.goto(server.PREFIX + '/beforeunload.html'); |
| 69 | + // We have to interact with a page so that 'beforeunload' handlers |
| 70 | + // fire. |
| 71 | + await page.click('body'); |
| 72 | + await browser.close(); |
| 73 | + }); |
| 74 | + }); |
| 75 | +}; |
0 commit comments