Forum: Ruby Using selenium in a ruby program

Posted by Kga Agk (kga85)
on 2009-06-30 16:54
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/", 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
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?
Posted by Kga Agk (kga85)
on 2009-07-01 13:43
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?
Posted by Ben Lovell (benl)
on 2009-07-01 15:04
(Received via mailing list)
On Wed, Jul 1, 2009 at 12:43 PM, Kga Agk <lord_kga@yahoo.no> wrote:

>    require 'rubygems'
>    require "selenium/client"
>          :url => "http://www.google.com",
>        @browser.type "q", "Selenium seleniumhq.org"
> --
> Posted via 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] http://gitrdoc.com/brynary/webrat/tree/master/

Rgds,
Ben
Posted by Kga Agk (kga85)
on 2009-07-01 15:19
Ben Lovell wrote:
> On Wed, Jul 1, 2009 at 12:43 PM, Kga Agk <lord_kga@yahoo.no> wrote:
> 
>>    require 'rubygems'
>>    require "selenium/client"
>>          :url => "http://www.google.com",
>>        @browser.type "q", "Selenium seleniumhq.org"
>> --
>> Posted via 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] 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).
Posted by Ben Lovell (benl)
on 2009-07-01 15:27
(Received via mailing list)
On Wed, Jul 1, 2009 at 2:19 PM, Kga Agk <lord_kga@yahoo.no> 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/.
>
>
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.
Posted by Kga Agk (kga85)
on 2009-07-02 12:46
Ben Lovell wrote:
> On Wed, Jul 1, 2009 at 2:19 PM, Kga Agk <lord_kga@yahoo.no> 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/.
>>
>>
> 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.
Posted by Kga Agk (kga85)
on 2009-07-03 14:43
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?
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.