Ruby + Selenium: Reuse open browser session [HELP]

People,
I’ am working with SeleniumDriver in ruby. I have a problem and need
your help. I will explain the idea:

I have 2 scripts in Ruby: LOGIN.rb and SCRIPTB.rb

These scripts are executed in the next order:
a. LOGIN.rb --> I call Selenium Server [selenium =
Selenium::SeleniumDriver.new( remote_control_addr, remote_control_port ,
platform, url, timeout)].
b. After this, the browser (Firefox) is continuing OPEN.
c. SCRIPT.rb --> I need reuse the browser (Firefox) to execute this
script.

My intention is execute many scripts in only one browser. I can’t use
methods and classes between the scripts, because is a limitation of my
platform. The ‘good new’ is that I can pass variables between the two
scripts.

I was listening about selenium.get_cookie() and
selenium.create_cookie(“arg1”,“arg2”) but I don’t understand how to use
between the two scripts.
The other option is using SESSION_ID, but I don’t understand how to set
and reuse the variable between the two scripts.

Someone could help me?

Thanks for read the question and help my,
Regards
Martín

Are you certain that you can’t use a class to contain these methods?
That sounds like an odd situation.

If you can pass a variable from one script to another, then you should
be able to pass the browser itself as an object.

Alternatively this might help:
http://code.google.com/p/selenium/wiki/FrequentlyAskedQuestions#Q:_How_do_I_handle_pop_up_windows?

Tanks Joel, but I need examples in Ruby.

For example:
WebDriver.switchTo().window() in ruby.

In my case I need REAL EXAMPLES that explain:
selenium.get_cookie() AND
selenium.create_cookie(“arg1”,“arg2”)

OR

Set SESSION_ID in Selenium + Ruby

Examples here in Ruby:
http://seleniumready.blogspot.co.uk/2010/09/how-to-create-selenium-cookies.html
Play around with IRB and see what you can do with it.

Perhaps if you explain what your goal is and what your obstacles are in
more detail we might be able to find the best method. My examples tend
to use watir-webdriver since this is what I am familiar with.

If your reason for experimenting with cookies is to match what your
standard profile uses, then that can be achieved with something like
this:
Watir::Browser.new :firefox, :profile => ‘default’

As far as I know Webdriver can only use windows created by the current
session, since the “attach” method only existed for browsers using a COM
object (IE).

If you’re looking to log into a page and then execute a series of other
scripts, this is what I would do:


require ‘watir-webdriver’

class BrowserTest

attr_accessor :name, :pass, :driver #Not technically required

def initialize
@name = ‘me’
@pass = ‘pass’
end

def start
@driver = Watir::Browser.new
end

def login
@driver.goto ‘some-page’
@driver.text_field(:id => ‘name’).set @name
@driver.text_field(:id => ‘pass’).set @pass
@driver.button(:name => ‘go’).click
end

def script1
puts @driver.url
end

def script2
puts @driver.title
end

def run
start
login
puts ‘Press enter to continue…’
gets
script1
script2
puts ‘Press enter to quit’
gets
close
end

def close
@driver.close
end

end

BrowserTest.new.run

If you’re using multiple scripts you should be using “require” or
“require_relative” to put them together so they can access each other’s
objects. You can build them into the same Class or Module even when they
are in seperate files.
As your scripts are executing in the same environment and are able to
pass objects between them, you should look at how Ruby can build a
single object from multiple files.

File1:


class MyClass
def some_method
#Do stuff
end
end


File2:


class MyClass
def some_other_method
#Do more stuff
end
end


File3:


require ‘File1’
require ‘File2’

class_instance = MyClass.new
class_instance.some_method
class_instance.some_other_method


Joel,

Thanks for your response. I’m found a solution for the problem, but
first I explain you my tool and limitations.

The name of the tool is Cacique. This tool was developed by Mercado
Libre testers team. Mercado Libre is similar to Ebay, but the difference
is that was developed in Argentina to Latin America. This is the link:
http://www.mercadolibre.com/

Cacique is Open Source tool, that working on UNIX OS. You access via a
browser. For Example: Firefox, Chrome, IE, Opera or Safary. In the site,
you have a special workspace when you design your test or make suites
and execute the test.
You can move data between two script with Data Set. Data is for example
a variable or in my case, I found that you can send the DRIVER method.
This is very important. Why? If you have two or more script in a Suite,
you can use and reuse the same browser.

If you need more information about Cacique, you can visit:
http://cacique.mercadolibre.com/

Regards,
Martin,