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:

1
$ gem install selenium-webdriver

Write sample code to sample_scrpit.rb:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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 = Selenium::WebDriver::Wait.new(timeout: 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
driver.quit

Execute the sample code:

1
$ 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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# 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)
elem.sendKeys("/path/to/file")

Driver Operation Examples

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# Save screen shot
driver.save_screenshot('screenshot.png')
# Wait for a specific element to show up
wait = Selenium::WebDriver::Wait.new(:timeout => 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
end
# Delete Cookie
driver.manage.delete_cookie("cookie_name")

Special Thanks