How do I use selenium with Ruby?
I figured it out after a few hours of searching on forums and through google.
What I needed to do was have the selenium server running for it to work. I was able to download it from this site http://seleniumhq.org/download/ (current: Selenium RC February 23, 2010 1.0.3).
From there I opened up a new terminal and did
cd Downloads/selenium-remote-control-1.0.3/selenium-server-1.0.3java -jar selenium-server.jar
Then ran my ruby generated script with another terminal window
ruby google_test.rb
And it worked!
This is Selenium Webdriver example for simple google searchSave as google_search.rb
require "selenium-webdriver"require "test/unit"class GoogleSearch < Test::Unit::TestCase def setup @driver = Selenium::WebDriver.for :firefox @base_url = "http://www.google.com/" @driver.manage.timeouts.implicit_wait = 30 @verification_errors = [] end def teardown @driver.quit assert_equal [], @verification_errors end def test_google_search @driver.get(@base_url) @driver.find_element(:name, "q").clear @driver.find_element(:name, "q").send_keys "Thiyagarajan Veluchamy" @driver.find_element(:name, "btnK").click end def element_present?(how, what) @driver.find_element(how, what) true rescue Selenium::WebDriver::Error::NoSuchElementError false end def verify(&blk) yield rescue Test::Unit::AssertionFailedError => ex @verification_errors << ex endend
$ruby google_search.rb
Here is a much simpler version of the script:
require "selenium-webdriver"@driver = Selenium::WebDriver.for :chrome@base_url = "http://www.google.com/"@driver.get(@base_url)@driver.find_element(:name, "q").send_keys "Stack Overflow"
Methods available on the @driver object can be found here: http://selenium.googlecode.com/svn/trunk/docs/api/rb/Selenium/WebDriver/Driver.html
find_element gives you access to the Element class. Methods available on the Element class can be found here:http://selenium.googlecode.com/svn/trunk/docs/api/rb/Selenium/WebDriver/Element.html