<commit_before>/**
 * Copyright 2017 Google Inc. All rights reserved.
 *
 * 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 rm = require('rimraf').sync;
const path = require('path');
const {helper} = require('../lib/helper');
if (process.env.COVERAGE)
  helper.recordPublicAPICoverage();
console.log('Testing on Node', process.version);
const puppeteer = require('..');
const SimpleServer = require('./server/SimpleServer');
const GoldenUtils = require('./golden-utils');

const YELLOW_COLOR = '\x1b[33m';
const RESET_COLOR = '\x1b[0m';

const GOLDEN_DIR = path.join(__dirname, 'golden');
const OUTPUT_DIR = path.join(__dirname, 'output');

const PORT = 8907;
const PREFIX = 'http://localhost:' + PORT;
const CROSS_PROCESS_PREFIX = 'http://127.0.0.1:' + PORT;
const EMPTY_PAGE = PREFIX + '/empty.html';
const HTTPS_PORT = 8908;
const HTTPS_PREFIX = 'https://localhost:' + HTTPS_PORT;

const headless = (process.env.HEADLESS || 'true').trim().toLowerCase() === 'true';
const slowMo = parseInt((process.env.SLOW_MO || '0').trim(), 10);
const executablePath = process.env.CHROME;
if (executablePath)
  console.warn(`${YELLOW_COLOR}WARN: running tests with ${executablePath}${RESET_COLOR}`);

const defaultBrowserOptions = {
  executablePath,
  slowMo,
  headless,
  args: ['--no-sandbox']
};

if (process.env.DEBUG_TEST || slowMo)
  jasmine.DEFAULT_TIMEOUT_INTERVAL = 1000 * 1000 * 1000;
else
  jasmine.DEFAULT_TIMEOUT_INTERVAL = 10 * 1000;

// Make sure the `npm install` was run after the chromium roll.
{
  const Downloader = require('../utils/ChromiumDownloader');
  const chromiumRevision = require('../package.json').puppeteer.chromium_revision;
  const revisionInfo = Downloader.revisionInfo(Downloader.currentPlatform(), chromiumRevision);
  console.assert(revisionInfo.downloaded, `Chromium r${chromiumRevision} is not downloaded. Run 'npm install' and try to re-run tests.`);
}

let server;
let httpsServer;
beforeAll(SX(async function() {
  const assetsPath = path.join(__dirname, 'assets');
  server = await SimpleServer.create(assetsPath, PORT);
  httpsServer = await SimpleServer.createHTTPS(assetsPath, HTTPS_PORT);
  if (fs.existsSync(OUTPUT_DIR))
    rm(OUTPUT_DIR);
}));

afterAll(SX(async function() {
  await Promise.all([
    server.stop(),
    httpsServer.stop(),
  ]);
}));

describe('Puppeteer', function() {
  describe('Puppeteer.launch', function() {
    it('should support ignoreHTTPSErrors option', SX(async function() {
      const options = Object.assign({ignoreHTTPSErrors: true}, defaultBrowserOptions);
      const browser = await puppeteer.launch(options);
      const page = await browser.newPage();
      let error = null;
      const response = await page.goto(HTTPS_PREFIX + '/empty.html').catch(e => error = e);
      expect(error).toBe(null);
      expect(response.ok).toBe(true);
      browser.close();
    }));
    it('should reject all promises when browser is closed', SX(async function() {
      const browser = await puppeteer.launch(defaultBrowserOptions);
      const page = await browser.newPage();
      let error = null;
      const neverResolves = page.evaluate(() => new Promise(r => {})).catch(e => error = e);
      browser.close();
      await neverResolves;
      expect(error.message).toContain('Protocol error');
    }));
    it('userDataDir option', SX(async function() {
      const userDataDir = fs.mkdtempSync(path.join(__dirname, 'test-user-data-dir'));
      const options = Object.assign({userDataDir}, defaultBrowserOptions);
      const browser = await puppeteer.launch(options);
      expect(fs.readdirSync(userDataDir).length).toBeGreaterThan(0);
      await browser.close();
      expect(fs.readdirSync(userDataDir).length).toBeGreaterThan(0);
      rm(userDataDir);
    }));
    it('userDataDir argument', SX(async function() {
      const userDataDir = fs.mkdtempSync(path.join(__dirname, 'test-user-data-dir'));
      const options = Object.assign({}, defaultBrowserOptions);
      options.args = [`--user-data-dir=${userDataDir}`].concat(options.args);
      const browser = await puppeteer.launch(options);
      expect(fs.readdirSync(userDataDir).length).toBeGreaterThan(0);
      await browser.close();
      expect(fs.readdirSync(userDataDir).length).toBeGreaterThan(0);
      rm(userDataDir);
    }));
  });<commit_msg>test: add failing test for storing data in custom userDataDir<commit_after>/**
 * Copyright 2017 Google Inc. All rights reserved.
 *
 * 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 rm = require('rimraf').sync;
const path = require('path');
const {helper} = require('../lib/helper');
if (process.env.COVERAGE)
  helper.recordPublicAPICoverage();
console.log('Testing on Node', process.version);
const puppeteer = require('..');
const SimpleServer = require('./server/SimpleServer');
const GoldenUtils = require('./golden-utils');

const YELLOW_COLOR = '\x1b[33m';
const RESET_COLOR = '\x1b[0m';

const GOLDEN_DIR = path.join(__dirname, 'golden');
const OUTPUT_DIR = path.join(__dirname, 'output');

const PORT = 8907;
const PREFIX = 'http://localhost:' + PORT;
const CROSS_PROCESS_PREFIX = 'http://127.0.0.1:' + PORT;
const EMPTY_PAGE = PREFIX + '/empty.html';
const HTTPS_PORT = 8908;
const HTTPS_PREFIX = 'https://localhost:' + HTTPS_PORT;

const headless = (process.env.HEADLESS || 'true').trim().toLowerCase() === 'true';
const slowMo = parseInt((process.env.SLOW_MO || '0').trim(), 10);
const executablePath = process.env.CHROME;
if (executablePath)
  console.warn(`${YELLOW_COLOR}WARN: running tests with ${executablePath}${RESET_COLOR}`);

const defaultBrowserOptions = {
  executablePath,
  slowMo,
  headless,
  args: ['--no-sandbox']
};

if (process.env.DEBUG_TEST || slowMo)
  jasmine.DEFAULT_TIMEOUT_INTERVAL = 1000 * 1000 * 1000;
else
  jasmine.DEFAULT_TIMEOUT_INTERVAL = 10 * 1000;

// Make sure the `npm install` was run after the chromium roll.
{
  const Downloader = require('../utils/ChromiumDownloader');
  const chromiumRevision = require('../package.json').puppeteer.chromium_revision;
  const revisionInfo = Downloader.revisionInfo(Downloader.currentPlatform(), chromiumRevision);
  console.assert(revisionInfo.downloaded, `Chromium r${chromiumRevision} is not downloaded. Run 'npm install' and try to re-run tests.`);
}

let server;
let httpsServer;
beforeAll(SX(async function() {
  const assetsPath = path.join(__dirname, 'assets');
  server = await SimpleServer.create(assetsPath, PORT);
  httpsServer = await SimpleServer.createHTTPS(assetsPath, HTTPS_PORT);
  if (fs.existsSync(OUTPUT_DIR))
    rm(OUTPUT_DIR);
}));

afterAll(SX(async function() {
  await Promise.all([
    server.stop(),
    httpsServer.stop(),
  ]);
}));

describe('Puppeteer', function() {
  describe('Puppeteer.launch', function() {
    it('should support ignoreHTTPSErrors option', SX(async function() {
      const options = Object.assign({ignoreHTTPSErrors: true}, defaultBrowserOptions);
      const browser = await puppeteer.launch(options);
      const page = await browser.newPage();
      let error = null;
      const response = await page.goto(HTTPS_PREFIX + '/empty.html').catch(e => error = e);
      expect(error).toBe(null);
      expect(response.ok).toBe(true);
      browser.close();
    }));
    it('should reject all promises when browser is closed', SX(async function() {
      const browser = await puppeteer.launch(defaultBrowserOptions);
      const page = await browser.newPage();
      let error = null;
      const neverResolves = page.evaluate(() => new Promise(r => {})).catch(e => error = e);
      browser.close();
      await neverResolves;
      expect(error.message).toContain('Protocol error');
    }));
    it('userDataDir option', SX(async function() {
      const userDataDir = fs.mkdtempSync(path.join(__dirname, 'test-user-data-dir'));
      const options = Object.assign({userDataDir}, defaultBrowserOptions);
      const browser = await puppeteer.launch(options);
      expect(fs.readdirSync(userDataDir).length).toBeGreaterThan(0);
      await browser.close();
      expect(fs.readdirSync(userDataDir).length).toBeGreaterThan(0);
      rm(userDataDir);
    }));
    it('userDataDir argument', SX(async function() {
      const userDataDir = fs.mkdtempSync(path.join(__dirname, 'test-user-data-dir'));
      const options = Object.assign({}, defaultBrowserOptions);
      options.args = [`--user-data-dir=${userDataDir}`].concat(options.args);
      const browser = await puppeteer.launch(options);
      expect(fs.readdirSync(userDataDir).length).toBeGreaterThan(0);
      await browser.close();
      expect(fs.readdirSync(userDataDir).length).toBeGreaterThan(0);
      rm(userDataDir);
    }));
    // Headless has issues shutting down gracefully
    // @see https://crbug.com/771830
    (headless ? xit : it)('userDataDir option should restore state', SX(async function() {
      const userDataDir = fs.mkdtempSync(path.join(__dirname, 'test-user-data-dir'));
      const options = Object.assign({userDataDir}, defaultBrowserOptions);
      const browser = await puppeteer.launch(options);
      const page = await browser.newPage();
      await page.goto(EMPTY_PAGE);
      await page.evaluate(() => localStorage.hey = 'hello');
      await browser.close();

      const browser2 = await puppeteer.launch(options);
      const page2 = await browser2.newPage();
      await page2.goto(EMPTY_PAGE);
      expect(await page2.evaluate(() => localStorage.hey)).toBe('hello');
      await browser2.close();
      rm(userDataDir);
    }));
  });
{'fraction_non_alphanumeric': 0.14126112478697217, 'fraction_numerical': 0.010982768415072904, 'mean_word_length': 4.265702891326022, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, 'https://': 3, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1297939', 'n_tokens_mistral': 3258, 'n_tokens_neox': 3166, 'n_words': 794}