Using selenium in a ruby program

K

Kga Agk

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?
 
K

Kga Agk

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?
 
B

Ben Lovell

[Note: parts of this message were removed to make it a legal post.]

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?
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
 
K

Kga Agk

Ben said:
require 'rubygems'
require "selenium/client"
:url => "http://www.google.com",
@browser.type "q", "Selenium seleniumhq.org"
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).
 
B

Ben Lovell

[Note: parts of this message were removed to make it a legal post.]

Ben said:
require 'rubygems'
require "selenium/client"
:url => "http://www.google.com",
@browser.type "q", "Selenium seleniumhq.org"
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).
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.
 
K

Kga Agk

Ben said:
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.
 
K

Kga Agk

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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top