# Ruby + Selenium-webdriver

**URL:** <https://rubytalk.org/t/ruby-selenium-webdriver/66774>\
**Category:** ruby-talk\
**Created:** [26 November 2012 18:51 UTC](https://rubytalk.org/t/ruby-selenium-webdriver/66774 "2012-11-26T18:51:25Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![7stud2](https://avatars.discourse-cdn.com/v4/letter/7/9de053/32.png) [@7stud2](https://rubytalk.org/u/7stud2)\
**Post date:** [26 November 2012 18:51 UTC](https://rubytalk.org/t/ruby-selenium-webdriver/66774/1 "2012-11-26T18:51:25Z")

</div>

Hi,

I am a total beginner of Ruby and Selenium and i want to use both =)

1. Selenium-webdriver gem - How do I find out which methods that this  
gem contains or other gems?  
2. Catch a bug – If a link or element is missing on the page how do I  
catch this defect and output it and then continue with my test script?  
Some design tips?

Best Regards,  
Mattias

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![7stud2](https://avatars.discourse-cdn.com/v4/letter/7/9de053/32.png) [@7stud2](https://rubytalk.org/u/7stud2)\
**Post date:** [26 November 2012 21:19 UTC](https://rubytalk.org/t/ruby-selenium-webdriver/66774/2 "2012-11-26T21:19:44Z")

</div>

I recommend using Watir-Webdriver, as this is Selenium with a great  
interface.

Check out tips and examples here:  
[http://watirwebdriver.com/](http://watirwebdriver.com/)

Plenty of methods are shown there and in the documentation. As for  
catching missing elements, you have methods such as "present?" returning  
a boolean value.

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![7stud2](https://avatars.discourse-cdn.com/v4/letter/7/9de053/32.png) [@7stud2](https://rubytalk.org/u/7stud2)\
**Post date:** [27 November 2012 13:42 UTC](https://rubytalk.org/t/ruby-selenium-webdriver/66774/3 "2012-11-27T13:42:24Z")

</div>

Try this:

require 'watir-webdriver'  
b = Watir::Browser.start '[bit.ly/watir-webdriver-demo](http://bit.ly/watir-webdriver-demo)'  
l = b.link :text =\> 'Google Docs'  
b.goto l.href

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![7stud2](https://avatars.discourse-cdn.com/v4/letter/7/9de053/32.png) [@7stud2](https://rubytalk.org/u/7stud2)\
**Post date:** [27 November 2012 16:57 UTC](https://rubytalk.org/t/ruby-selenium-webdriver/66774/4 "2012-11-27T16:57:05Z")

</div>

Joel Pearson wrote in post #1086551:

> I recommend using Watir-Webdriver, as this is Selenium with a great  
> interface.
> 
> Check out tips and examples here:  
> [http://watirwebdriver.com/](http://watirwebdriver.com/)
> 
> Plenty of methods are shown there and in the documentation. As for  
> catching missing elements, you have methods such as "present?" returning  
> a boolean value.

Thank you Joel I will check this out.

Best Regards  
Mattias

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![7stud2](https://avatars.discourse-cdn.com/v4/letter/7/9de053/32.png) [@7stud2](https://rubytalk.org/u/7stud2)\
**Post date:** [27 November 2012 21:06 UTC](https://rubytalk.org/t/ruby-selenium-webdriver/66774/5 "2012-11-27T21:06:32Z")

</div>

Mattias A. wrote in post #1086723:

> Joel Pearson wrote in post #1086551:
> 
> > I recommend using Watir-Webdriver, as this is Selenium with a great  
> > interface.
> > 
> > Check out tips and examples here:  
> > [http://watirwebdriver.com/](http://watirwebdriver.com/)
> > 
> > Plenty of methods are shown there and in the documentation. As for  
> > catching missing elements, you have methods such as "present?" returning  
> > a boolean value.
> 
> Thank you Joel I will check this out.
> 
> Best Regards  
> Mattias

Hi again,

What actually happens below? See my comments after #  
Would really appreciate if someone wanted to take the time to explain.

require 'rubygems' #lib?  
require 'selenium-webdriver' #lib?

driver = Selenium::WebDriver.for :firefox #instantiate driver with  
Selenium and Webdriver methods?  
driver.get "[http://google.com](http://google.com)"

element = driver.find\_element :name =\> "q" # What happens here? :name =\>  
"q"?  
element.send\_keys "Cheese!"  
element.submit

puts "Page title is #{driver.title}"

wait = Selenium::WebDriver::Wait.new(:timeout =\> 2)#Wait is a new class?  
wait.until { driver.title.downcase.start\_with? "cheese!" }#Verify that  
cheese has been printed?

puts "Page title is #{driver.title}"  
driver.quit

Best Regards  
Mattias

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![7stud2](https://avatars.discourse-cdn.com/v4/letter/7/9de053/32.png) [@7stud2](https://rubytalk.org/u/7stud2)\
**Post date:** [28 November 2012 08:56 UTC](https://rubytalk.org/t/ruby-selenium-webdriver/66774/6 "2012-11-28T08:56:08Z")

</div>

> What actually happens below? See my comments after #  
> Would really appreciate if someone wanted to take the time to explain.
> 
> require 'rubygems' #lib?

You don't need this in Ruby 1.9

> require 'selenium-webdriver' #lib?

Have a look at this:  
[http://watirmelon.com/2011/05/05/selenium-webdriver-vs-watir-webdriver-in-ruby/](http://watirmelon.com/2011/05/05/selenium-webdriver-vs-watir-webdriver-in-ruby/)

> driver = Selenium::WebDriver.for :firefox #instantiate driver with  
> Selenium and Webdriver methods?

This just creates an instance of firefox with a temp profile which can  
be controlled via Selenium-webdriver

> driver.get "[http://google.com](http://google.com)"
> 
> element = driver.find\_element :name =\> "q" # What happens here? :name =\>  
> "q"?

The element on the page with the name "q" happens to be the search text  
field. I recommend using Firebug to find out what each element can be  
identified by.

> element.send\_keys "Cheese!"  
> element.submit
> 
> puts "Page title is #{driver.title}"
> 
> wait = Selenium::WebDriver::Wait.new(:timeout =\> 2)#Wait is a new class?

Watir-webdriver's latest version has waiting built-in, it looks like  
Selenium has a seperate wait module.

> wait.until { driver.title.downcase.start\_with? "cheese!" }#Verify that  
> cheese has been printed?

You're passing a block to the wait module, until method which will stop  
waiting once it evaluates to "true". This waits until the page has  
loaded to a point that it has changed the title of the browser window.

> puts "Page title is #{driver.title}"  
> driver.quit

I'm not sure about Selenium, but with watir-webdriver using "close"  
rather than "quit" cleans up the temp files as well.

> Best Regards  
> Mattias

This is what the same sequence looks like with watir-webdriver:

require 'watir-webdriver'  
driver = Watir::Browser.new :firefox  
driver.goto "[http://google.com](http://google.com)"  
element = driver.text\_field(:name =\> "q")  
element.set "Cheese!\n"  
puts "Page title is #{driver.title}"  
driver.wait\_until { driver.title.downcase.start\_with? "cheese!" }  
puts "Page title is #{driver.title}"  
driver.close

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).
