Ruby selenium-webdriver Cheat Sheat

“Selenium WebDriver” is a library to control browser by Java/Ruby/JavaScript code.
This article is a cheat sheet for Ruby “selenium-webdriver

😼 Get Started

Install Rubygem selenium-webdriver:

$ gem install selenium-webdriver

Write sample code to sample_scrpit.rb:

require "selenium-webdriver"
# start an instance of firefox with selenium-webdriver
driver = Selenium::WebDriver.for :firefox # generate browser object
driver.get "http://localhost:3000"
# wait for a specific element to show up
wait = 10)
wait.until { /Sample Title/.match(browser.page_source) }
driver.find_element(:id, 'hoge').send_keys('fuga')
driver.find_element(:id, 'goto_next').click
puts "Test Passed: Page 1 Validated"
# Drop browser object

Execute the sample code:

$ ruby sample_script.rb

🐰 Locating Element Examples

  • find_element - Find the first element matching the given arguments
  • find_elements - Find all elements matching the given arguments
# Find the element by ID
element = driver.find_element(:id, "sample-id")
# Find the element by class name
elements = driver.find_element(:class, 'sample-class')
# Find the element by tag name
element = driver.find_element(:tag_name, 'div')
# Find the element by name
element = driver.find_element(:name, 'search')
# Find the element by link text
element = driver.find_element(:link, 'link text')
# Find the element by xpath
element = driver.find_element(:xpath, "//a[@href='/logout']")

🐡 Element Operation Examples

# Click element
driver.find_element(:id, 'button_id').click
# Input some text
driver.find_element(:id, 'TextArea').send_keys 'InputText'
# Select checkbox/radio
driver.find_element(:id, 'check_box_id').click
# Check if it is selected
driver.find_element(:id, 'check_box_id').selected?
# Deselect the element
driver.find_element(:id, 'check_box_id').clear
# get all the options for this element
driver.find_element(:id, "select_id").find_elements(:tag_name, "option")
# Select the options
select.find_elements(:tag_name, "option")(&:click)
# Get text
driver.find_element(:id, 'some_element_id').text
# Get an attribute
class_name = element.attribute("class")
# Upload file
elem = driver.execute_script(js, elem)

🐹 Driver Operation Examples

# Save screen shot
# Wait for a specific element to show up
wait = => 10) # seconds
wait.until { driver.find_element(:id => "foo") }
# WebDriver polls the DOM for a certain amount of time when trying to find an element
# or elements if they are not immediately available for 30 seconds
driver.manage.timeouts.implicit_wait = 30
element = driver.find_element(:id => "some-dynamic-element")
# Execute arbitrary javascript
puts driver.execute_script("return window.location.pathname")
# Handle JavaScript dialog
a = driver.switch_to.alert
a.text == 'Aleart Message' ? a.dismiss : a.accept
# Switch window
driver.window_handles.each do |handle|
driver.switch_to.window handle
# Delete Cookie

🏈 Special Thanks

🖥 Recommended VPS Service

VULTR provides high performance cloud compute environment for you. Vultr has 15 data-centers strategically placed around the globe, you can use a VPS with 512 MB memory for just $ 2.5 / month ($ 0.004 / hour). In addition, Vultr is up to 4 times faster than the competition, so please check it => Check Benchmark Results!!