<commit_before>require 'rubygems'
require 'watir-webdriver'
require 'trollop'

opts = Trollop::options do
  opt :pos, "Browser window position <x,y>", :type => :string
  opt :size, "Browser window size <x,y>", :type => :string
end

# check if osxautomation or xaut is installed
`osxautomation`
osxautomation_installed = $?.success?
`python check_xaut.py`
xaut_installed = $?.success?

Dir["automators/*.rb"].each {|file| require file }
if osxautomation_installed
  @automator = OsxautomationAutomator.new
elsif xaut_installed
  @automator = XautAutomator.new
else
  @automator = BaseAutomator.new
end


@browser = Watir::Browser.new :chrome
@browser.driver.manage.timeouts.implicit_wait = 10

if opts[:pos]
  begin
    pos = opts[:pos].split(',')
    @browser.driver.manage.window.move_to(pos[0].to_i, pos[1].to_i)
  rescue Exception => e
    puts "Invalid position string #{opts[:pos]}"
  end
end

if opts[:size]
  begin
    size = opts[:size].split(',')
    @browser.driver.manage.window.resize_to(size[0].to_i, size[1].to_i)
  rescue Exception => e
    puts "Invalid size string #{opts[:size]}"
  end
end

# calibrate

@browser.execute_script <<-JS
  document.write('Showtime! Click into window to begin.');
  document.onclick = function(e){
    window.activated = true;
    window.cursorX = e.pageX;
    window.cursorY = e.pageY;
    document.write('<br>Go!');
  }
JS

# auto-calibrate when possible
if opts[:pos] and opts[:size]
  begin
    size = opts[:size].split(',')
    pos = opts[:pos].split(',')
    centre_x = pos[0].to_i + size[0].to_i / 2.0
    centre_y = pos[1].to_i + size[1].to_i / 2.0
    @automator.mouse_move(centre_x, centre_y)

    (1..5).each do
      sleep 0.5
      @automator.mouse_click
    end
  rescue Exception => e
    puts "Invalid position or size string"
  end
end




activated = false
while !activated
  activated = @browser.execute_script <<-JS
    return window.activated === true;
  JS
  sleep 0.1
end


posBrowser = @browser.execute_script <<-JS
  return [window.cursorX, window.cursorY];
JS
posGlobal = @automator.mouse_location
@offsets = {
  :x => posGlobal[0].to_i - posBrowser[0].to_i,
  :y => posGlobal[1].to_i - posBrowser[1].to_i
}


Dir["journeys/*.rb"].each {|file| require file }

journeys = [
  LicensingJourney.new(@browser, @automator, @offsets),
  TransactionsExplorerJourney.new(@browser, @automator, @offsets),
  PayLegalisationPostJourney.new(@browser, @automator, @offsets)
]



while true
  journeys.each do |journey|
    journey.run
  end
end

<commit_msg>Quit browser when error occurs<commit_after>require 'rubygems'
require 'watir-webdriver'
require 'trollop'

opts = Trollop::options do
  opt :pos, "Browser window position <x,y>", :type => :string
  opt :size, "Browser window size <x,y>", :type => :string
end

# check if osxautomation or xaut is installed
`osxautomation`
osxautomation_installed = $?.success?
`python check_xaut.py`
xaut_installed = $?.success?

Dir["automators/*.rb"].each {|file| require file }
if osxautomation_installed
  @automator = OsxautomationAutomator.new
elsif xaut_installed
  @automator = XautAutomator.new
else
  @automator = BaseAutomator.new
end


@browser = Watir::Browser.new :chrome
@browser.driver.manage.timeouts.implicit_wait = 10

if opts[:pos]
  begin
    pos = opts[:pos].split(',')
    @browser.driver.manage.window.move_to(pos[0].to_i, pos[1].to_i)
  rescue Exception => e
    puts "Invalid position string #{opts[:pos]}"
  end
end

if opts[:size]
  begin
    size = opts[:size].split(',')
    @browser.driver.manage.window.resize_to(size[0].to_i, size[1].to_i)
  rescue Exception => e
    puts "Invalid size string #{opts[:size]}"
  end
end

# calibrate

@browser.execute_script <<-JS
  document.write('Showtime! Click into window to begin.');
  document.onclick = function(e){
    window.activated = true;
    window.cursorX = e.pageX;
    window.cursorY = e.pageY;
    document.write('<br>Go!');
  }
JS

# auto-calibrate when possible
if opts[:pos] and opts[:size]
  begin
    size = opts[:size].split(',')
    pos = opts[:pos].split(',')
    centre_x = pos[0].to_i + size[0].to_i / 2.0
    centre_y = pos[1].to_i + size[1].to_i / 2.0
    @automator.mouse_move(centre_x, centre_y)

    (1..5).each do
      sleep 0.5
      @automator.mouse_click
    end
  rescue Exception => e
    puts "Invalid position or size string"
  end
end




activated = false
while !activated
  activated = @browser.execute_script <<-JS
    return window.activated === true;
  JS
  sleep 0.1
end


posBrowser = @browser.execute_script <<-JS
  return [window.cursorX, window.cursorY];
JS
posGlobal = @automator.mouse_location
@offsets = {
  :x => posGlobal[0].to_i - posBrowser[0].to_i,
  :y => posGlobal[1].to_i - posBrowser[1].to_i
}


Dir["journeys/*.rb"].each {|file| require file }

journeys = [
  LicensingJourney.new(@browser, @automator, @offsets),
  TransactionsExplorerJourney.new(@browser, @automator, @offsets),
  PayLegalisationPostJourney.new(@browser, @automator, @offsets)
]



begin
  while true
    journeys.each do |journey|
      journey.run
    end
  end
ensure
  @browser.quit
end


{'fraction_non_alphanumeric': 0.14324960753532182, 'fraction_numerical': 0.009419152276295133, 'mean_word_length': 3.938953488372093, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '45707', 'n_tokens_mistral': 1883, 'n_tokens_neox': 1840, 'n_words': 418}