# Ruby + Selenium: Reuse open browser session \[HELP\]

**URL:** <https://www.ruby-forum.com/t/ruby-selenium-reuse-open-browser-session-help/225564>\
**Category:** Ruby\
**Created:** [January 15, 2013, 3:09pm UTC](https://www.ruby-forum.com/t/ruby-selenium-reuse-open-browser-session-help/225564 "2013-01-15T15:09:12Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![aris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/258eb7/32.png) [@aris](https://www.ruby-forum.com/u/aris)\
**Post date:** [January 15, 2013, 3:09pm UTC](https://www.ruby-forum.com/t/ruby-selenium-reuse-open-browser-session-help/225564/1 "2013-01-15T15:09:12Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mkrauskopf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c37758/32.png) [@mkrauskopf](https://www.ruby-forum.com/u/mkrauskopf)\
**Post date:** [January 15, 2013, 4:20pm UTC](https://www.ruby-forum.com/t/ruby-selenium-reuse-open-browser-session-help/225564/2 "2013-01-15T16:20:26Z")

</div>

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:  
[GitHub - SeleniumHQ/selenium-google-code-issue-archive: Archive, please see main selenium repo](http://code.google.com/p/selenium/wiki/FrequentlyAskedQuestions#Q:_How_do_I_handle_pop_up_windows)?

---

<div class="post-metadata">

**Author:** ![mkrauskopf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c37758/32.png) [@mkrauskopf](https://www.ruby-forum.com/u/mkrauskopf)\
**Post date:** [January 15, 2013, 5:28pm UTC](https://www.ruby-forum.com/t/ruby-selenium-reuse-open-browser-session-help/225564/3 "2013-01-15T17:28:42Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mkrauskopf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c37758/32.png) [@mkrauskopf](https://www.ruby-forum.com/u/mkrauskopf)\
**Post date:** [January 15, 2013, 10:20pm UTC](https://www.ruby-forum.com/t/ruby-selenium-reuse-open-browser-session-help/225564/4 "2013-01-15T22:20:20Z")

</div>

Examples here in Ruby:

> **[How to create selenium cookies](http://seleniumready.blogspot.com/2010/09/how-to-create-selenium-cookies.html)**
>
> create cookie as: selenium.createCookie("name=cookieone", "path=/"); selenium.createCookie("name=cookietwo", "max\_age=60"); display it as:...

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

---

<div class="post-metadata">

**Author:** ![mkrauskopf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c37758/32.png) [@mkrauskopf](https://www.ruby-forum.com/u/mkrauskopf)\
**Post date:** [January 25, 2013, 9:58pm UTC](https://www.ruby-forum.com/t/ruby-selenium-reuse-open-browser-session-help/225564/5 "2013-01-25T21:58:15Z")

</div>

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

* * *

---

<div class="post-metadata">

**Author:** ![mkrauskopf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c37758/32.png) [@mkrauskopf](https://www.ruby-forum.com/u/mkrauskopf)\
**Post date:** [January 25, 2013, 3:46pm UTC](https://www.ruby-forum.com/t/ruby-selenium-reuse-open-browser-session-help/225564/6 "2013-01-25T15:46:52Z")

</div>

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:

> **[Mercado Libre - Envíos Gratis en el día](https://www.mercadolibre.com/)**
>
> Compre productos con Envío Gratis en el día en Mercado Libre. Encuentre miles de marcas y productos a precios increíbles.

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/](http://cacique.mercadolibre.com/)

Regards,  
Martin,
