cheat sheet · selenium · selenium-webdriver · Selenium-webdriver

Selenium WebDriver – Cheat Sheet

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

Handling Select Box using selenium-webdriver rubygem

Handling Select Box using selenium-webdriver rubygem

Assuming that select field –> name is ‘num’ and it has options –>  “10 results” and “20 results”

User need to select “20 results”  from selct box

Code:

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