<commit_before>/*
  Copyright 2016 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.
*/

'use strict';

const execSync = require('child_process').execSync;
const fs = require('fs');
const chalk = require('chalk');
const webdriver = require('selenium-webdriver');

/**
 * <p>A base class that is designed to be extended to handle browser specific
 * values.</p>
 *
 * <p>An instance of this class helps find and start browsers using selenium.</p>
 *
 * <p>Instances of this class are returned by
 * [automatedBrowserTesting.getDiscoverableBrowsers()]{@link AutomatedBrowserTesting#getDiscoverableBrowsers}</p>
 */
class WebDriverBrowser {
  /**
   * <p>This constructor will throw an error should any of the inputs be
   * invalid / unexpected.</p>
   *
   * @param  {String} prettyName        A user friendly name of the browser
   * @param  {String} release           Release type of browser (can be either
   * 'stable', 'beta' or 'unstable')
   * @param  {String} seleniumBrowserId An id of the browser that will be
   * accepted by selenium (either 'chrome' or 'firefox')
   * @param  {SeleniumOptions} seleniumOptions   This is an instance of either
   * `selenium-webdriver/firefox` or `selenium-webdriver/chrome`
   */
  constructor(prettyName, release, seleniumBrowserId, seleniumOptions) {
    if (typeof prettyName !== 'string' || prettyName.length === 0) {
      throw new Error('Invalid prettyName value: ', prettyName);
    }

    if (release !== 'stable' && release !== 'beta' && release !== 'unstable') {
      throw new Error('Unexpected browser release given: ', release);
    }

    if (
      seleniumBrowserId !== 'chrome' &&
      seleniumBrowserId !== 'firefox' &&
      seleniumBrowserId !== 'opera' &&
      seleniumBrowserId !== 'safari'
    ) {
      throw new Error('Unexpected browser ID given: ', seleniumBrowserId);
    }

    this._prettyName = prettyName;
    this._release = release;
    this._seleniumBrowserId = seleniumBrowserId;
    this._seleniumOptions = seleniumOptions;
  }

  /* eslint-disable valid-jsdoc */
  /**
   * To get the path of the browsers executable file, call this method.
   * @return {String} Path of the browsers executable file or null if
   * it can't be found.
   */
  getExecutablePath() {
    throw new Error('getExecutablePath() must be overriden by subclasses');
  }
  /* eslint-enable valid-jsdoc */

  /**
   * If you need to identify a browser based on it's version number but
   * the high level version number isn't specific enough, you can use the
   * raw version string (this will be the result of calling the browser
   * executable with an appropriate flag to get the version)
   * @return {String} Raw string that identifies the browser
   */
  getRawVersionString() {
    const executablePath = this.getExecutablePath();
    if (!executablePath) {
      return null;
    }

    try {
      return execSync(`"${executablePath}" --version`)
        .toString();
    } catch (err) {
      console.warn(chalk.red('WARNING') + ': Unable to get a version string ' +
        'for ' + this.getPrettyName());
    }

    return null;
  }

  /* eslint-disable valid-jsdoc */
  /**
   * <p>This method returns an integer if it can be determined from
   * the browser executable or -1 if the version is unknown.</p>
   *
   * <p>A scenario where it will be unable to produce a valid version
   * is if the browsers executable path can't be found.</p>
   *
   * @return {Integer} Version number if it can be found
   */
  getVersionNumber() {
    throw new Error('getVersionNumber() must be overriden by subclasses');
  }
  /* eslint-enable valid-jsdoc */

