Selenium-webdriver

Selenium WebDriver Basics – Ruby

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

Advertisements

11 thoughts on “Selenium WebDriver Basics – Ruby

  1. Hi Raveendran,

    Thanks for this post. Do you have any suggestion on how to check for an element based on more than 1 attributes eg based on id and class?

    a=driver.find_element(:link,’Advanced search’ || :id, ‘advSearch’)

    Thanks

    1. Hi Anil,

      My Question is –> Why you want to mention 2 attributes to identify the single element ? You can use anyone of the unique attribute to automate the field.

  2. Thanks Raveendran. Actually There are variety of attributes that are either common or unique but there no one attribute that is unique … I used xpath for now … It seems to work. Thanks

  3. HI Raveendran, Iam new to selenium, and I want to learn more about webdriver with ruby, Iam not a Automation tester but interested so is there a way you can guide me.

  4. Hi
    I want to learn selenium-web driver with ruby language , can u pls suggest me how could find resource for this

  5. Hello;
    Running your code and DOS window opens/closes very fast. IE browser never opens.
    Any idea why? Other RUBY/Selenium code runs fine, so I don’t think it’s the environment. Thanks

  6. I am using Selenium webdriver with TestNG and i have created a small framework using this. But I am not aware that whether it would help me in working witj selenium in any company or not bcuause I do not know much about it.
    I am stuck in quikr.com site where I am unable to identify an object. I have created an add in quikr and then whie editing that add, there is a customized listbox which I am not able to identify.
    Customized list box means:-it has main categry like Mobile/OS etc and after selecting the Category, it displays subcategories for that in the same customized list box. I am able to select the category but not able to Identify subcategory list box.
    Kindly help me here if you can.

    Also please let me know if you can provide me support on automating a webapplication and that site would be any site like quikr or any other site.
    I want your help because I want a job in Selenium very urgently and tehre are lot of things which I have not used I have not worked in selenium framework.
    I will pay you for this. Just help me please….

    thanks

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s