coderrr

October 15, 2007

Patch to FireWatir and jssh to support testing with multiple concurrent Firefox browsers

Filed under: patch, ruby, testing — Tags: , , — coderrr @ 6:55 pm

With these patches you can now run two different firefox browsers with different profiles (and thus different sets of cookies) concurrently. This means if you are writing a test for an application that might require you to log in through two different user accounts (e.g. a facebook app), you can make your tests run a lot faster since you won’t have to constantly log in and log out of each user. By using two browsers you can reduce the number of steps your tests take so they run faster. For example:

- login to user1
- do something with user1
- logout user1/login user2
- do something with user2
- logout user2/login user 1
- do something with user1
- etc…

versus

- browser 1: login to user1
- browser 2: login to user2
- b1: do something with user1
- b2: do something with user2
- b1: do something with user1

Here’s an example of a test using two browsers, this logs in to two different facebook accounts at the same time:


require 'firewatir'

t1 = Thread.new do
  ff1 = FireWatir::Firefox.new(:port => 10001, :profile => 'watir1')
  ff1.goto 'facebook.com'

  ff1.text_field(:id, 'email').value = "tester1@gmail.com"
  ff1.text_field(:id, 'pass').value = "tester1"
  ff1.button(:id, 'doquicklogin').click
end

t2 = Thread.new do
  ff2 = FireWatir::Firefox.new(:port => 10002, :profile => 'watir2')
  ff2.goto 'facebook.com'

  ff2.text_field(:id, 'email').value = "tester2@gmail.com"
  ff2.text_field(:id, 'pass').value = "tester1"
  ff2.button(:id, 'doquicklogin').click
end

t1.join
t2.join

How to do it

There are two patches you need to apply. One to FireWatir, one to the jssh firefox extension. The jssh patch allows you to specify a port number for jssh to listen on with a command line option: firefox -jssh port#. Previously jssh always listened on port 9997 (unless you edited the config file). The FireWatir patch gets rid of some ugly global variables which prevented you from using more than one browser. It also allows you to specify the jssh port when initializing a Firefox browser object.

Apply the FireWatir patch to the svn trunk (rev. 123)
Apply the jssh patch to components/nsJSShStarter.js in your jssh extension directory

FireWatir patch note: use the attachment (link is at the top) not the actual syntax highlighted text
FireWatir gem after patching
jssh patch
patched jssh XPI for linux
patched jssh XPI for windows
patched jssh XPI for darwin

Update

I updated the FireWatir patch so that it uses the -no-remote command line option in Windows so that you can run two instances simultaneously.

Update

I uploaded a patched FireWatir gem and patched XPIs for jSSH

12 Comments »

  1. I would like to see a continuation of the topic

    Comment by Maximus — December 20, 2007 @ 2:50 pm

  2. I just tried the above code in IRB… but when ever i create a new profile and the port… I’m in a need to change the JSSH port… so its not possible… what shall i do???

    heres my IRB entries…

    >> require “firewatir”
    => false
    >> ff1 = FireWatir::Firefox.new(:port => 9997, :profile => ‘watir1′)
    PROFILE: -P watir1
    => #<FireWatir::Firefox:0×14f37dc @window_url=”http://www.google.lk/firefox?client=firefox-a&rls=org.mozilla:en-US:official”, @port=9997, @window_title=”Mozilla Firefox Start Page”, @jssh_socket=#>
    >> ff1.goto “facebook.com”
    ^[[A^[[A^[[A=> “http://www.facebook.com/”
    >> ff1.text_field(:id, ‘email’).value = “notme@gmail.com”
    => “notme@gmail.com”
    >> ff1.text_field(:id, ‘pass’).value = “blahblah”
    => “blahblah”
    >> ff1.button(:id, ‘doquicklogin’).click
    => 0
    >> ff2 = FireWatir::Firefox.new(:port => 9997, :profile => ‘watir2′)
    PROFILE: -P watir2
    => #<FireWatir::Firefox:0×1424338 @window_url=”about:blank”, @port=9997, @window_title=”", @jssh_socket=#>
    >> ff2.goto “facebook.com”
    => “http://www.facebook.com/home.php?”

    **note the above line i tried to access but its already logged in…

    I just clicked JSSH on firefox… set the port to 9997 nd opend a new window set the port to 9998….

    Comment by Harry — March 14, 2008 @ 9:18 am

  3. You need to pass a different port to Firefox.new for each profile as the example shows. If you have the patched jssh extension it should work.

    In your IRB statements you used the same port for both.

    Comment by coderrr — March 14, 2008 @ 9:32 am

  4. Yes i tried diffrent ports and it seems… the JSSH accepts 1 port at a time… so how could i use both of the port at the same time…

    Comment by Harry — March 15, 2008 @ 1:20 am

  5. hey please note that i tired this one on the leopard… and the above problem came… and i tied this on windows…. and i couldn’t find JSSH config window…

    Comment by Harry — March 15, 2008 @ 1:36 am

  6. Are you sure you have two instances of firefox running, each with a different profile? Have you created both profiles and installed the jssh extension on each one?

    Comment by coderrr — March 23, 2008 @ 4:32 am

  7. How do I install the Firewatir patch?

    I click the link then I svn trunk then I download the installation guide.doc.

    I don’t have telnet, so I’m not sure what to do there.

    I attempted to install the gems, but they didn’t exists.

    I’m getting frustrated with firewatir

    Comment by Darin Duphorn — June 17, 2008 @ 5:39 pm

  8. Hi,

    Have you got a jssh version that supports Firefox 3.1 on windows and linux? If not, please could you tell me how to build them/modify them.

    Thanks in advance.
    Craig.

    Comment by Craig Stock — August 30, 2008 @ 9:05 pm

  9. [...] patched version appeared where it was necessary to say a port number, but the patched plug-in is compatible only with [...]

    Pingback by valibuk.net » JSSh (Firefox 3.0) with Optional Port Number — November 9, 2008 @ 6:15 pm

  10. [...] For one of my contracts I reworked the FireWatir (automated UI testing) code to be thread-safe so that you could control multiple [...]

    Pingback by Contributing to open source can be hard and frustrating « coderrr — December 5, 2008 @ 9:54 am

  11. if a new param was added — nb, jssh-port as per an existing patch in the firefox issue tracker — this could be made more transparent.

    Comment by Anonymous — December 17, 2008 @ 11:24 pm

  12. [...] Re: Anyone scraping dynamic AJAX sites? On Mon, Dec 1, 2008 at 10:23 AM, Florian Gilcher <flo> wrote: > —–BEGIN PGP SIGNED MESSAGE—– > Hash: SHA1 > > Actually, firewatir and scRUBYt! are nice. > > But is there a possibility to start firefox with a second profile (so that > it circumvents the "one instance"-rule) and rendering to a hidden display? > [1][2] > > Otherwise, this really hurts testablity (as the browser might retain your > personal session) and usability on a deployment server. > > Regards, > Florian Gilcher > > [1]: Preferably a virtal one on a console-only machine. > [2]: Sadly, afaik, firefox has no hidden-mode. http://coderrr.wordpress.com/2007/10…efox-browsers/ [...]

    Pingback by Anyone scraping dynamic AJAX sites? | keyongtech — January 18, 2009 @ 5:35 pm


RSS feed for comments on this post. TrackBack URI

Leave a comment

Blog at WordPress.com.