  _getMinSupportedVersion() {
    return false;
  }<commit_msg>May help with perf of isValid<commit_after>/*
  Copyright 2016 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.
*/

'use strict';

const execSync = require('child_process').execSync;
const fs = require('fs');
const chalk = require('chalk');
const webdriver = require('selenium-webdriver');

/**
 * <p>A base class that is designed to be extended to handle browser specific
 * values.</p>
 *
 * <p>An instance of this class helps find and start browsers using selenium.</p>
 *
 * <p>Instances of this class are returned by
 * [automatedBrowserTesting.getDiscoverableBrowsers()]{@link AutomatedBrowserTesting#getDiscoverableBrowsers}</p>
 */
class WebDriverBrowser {
  /**
   * <p>This constructor will throw an error should any of the inputs be
   * invalid / unexpected.</p>
   *
   * @param  {String} prettyName        A user friendly name of the browser
   * @param  {String} release           Release type of browser (can be either
   * 'stable', 'beta' or 'unstable')
   * @param  {String} seleniumBrowserId An id of the browser that will be
   * accepted by selenium (either 'chrome' or 'firefox')
   * @param  {SeleniumOptions} seleniumOptions   This is an instance of either
   * `selenium-webdriver/firefox` or `selenium-webdriver/chrome`
   */
  constructor(prettyName, release, seleniumBrowserId, seleniumOptions) {
    if (typeof prettyName !== 'string' || prettyName.length === 0) {
      throw new Error('Invalid prettyName value: ', prettyName);
    }

    if (release !== 'stable' && release !== 'beta' && release !== 'unstable') {
      throw new Error('Unexpected browser release given: ', release);
    }

    if (
      seleniumBrowserId !== 'chrome' &&
      seleniumBrowserId !== 'firefox' &&
      seleniumBrowserId !== 'opera' &&
      seleniumBrowserId !== 'safari'
    ) {
      throw new Error('Unexpected browser ID given: ', seleniumBrowserId);
    }

    this._prettyName = prettyName;
    this._release = release;
    this._seleniumBrowserId = seleniumBrowserId;
    this._seleniumOptions = seleniumOptions;
  }

  /* eslint-disable valid-jsdoc */
  /**
   * To get the path of the browsers executable file, call this method.
   * @return {String} Path of the browsers executable file or null if
   * it can't be found.
   */
  getExecutablePath() {
    throw new Error('getExecutablePath() must be overriden by subclasses');
  }
  /* eslint-enable valid-jsdoc */

  /**
   * If you need to identify a browser based on it's version number but
   * the high level version number isn't specific enough, you can use the
   * raw version string (this will be the result of calling the browser
   * executable with an appropriate flag to get the version)
   * @return {String} Raw string that identifies the browser
   */
  getRawVersionString() {
    if (this._rawVerstionString) {
      console.log('HERE <------------------------');
      return this._rawVerstionString;
    }

    const executablePath = this.getExecutablePath();
    if (!executablePath) {
      return null;
    }

    this._rawVerstionString = null;

    try {
      this._rawVerstionString = execSync(`"${executablePath}" --version`)
        .toString();
    } catch (err) {
      console.warn(chalk.red('WARNING') + ': Unable to get a version string ' +
        'for ' + this.getPrettyName());
    }

    return this._rawVerstionString;
  }

  /* eslint-disable valid-jsdoc */
  /**
   * <p>This method returns an integer if it can be determined from
   * the browser executable or -1 if the version is unknown.</p>
   *
   * <p>A scenario where it will be unable to produce a valid version
   * is if the browsers executable path can't be found.</p>
   *
   * @return {Integer} Version number if it can be found
   */
  getVersionNumber() {
    throw new Error('getVersionNumber() must be overriden by subclasses');
  }
  /* eslint-enable valid-jsdoc */

  _getMinSupportedVersion() {
    return false;
  }
{'fraction_non_alphanumeric': 0.10596945858398889, 'fraction_numerical': 0.0023137436372049976, 'mean_word_length': 3.233594515181195, 'pattern_counts': {'":': 0, '<': 28, '<?xml version=': 0, '>': 27, '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': '4659215', 'n_tokens_mistral': 2511, 'n_tokens_neox': 2375, 'n_words': 904}