Selenium WebDriver – Cheat Sheet
September 5, 2011
Getting Started
Load the Selenium Webdriver library
require 'selenium-webdriver'
Open a browser (Ex: Internet Explorer)
driver = Selenium::WebDriver.for :ie
Go to a specified URL
driver.navigate.to 'http://www.orbitz.com/'
driver.get 'http://www.orbitz.com/'
Close the browser
driver.close
Access an Element
Type somethign in the Text box or text area
driver.find_element(:id,'airOrigin').send_keys("MAA")
To Clear the text from text field
driver.find_element(:id,'airOrigin').send_keys [:control, 'a'], :space
Button
driver.find_element(:id,'BUTTON_ID'').click
Drop down list
select_box=driver.find_element(:id,'airStartTime')
options=select_box.find_elements(:tag_name=>"option")
options.each do |option_field|
if option_field.text == '12a-9a'
option_field.click
break
end
end
Check box
driver.find_element(:id,'airNonStopsPreferred').click
Radio button
driver.find_element(:id,'htlChoice').click
To verify Flights radio button selected or not
driver.find_element(:id,'airChoice').selected?
#if it returns TRUE then radio button already selected.
Return the title of the document
puts driver.title
Return true if the specified text appears on the TAG
puts driver.find_element(:class,'welcomeText').text.include?("Welcome to Orbitz")
To Click SPAN Elements
options=driver.find_elements(:tag_name=>"span")
options.each do |span_field|
if span_field.text == 'Find Flights'
span_field.click
break
end
end
Selenium WebDriver Basics – Ruby
August 18, 2011
Installation:
1. Install Ruby
2. In Command Prompt >gem install selenium-webdriver
3. Copy and Paste the below code and save the file as sample_raveendran.rb
require ‘rubygems’
require “selenium-webdriver”
driver = Selenium::WebDriver.for :ie
driver.navigate.to “http://google.com”
if driver.title == “Google”
puts “Testcase 1 passed”
else
puts “Testcase 1 failed”
end
q = driver.find_element(:name, ‘q’)
q.send_keys “Raveendran Selenium”
q.submit
sleep 5
if driver.title == “RAVEENDRAN SELENIUM – Google Search”
puts “Testcase 2 passed”
else
puts “Testcase 2 failed”
end
q=driver.page_source
if q.include?(“raveendran.wordpress.com”)
puts “Testcase 3 passed”
else
puts “Testcase 3 failed”
end
driver.navigate.to “http://google.com”
a=driver.find_element(:link,’Advanced search’)
a.click
if driver.title == “Google Advanced Search”
puts “Testcase 4 passed”
else
puts “Testcase 4 failed”
end
a=driver.find_element(:name,’as_q’)
a.send_keys(“Raveendran Selenium”)
a=driver.find_element(:name,’num’)
options=a.find_elements(:tag_name=>”option”)
options.each do |g|
if g.text == “20 results”
g.click
break
end
end
a=driver.find_elements(:xpath=> ‘/html/body/table[2]/tbody/tr/td/table/tbody/tr/td/div
/form/div/table[4]/tbody/tr/td/input’)
a.each do |b|
b.click
end
sleep 4
if driver.title == “RAVEENDRAN SELENIUM – Google Search”
puts “Testcase 5 passed”
else
puts “Testcase 5 failed”
end
4. Run the ruby code
5. The Output looks like
Testcase 1 passed
Testcase 2 passed
Testcase 3 passed
Testcase 4 passed
Testcase 5 passed