# Using selenium in a ruby program

**URL:** <https://www.ruby-forum.com/t/using-selenium-in-a-ruby-program/171320>\
**Category:** Ruby\
**Created:** [June 30, 2009, 4:54pm UTC](https://www.ruby-forum.com/t/using-selenium-in-a-ruby-program/171320 "2009-06-30T16:54:14Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![kga85](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/90ced4/32.png) [@kga85](https://www.ruby-forum.com/u/kga85)\
**Post date:** [June 30, 2009, 4:54pm UTC](https://www.ruby-forum.com/t/using-selenium-in-a-ruby-program/171320/1 "2009-06-30T16:54:14Z")

</div>

Is it posible to make a ruby file that runs test that uses selenium.

This code is from the selenium ide (Only somthing don in a hurry):

require “selenium”  
require “test/unit”

class Untitled \< Test::Unit::TestCase  
def setup  
@verification\_errors = []  
if $selenium  
@selenium = $selenium  
else  
@selenium = Selenium::SeleniumDriver.new(“localhost”, 4444,  
“\*chrome”, “[http://change-this-to-the-site-you-are-testing/](http://change-this-to-the-site-you-are-testing/)”, 10000);  
@selenium.start  
end  
@selenium.set\_context(“test\_untitled”)  
end

def teardown  
@selenium.stop unless $selenium  
assert\_equal [], @verification\_errors  
end

def test\_untitled  
@selenium.open “/”  
@selenium.click “link=Documentation”  
@selenium.wait\_for\_page\_to\_load “30000”  
@selenium.click “//div[@id=‘selenium-documentation’]/ul[1]/li[1]/a”  
@selenium.wait\_for\_page\_to\_load “30000”  
assert @selenium.is\_text\_present(“welcome”)  
end  
end

Would it be posible to runn this code inside eclipse, or from the  
command line?

I have tried:  
gem install selenium-client  
gem install Selenium  
gem install selenium-rails

I have also tryed to download selenium RC and placed it in my app  
folder. And made a selenium.rb that point in to it. Somthing like this  
[http://siannopollo.blogspot.com/2007/02/selenium-and-ruby-they-actually-work.html](http://siannopollo.blogspot.com/2007/02/selenium-and-ruby-they-actually-work.html)  
But I am notetierly sure if I did this corectly.

If it is posible, do anyone have a clear and easy description of how to  
get it to work. And including a example file with requier files and all?

---

<div class="post-metadata">

**Author:** ![kga85](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/90ced4/32.png) [@kga85](https://www.ruby-forum.com/u/kga85)\
**Post date:** [July 1, 2009, 1:43pm UTC](https://www.ruby-forum.com/t/using-selenium-in-a-ruby-program/171320/2 "2009-07-01T13:43:54Z")

</div>

I got it to work in ruby, but I have to start the selenium servver  
manualy before I runn the program. That is unesesary mutch work and  
complicated. Is there a way to start the selenium server inside the code  
and stop it at the end?

Here is a code example i got to work… it is a bit messy

```
require 'rubygems'

```

gem ‘thoughtbot-shoulda’  
require ‘shoulda’

require ‘spec’  
require ‘spec/story’

require ‘test/unit’  
require “rubygems”  
gem “selenium-client”, “\>=1.2.15”  
require “selenium/client”  
require “selenium”

```
describe "test" do

  before :all do
    @browser = Selenium::Client::Driver.new \
      :host => "localhost",
      :port => 4444,
      :browser => "*firefox",
      :url => "http://www.google.com",
      :timeout_in_second => 60
  end
  after :all do
    @browser.close_current_browser_session
  end

  it "test1" do
    @browser.start_new_browser_session
    @browser.open "/"
    @browser.type "q", "Selenium seleniumhq.org"
    @browser.click "btnG", :wait_for => :page
    assert @browser.text?("seleniumhq.org")
  end
end

```

What do I need to add to get it to start the selenium server automaticly  
and stop it at the end?

---

<div class="post-metadata">

**Author:** ![kga85](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/90ced4/32.png) [@kga85](https://www.ruby-forum.com/u/kga85)\
**Post date:** [July 1, 2009, 3:04pm UTC](https://www.ruby-forum.com/t/using-selenium-in-a-ruby-program/171320/3 "2009-07-01T15:04:30Z")

</div>

On Wed, Jul 1, 2009 at 12:43 PM, Kga A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ## require ‘rubygems’ require “selenium/client” :url =\> “[http://www.google.com](http://www.google.com)”, @browser.type “q”, “Selenium [seleniumhq.org](http://seleniumhq.org)”
> 
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

You might want to take a look at Webrat if you haven’t already [0] You  
can  
fairly easily switch between a simulated browser (if you don’t need to  
test  
javascript) or run in automated mode i.e. selenium.

[0] [gitrdoc.com](http://gitrdoc.com/brynary/webrat/tree/master/)

Rgds,  
Ben

---

<div class="post-metadata">

**Author:** ![kga85](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/90ced4/32.png) [@kga85](https://www.ruby-forum.com/u/kga85)\
**Post date:** [July 1, 2009, 3:19pm UTC](https://www.ruby-forum.com/t/using-selenium-in-a-ruby-program/171320/4 "2009-07-01T15:19:00Z")

</div>

Ben L. wrote:

> On Wed, Jul 1, 2009 at 12:43 PM, Kga A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > ## require ‘rubygems’ require “selenium/client” :url =\> “[http://www.google.com](http://www.google.com)”, @browser.type “q”, “Selenium [seleniumhq.org](http://seleniumhq.org)”
> > 
> > Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> You might want to take a look at Webrat if you haven’t already [0] You  
> can  
> fairly easily switch between a simulated browser (if you don’t need to  
> test  
> javascript) or run in automated mode i.e. selenium.
> 
> [0] [gitrdoc.com](http://gitrdoc.com/brynary/webrat/tree/master/)
> 
> Rgds,  
> Ben

I can take a look, but I have to test some sites using javascript. I  
have to test a bunch of server side programs, and the way to start them  
is to click on stuff that are generated by javascript (or somthing like  
that).

---

<div class="post-metadata">

**Author:** ![kga85](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/90ced4/32.png) [@kga85](https://www.ruby-forum.com/u/kga85)\
**Post date:** [July 1, 2009, 3:27pm UTC](https://www.ruby-forum.com/t/using-selenium-in-a-ruby-program/171320/5 "2009-07-01T15:27:17Z")

</div>

On Wed, Jul 1, 2009 at 2:19 PM, Kga A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > >
> 
> ## I can take a look, but I have to test some sites using javascript. I have to test a bunch of server side programs, and the way to start them is to click on stuff that are generated by javascript (or somthing like that).
> 
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

That’s fine too. As I said earlier it can run selenium tests/server. The  
key  
difference being this abstracts that away and manages the lifecycle of  
the  
selenium server for you.

---

<div class="post-metadata">

**Author:** ![kga85](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/90ced4/32.png) [@kga85](https://www.ruby-forum.com/u/kga85)\
**Post date:** [July 2, 2009, 12:46pm UTC](https://www.ruby-forum.com/t/using-selenium-in-a-ruby-program/171320/6 "2009-07-02T12:46:24Z")

</div>

Ben L. wrote:

> On Wed, Jul 1, 2009 at 2:19 PM, Kga A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > > >
> > 
> > ## I can take a look, but I have to test some sites using javascript. I have to test a bunch of server side programs, and the way to start them is to click on stuff that are generated by javascript (or somthing like that).
> > 
> > Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> That’s fine too. As I said earlier it can run selenium tests/server. The  
> key  
> difference being this abstracts that away and manages the lifecycle of  
> the  
> selenium server for you.

Do you have an easy example. Soem code that i can easely copy paste and  
it works? I am new to this kind of coding and I have some problems.

---

<div class="post-metadata">

**Author:** ![kga85](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/90ced4/32.png) [@kga85](https://www.ruby-forum.com/u/kga85)\
**Post date:** [July 3, 2009, 2:43pm UTC](https://www.ruby-forum.com/t/using-selenium-in-a-ruby-program/171320/7 "2009-07-03T14:43:30Z")

</div>

The site I am testing is a secure site with username and password. And i  
have one problem. When i start Selenium tests from a ruby program then a  
new browser is opened, and this browser is cleen like a freshly  
installed firefox browser.

My problem is that the site I am testing gives this error:

Secure Connection Failed  
xxx uses an invalid security certificate.  
The certificate is not trusted because the issuer certificate is  
unknown.  
The certificate is only valid for xxx

(Error code: sec\_error\_unknown\_issuer)

This stops my test and i also has to accept this every time, it wont  
remember that I acceptet it earlyer because it always starts a fresh  
cleen firefox browser.

Is there a way to start the regular browser insted. A browser that  
remember that i have acceptet this before?

When i start the test from the Selenium IDE then it uses my regular  
browser: I want it to use my regular browser when I start it from a ruby  
program to. How do I do that?
