<commit_before>/**
 * Copyright 2017 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 fs = require('fs');
const path = require('path');
const utils = require('./utils');
const {waitEvent} = utils;

module.exports.describe = function({testRunner, expect, headless, playwright, FFOX, CHROME, WEBKIT}) {
  const {describe, xdescribe, fdescribe} = testRunner;
  const {it, fit, xit, dit} = testRunner;
  const {beforeAll, beforeEach, afterAll, afterEach} = testRunner;

  describe('Page.close', function() {
    it('should reject all promises when page is closed', async({context}) => {
      const newPage = await context.newPage();
      let error = null;
      await Promise.all([
        newPage.evaluate(() => new Promise(r => {})).catch(e => error = e),
        newPage.close(),
      ]);
      expect(error.message).toContain('Protocol error');
    });
    it('should not be visible in context.pages', async({context}) => {
      const newPage = await context.newPage();
      expect(await context.pages()).toContain(newPage);
      await newPage.close();
      expect(await context.pages()).not.toContain(newPage);
    });
    it('should run beforeunload if asked for', async({context, server}) => {
      const newPage = await context.newPage();
      await newPage.goto(server.PREFIX + '/beforeunload.html');
      // We have to interact with a page so that 'beforeunload' handlers
      // fire.
      await newPage.click('body');
      const pageClosingPromise = newPage.close({ runBeforeUnload: true });
      const dialog = await waitEvent(newPage, 'dialog');
      expect(dialog.type()).toBe('beforeunload');
      expect(dialog.defaultValue()).toBe('');
      if (CHROME)
        expect(dialog.message()).toBe('');
      else if (WEBKIT)
        expect(dialog.message()).toBe('Leave?');
      else
        expect(dialog.message()).toBe('This page is asking you to confirm that you want to leave - data you have entered may not be saved.');
      await dialog.accept();
      await pageClosingPromise;
    });
    it('should *not* run beforeunload by default', async({context, server}) => {
      const newPage = await context.newPage();
      await newPage.goto(server.PREFIX + '/beforeunload.html');
      // We have to interact with a page so that 'beforeunload' handlers
      // fire.
      await newPage.click('body');
      await newPage.close();
    });
    it('should set the page close state', async({context}) => {
      const newPage = await context.newPage();
      expect(newPage.isClosed()).toBe(false);
      await newPage.close();
      expect(newPage.isClosed()).toBe(true);
    });
    it.skip(FFOX)('should terminate network waiters', async({context, server}) => {
      const newPage = await context.newPage();
      const results = await Promise.all([
        newPage.waitForRequest(server.EMPTY_PAGE).catch(e => e),
        newPage.waitForResponse(server.EMPTY_PAGE).catch(e => e),
        newPage.close()
      ]);
      for (let i = 0; i < 2; i++) {
        const message = results[i].message;
        expect(message).toContain('Target closed');
        expect(message).not.toContain('Timeout');
      }
    });
  });

  describe('Page.Events.Load', function() {
    it('should fire when expected', async({page, server}) => {
      await Promise.all([
        page.goto('about:blank'),
        utils.waitEvent(page, 'load'),
      ]);
    });
  });

  describe('Async stacks', () => {
    it('should work', async({page, server}) => {
      server.setRoute('/empty.html', (req, res) => {
        req.socket.end();
      });
      let error = null;
      await page.goto(server.EMPTY_PAGE).catch(e => error = e);
      expect(error).not.toBe(null);
      expect(error.stack).toContain(__filename);
    });
  });

  describe.skip(FFOX)('Page.Events.error', function() {
    it('should throw when page crashes', async({page}) => {
      let error = null;
      page.on('error', err => error = err);
      if (CHROME)
        page.goto('chrome://crash').catch(e => {});
      else if (WEBKIT)
        page._delegate._session.send('Page.crash', {}).catch(e => {});
      await waitEvent(page, 'error');
      expect(error.message).toBe('Page crashed!');
    });
  });

  describe('Page.Events.Popup', function() {
    it('should work', async({page}) => {
      const [popup] = await Promise.all([
        new Promise(x => page.once('popup', x)),
        page.evaluate(() => window.open('about:blank')),
      ]);
      expect(await page.evaluate(() => !!window.opener)).toBe(false);
      expect(await popup.evaluate(() => !!window.opener)).toBe(true);
    });
    it('should work with noopener', async({page}) => {
      const [popup] = await Promise.all([
        new Promise(x => page.once('popup', x)),
        page.evaluate(() => window.open('about:blank', null, 'noopener')),
      ]);
      expect(await page.evaluate(() => !!window.opener)).toBe(false);
      expect(await popup.evaluate(() => !!window.opener)).toBe(false);
    });
    it('should work with clicking target=_blank', async({page, server}) => {
      await page.goto(server.EMPTY_PAGE);
      await page.setContent('<a target=_blank rel="opener" href="/one-style.html">yo</a>');
      const [popup] = await Promise.all([
        new Promise(x => page.once('popup', x)),
        page.click('a'),
      ]);
      expect(await page.evaluate(() => !!window.opener)).toBe(false);
      expect(await popup.evaluate(() => !!window.opener)).toBe(true);
    });
    it.skip(FFOX)('should work with fake-clicking target=_blank and rel=noopener', async({page, server}) => {
      // TODO: FFOX sends events for "one-style.html" request to both pages.
      await page.goto(server.EMPTY_PAGE);
      await page.setContent('<a target=_blank rel=noopener href="/one-style.html">yo</a>');
      const [popup] = await Promise.all([
        page.waitForEvent('popup'),
        page.$eval('a', a => a.click()),
      ]);
      expect(await page.evaluate(() => !!window.opener)).toBe(false);
      // TODO: At this point popup might still have about:blank as the current document.
      // FFOX is slow enough to trigger this. We should do something about popups api.
      expect(await popup.evaluate(() => !!window.opener)).toBe(false);
    });
    it('should work with clicking target=_blank and rel=noopener', async({page, server}) => {
      await page.goto(server.EMPTY_PAGE);
      await page.setContent('<a target=_blank rel=noopener href="/one-style.html">yo</a>');
      const [popup] = await Promise.all([
        new Promise(x => page.once('popup', x)),
        page.click('a'),
      ]);
      expect(await page.evaluate(() => !!window.opener)).toBe(false);
      expect(await popup.evaluate(() => !!window.opener)).toBe(false);
    });
    // Crashes with Target closed on WebKit / Linux.
    it('should not treat navigations as new popups', async({page, server}) => {
      await page.goto(server.EMPTY_PAGE);
      await page.setContent('<a target=_blank rel=noopener href="/one-style.html">yo</a>');
      const [popup] = await Promise.all([
        page.waitForEvent('popup').then(popup => { popup.waitForLoadState(); return popup; }),
        page.click('a'),
      ]);
      let badSecondPopup = false;
      page.on('popup', () => badSecondPopup = true);
      await popup.goto(server.CROSS_PROCESS_PREFIX + '/empty.html');<commit_msg>fix: await the popup nav to make the test pass (#391)<commit_after>/**
 * Copyright 2017 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 fs = require('fs');
const path = require('path');
const utils = require('./utils');
const {waitEvent} = utils;

module.exports.describe = function({testRunner, expect, headless, playwright, FFOX, CHROME, WEBKIT}) {
  const {describe, xdescribe, fdescribe} = testRunner;
  const {it, fit, xit, dit} = testRunner;
  const {beforeAll, beforeEach, afterAll, afterEach} = testRunner;

  describe('Page.close', function() {
    it('should reject all promises when page is closed', async({context}) => {
      const newPage = await context.newPage();
      let error = null;
      await Promise.all([
        newPage.evaluate(() => new Promise(r => {})).catch(e => error = e),
        newPage.close(),
      ]);
      expect(error.message).toContain('Protocol error');
    });
    it('should not be visible in context.pages', async({context}) => {
      const newPage = await context.newPage();
      expect(await context.pages()).toContain(newPage);
      await newPage.close();
      expect(await context.pages()).not.toContain(newPage);
    });
    it('should run beforeunload if asked for', async({context, server}) => {
      const newPage = await context.newPage();
      await newPage.goto(server.PREFIX + '/beforeunload.html');
      // We have to interact with a page so that 'beforeunload' handlers
      // fire.
      await newPage.click('body');
      const pageClosingPromise = newPage.close({ runBeforeUnload: true });
      const dialog = await waitEvent(newPage, 'dialog');
      expect(dialog.type()).toBe('beforeunload');
      expect(dialog.defaultValue()).toBe('');
      if (CHROME)
        expect(dialog.message()).toBe('');
      else if (WEBKIT)
        expect(dialog.message()).toBe('Leave?');
      else
        expect(dialog.message()).toBe('This page is asking you to confirm that you want to leave - data you have entered may not be saved.');
      await dialog.accept();
      await pageClosingPromise;
    });
    it('should *not* run beforeunload by default', async({context, server}) => {
      const newPage = await context.newPage();
      await newPage.goto(server.PREFIX + '/beforeunload.html');
      // We have to interact with a page so that 'beforeunload' handlers
      // fire.
      await newPage.click('body');
      await newPage.close();
    });
    it('should set the page close state', async({context}) => {
      const newPage = await context.newPage();
      expect(newPage.isClosed()).toBe(false);
      await newPage.close();
      expect(newPage.isClosed()).toBe(true);
    });
    it.skip(FFOX)('should terminate network waiters', async({context, server}) => {
      const newPage = await context.newPage();
      const results = await Promise.all([
        newPage.waitForRequest(server.EMPTY_PAGE).catch(e => e),
        newPage.waitForResponse(server.EMPTY_PAGE).catch(e => e),
        newPage.close()
      ]);
      for (let i = 0; i < 2; i++) {
        const message = results[i].message;
        expect(message).toContain('Target closed');
        expect(message).not.toContain('Timeout');
      }
    });
  });

  describe('Page.Events.Load', function() {
    it('should fire when expected', async({page, server}) => {
      await Promise.all([
        page.goto('about:blank'),
        utils.waitEvent(page, 'load'),
      ]);
    });
  });

  describe('Async stacks', () => {
    it('should work', async({page, server}) => {
      server.setRoute('/empty.html', (req, res) => {
        req.socket.end();
      });
      let error = null;
      await page.goto(server.EMPTY_PAGE).catch(e => error = e);
      expect(error).not.toBe(null);
      expect(error.stack).toContain(__filename);
    });
  });

  describe.skip(FFOX)('Page.Events.error', function() {
    it('should throw when page crashes', async({page}) => {
      let error = null;
      page.on('error', err => error = err);
      if (CHROME)
        page.goto('chrome://crash').catch(e => {});
      else if (WEBKIT)
        page._delegate._session.send('Page.crash', {}).catch(e => {});
      await waitEvent(page, 'error');
      expect(error.message).toBe('Page crashed!');
    });
  });

  describe('Page.Events.Popup', function() {
    it('should work', async({page}) => {
      const [popup] = await Promise.all([
        new Promise(x => page.once('popup', x)),
        page.evaluate(() => window.open('about:blank')),
      ]);
      expect(await page.evaluate(() => !!window.opener)).toBe(false);
      expect(await popup.evaluate(() => !!window.opener)).toBe(true);
    });
    it('should work with noopener', async({page}) => {
      const [popup] = await Promise.all([
        new Promise(x => page.once('popup', x)),
        page.evaluate(() => window.open('about:blank', null, 'noopener')),
      ]);
      expect(await page.evaluate(() => !!window.opener)).toBe(false);
      expect(await popup.evaluate(() => !!window.opener)).toBe(false);
    });
    it('should work with clicking target=_blank', async({page, server}) => {
      await page.goto(server.EMPTY_PAGE);
      await page.setContent('<a target=_blank rel="opener" href="/one-style.html">yo</a>');
      const [popup] = await Promise.all([
        new Promise(x => page.once('popup', x)),
        page.click('a'),
      ]);
      expect(await page.evaluate(() => !!window.opener)).toBe(false);
      expect(await popup.evaluate(() => !!window.opener)).toBe(true);
    });
    it.skip(FFOX)('should work with fake-clicking target=_blank and rel=noopener', async({page, server}) => {
      // TODO: FFOX sends events for "one-style.html" request to both pages.
      await page.goto(server.EMPTY_PAGE);
      await page.setContent('<a target=_blank rel=noopener href="/one-style.html">yo</a>');
      const [popup] = await Promise.all([
        page.waitForEvent('popup'),
        page.$eval('a', a => a.click()),
      ]);
      expect(await page.evaluate(() => !!window.opener)).toBe(false);
      // TODO: At this point popup might still have about:blank as the current document.
      // FFOX is slow enough to trigger this. We should do something about popups api.
      expect(await popup.evaluate(() => !!window.opener)).toBe(false);
    });
    it('should work with clicking target=_blank and rel=noopener', async({page, server}) => {
      await page.goto(server.EMPTY_PAGE);
      await page.setContent('<a target=_blank rel=noopener href="/one-style.html">yo</a>');
      const [popup] = await Promise.all([
        new Promise(x => page.once('popup', x)),
        page.click('a'),
      ]);
      expect(await page.evaluate(() => !!window.opener)).toBe(false);
      expect(await popup.evaluate(() => !!window.opener)).toBe(false);
    });
    // Crashes with Target closed on WebKit / Linux.
    it('should not treat navigations as new popups', async({page, server}) => {
      await page.goto(server.EMPTY_PAGE);
      await page.setContent('<a target=_blank rel=noopener href="/one-style.html">yo</a>');
      const [popup] = await Promise.all([
        page.waitForEvent('popup').then(async popup => { await popup.waitForLoadState(); return popup; }),
        page.click('a'),
      ]);
      let badSecondPopup = false;
      page.on('popup', () => badSecondPopup = true);
      await popup.goto(server.CROSS_PROCESS_PREFIX + '/empty.html');
{'fraction_non_alphanumeric': 0.16733567486201706, 'fraction_numerical': 0.001442548921224285, 'mean_word_length': 3.149102263856362, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 109, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1954008', 'n_tokens_mistral': 4689, 'n_tokens_neox': 4678, 'n_words': 1205}