<commit_before>require 'selenium/webdriver'

$browser_configs = JSON.load(open("browsers.json"))

if ENV['TEST_LOCAL'] == 'true'
  # This drives a local installation of ChromeDriver running on port 9515, instead of BrowserStack.
  browser = Selenium::WebDriver.for :chrome, :url=>"http://127.0.0.1:9515"

  if ENV['MAXIMIZE_LOCAL'] == 'true'
    max_width, max_height = browser.execute_script("return [window.screen.availWidth, window.screen.availHeight];")
    browser.manage.window.resize_to(max_width, max_height)
  end
else
  if CDO.saucelabs_username.blank?
    raise "Please define CDO.saucelabs_username"
  end

  if CDO.saucelabs_authkey.blank?
    raise "Please define CDO.saucelabs_authkey"
  end

  url = "http://#{CDO.saucelabs_username}:#{CDO.saucelabs_authkey}@ondemand.saucelabs.com:80/wd/hub"

  capabilities = Selenium::WebDriver::Remote::Capabilities.new
  browser_config = $browser_configs.detect {|b| b['name'] == ENV['BROWSER_CONFIG'] }

  browser_config.each do |key, value|
    capabilities[key] = value
  end

  capabilities[:javascript_enabled] = 'true'
  capabilities[:name] = ENV['TEST_RUN_NAME']
  capabilities[:build] = ENV['BUILD']

  puts "Capabilities: #{capabilities.inspect}"

  Time.now.to_i.tap do |start_time|
    browser = Selenium::WebDriver.for(:remote,
                                      url: url,
                                      desired_capabilities: capabilities,
                                      http_client: Selenium::WebDriver::Remote::Http::Default.new.tap{|c| c.timeout = 5.minutes}) # iOS takes more time
    puts "Got browser in #{Time.now.to_i - start_time}s"
  end

  puts "Browser: #{browser}"

  # Maximize the window on desktop, as some tests require 1280px width.
  unless ENV['MOBILE']
    max_width, max_height = browser.execute_script("return [window.screen.availWidth, window.screen.availHeight];")
    browser.manage.window.resize_to(max_width, max_height)
  end
end

# let's allow much longer timeouts when searching for an element
browser.manage.timeouts.implicit_wait = 2.minutes
browser.send(:bridge).setScriptTimeout(1.minute * 1000)

Before do
  @browser = browser
  @browser.manage.delete_all_cookies

  @sauce_session_id = @browser.send(:bridge).capabilities["webdriver.remote.sessionid"]
  puts 'visual log on sauce labs: https://saucelabs.com/tests/' + @sauce_session_id
end<commit_msg>Fix running UI tests locally<commit_after>require 'selenium/webdriver'

$browser_configs = JSON.load(open("browsers.json"))

if ENV['TEST_LOCAL'] == 'true'
  # This drives a local installation of ChromeDriver running on port 9515, instead of BrowserStack.
  browser = Selenium::WebDriver.for :chrome, :url=>"http://127.0.0.1:9515"

  if ENV['MAXIMIZE_LOCAL'] == 'true'
    max_width, max_height = browser.execute_script("return [window.screen.availWidth, window.screen.availHeight];")
    browser.manage.window.resize_to(max_width, max_height)
  end
else
  if CDO.saucelabs_username.blank?
    raise "Please define CDO.saucelabs_username"
  end

  if CDO.saucelabs_authkey.blank?
    raise "Please define CDO.saucelabs_authkey"
  end

  url = "http://#{CDO.saucelabs_username}:#{CDO.saucelabs_authkey}@ondemand.saucelabs.com:80/wd/hub"

  capabilities = Selenium::WebDriver::Remote::Capabilities.new
  browser_config = $browser_configs.detect {|b| b['name'] == ENV['BROWSER_CONFIG'] }

  browser_config.each do |key, value|
    capabilities[key] = value
  end

  capabilities[:javascript_enabled] = 'true'
  capabilities[:name] = ENV['TEST_RUN_NAME']
  capabilities[:build] = ENV['BUILD']

  puts "Capabilities: #{capabilities.inspect}"

  Time.now.to_i.tap do |start_time|
    browser = Selenium::WebDriver.for(:remote,
                                      url: url,
                                      desired_capabilities: capabilities,
                                      http_client: Selenium::WebDriver::Remote::Http::Default.new.tap{|c| c.timeout = 5.minutes}) # iOS takes more time
    puts "Got browser in #{Time.now.to_i - start_time}s"
  end

  puts "Browser: #{browser}"

  # Maximize the window on desktop, as some tests require 1280px width.
  unless ENV['MOBILE']
    max_width, max_height = browser.execute_script("return [window.screen.availWidth, window.screen.availHeight];")
    browser.manage.window.resize_to(max_width, max_height)
  end
end

# let's allow much longer timeouts when searching for an element
browser.manage.timeouts.implicit_wait = 2.minutes
browser.send(:bridge).setScriptTimeout(1.minute * 1000)

Before do
  @browser = browser
  @browser.manage.delete_all_cookies

  unless ENV['TEST_LOCAL'] == 'true'
    @sauce_session_id = @browser.send(:bridge).capabilities["webdriver.remote.sessionid"]
    puts 'visual log on sauce labs: https://saucelabs.com/tests/' + @sauce_session_id
  end
end
{'fraction_non_alphanumeric': 0.12323064113238967, 'fraction_numerical': 0.011240632805995004, 'mean_word_length': 3.974120082815735, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5280668', 'n_tokens_mistral': 1656, 'n_tokens_neox': 1573, 'n_words': 317}