<commit_before>// Licensed to the Software Freedom Conservancy (SFC) under one
// or more contributor license agreements.  See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership.  The SFC licenses this file
// to you 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.

import webdriver from 'browser-webdriver'
import { absolutifyUrl } from './utils'
import { Logger, Channels } from '../../stores/view/Logs'
import PlaybackState from '../../stores/view/PlaybackState'

const By = webdriver.By
const until = webdriver.until
const Key = webdriver.Key

const IMPLICIT_WAIT = 30 * 1000
const DEFAULT_CAPABILITIES = {
  browserName: 'chrome',
}
const DEFAULT_SERVER = 'http://localhost:4444/wd/hub'

export default class WebDriverExecutor {
  constructor(capabilities, server) {
    this.capabilities = capabilities || DEFAULT_CAPABILITIES
    this.server = server || DEFAULT_SERVER
    this.logger = new Logger(Channels.PLAYBACK)
  }

  async init({ baseUrl, variables }) {
    this.baseUrl = baseUrl
    this.variables = variables
    this.driver = await new webdriver.Builder()
      .withCapabilities(this.capabilities)
      .usingServer(this.server)
      .build()
  }

  async cleanup() {
    await this.driver.quit()
  }

  isWebDriverCommand() {
    return true
  }

  isAlive() {
    // webdriver will throw for us, but not necessarily for all commands
    // TODO: check if there are commands that will succeed if we always return true
    return true
  }

  name(command) {
    if (!command) {
      return 'skip'
    }

    const upperCase = command.charAt(0).toUpperCase() + command.slice(1)
    const func = ('do' + upperCase).replace('Verify', 'Assert')
    if (!this[func]) {
      throw new Error(`Unknown command ${command}`)
    }
    return func
  }

  // Commands go after this line

  // TODO
  // doAssertConfirmation
  // doAssertEditable
  // doAssertNotEditable
  // doAssertPrompt
  // doVerify
  // doVerifyChecked
  // doVerifyNotChecked
  // doVerifyEditable
  // doVerifyNotEditable
  // doVerifyElementPresent
  // doVerifyElementNotPresent
  // doVerifySelectedValue
  // doVerifyNotSelectedValue
  // doVerifyText
  // doVerifyNotText
  // doVerifyTitle
  // doVerifyValue
  // doVerifySelectedLabel
  // doDoubleClickAt
  // doDragAndDropToObject
  // doEditContent
  // doMouseMoveAt<commit_msg>add before and after command to the webdriver executor<commit_after>// Licensed to the Software Freedom Conservancy (SFC) under one
// or more contributor license agreements.  See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership.  The SFC licenses this file
// to you 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.

import webdriver from 'browser-webdriver'
import { absolutifyUrl } from './utils'
import { Logger, Channels } from '../../stores/view/Logs'
import PlaybackState from '../../stores/view/PlaybackState'

const By = webdriver.By
const until = webdriver.until
const Key = webdriver.Key

const IMPLICIT_WAIT = 30 * 1000
const DEFAULT_CAPABILITIES = {
  browserName: 'chrome',
}
const DEFAULT_SERVER = 'http://localhost:4444/wd/hub'

export default class WebDriverExecutor {
  constructor(capabilities, server) {
    this.capabilities = capabilities || DEFAULT_CAPABILITIES
    this.server = server || DEFAULT_SERVER
    this.logger = new Logger(Channels.PLAYBACK)
  }

  async init({ baseUrl, variables }) {
    this.baseUrl = baseUrl
    this.variables = variables
    this.driver = await new webdriver.Builder()
      .withCapabilities(this.capabilities)
      .usingServer(this.server)
      .build()
  }

  async cleanup() {
    await this.driver.quit()
  }

  isWebDriverCommand() {
    return true
  }

  isAlive() {
    // webdriver will throw for us, but not necessarily for all commands
    // TODO: check if there are commands that will succeed if we always return true
    return true
  }

  name(command) {
    if (!command) {
      return 'skip'
    }

    const upperCase = command.charAt(0).toUpperCase() + command.slice(1)
    const func = ('do' + upperCase).replace('Verify', 'Assert')
    if (!this[func]) {
      throw new Error(`Unknown command ${command}`)
    }
    return func
  }

  async beforeCommand(_commandObject) {}

  async afterCommand(_commandObject) {}

  // Commands go after this line

  // TODO
  // doAssertConfirmation
  // doAssertEditable
  // doAssertNotEditable
  // doAssertPrompt
  // doVerify
  // doVerifyChecked
  // doVerifyNotChecked
  // doVerifyEditable
  // doVerifyNotEditable
  // doVerifyElementPresent
  // doVerifyElementNotPresent
  // doVerifySelectedValue
  // doVerifyNotSelectedValue
  // doVerifyText
  // doVerifyNotText
  // doVerifyTitle
  // doVerifyValue
  // doVerifySelectedLabel
  // doDoubleClickAt
  // doDragAndDropToObject
  // doEditContent
  // doMouseMoveAt
{'fraction_non_alphanumeric': 0.09829931972789116, 'fraction_numerical': 0.005442176870748299, 'mean_word_length': 3.2218234027279253, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '6198406', 'n_tokens_mistral': 1673, 'n_tokens_neox': 1674, 'n_words': 543}