Skip to content

Instantly share code, notes, and snippets.

@yemartin
Created November 2, 2016 01:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yemartin/893f3edbfcff8e514e7aeb7ee769cabe to your computer and use it in GitHub Desktop.
Save yemartin/893f3edbfcff8e514e7aeb7ee769cabe to your computer and use it in GitHub Desktop.
Quick and dirty use of Capybara against a live site, for automation or smoke tests.
#!/usr/bin/env ruby
OPEN_TIME = "07:45"
TARGET_SITE = "http://example.com/"
PATIENT_NUMBER = '1111111'
require 'time'
require 'capybara'
require 'selenium-webdriver'
Capybara.run_server = false
Capybara.default_driver = :selenium
Selenium::WebDriver.for :firefox
Selenium::WebDriver::Driver.class_eval do
# Not sure why I did that ^^;
def quit
STDOUT.puts "#{self.class}#quit: no-op"
end
end
# Wait for the opening time.
Capybara.visit TARGET_SITE
sleep_time = Time.parse(OPEN_TIME) - Time.now
sleep sleep_time if sleep_time > 0
# Retry a few times in case the server time is skewed
# and the site is not open yet.
attempts = 10
begin
Capybara.visit TARGET_SITE
Capybara.click_on 'いますぐ受付'
rescue
attempts -= 1
if attempts > 0
puts "RETRYING. Attempts left: #{attempts}"
retry
end
raise "UNABLE TO CONNECT, giving up."
end
# Now perform the main automated task
Capybara.click_on '次へ'
Capybara.fill_in 'patient_number', with: PATIENT_NUMBER
Capybara.click_on '次へ'
Capybara.click_on 'はい'
Capybara.click_on '次へ'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment