Running two instances of web driver in parallel

6,984 views
Skip to first unread message

barryDev

unread,
Nov 9, 2008, 12:04:42 PM11/9/08
to webdriver
Hi All,

Firstly have to say I'm very impressed with WebDriver. Very
lightweight and simple to get running and very fast. Was struggling
to get selenium to work with firefox 3 and stumbled upon WebDriver.
20 minutes later I had tests running. Also really like the
PageObjects pattern.

Anyway I want to test a chat room so I need two instances of web
driver running beside each other to test users sending messages to
each other. I found a thread here mentioning using web driver in
separate threads for load testing. Before I go ahead and write this,
I thought I'd ask quickly if anyone has any cautionary tales on this
approach. Either way I'll report back on how I got on.

Simon Stewart

unread,
Nov 9, 2008, 1:28:43 PM11/9/08
to webd...@googlegroups.com
Hi,

Thank you for your kind words: it's great to hear that you've managed
to get up and running with it so quickly! :)

In answer to your question: the only drivers where you'll see the
expected behaviour you want is with the Firefox and HtmlUnit drivers.
The reason for this is that we are tied to the behaviour of the
underlying browsers. With Firefox we can simply create a new,
temporary profile per instance, and HtmlUnit works in isolation too.
OTOH, Internet Explorer quite reasonably expects that only one user
will be using it. As a result it will share a lot of data, in
particular in the form of cookies, between running instances. This
makes the "side by side" testing hard to do with, though if you can
live with the limitation that only one user can be logged in at a time
(effectively chatting to themselves) the IE driver should work fine
too.

Regards,

Simon

Nat Pryce

unread,
Nov 9, 2008, 1:54:05 PM11/9/08
to webd...@googlegroups.com
You could set up two (or more) web driver servers, each acting as a
different user, and remotely control them from your tests. We do that
to test our GUI on Windows browsers when our tests are run by our
TeamCity server on Unix.

--Nat

2008/11/9 Simon Stewart <simon.m...@gmail.com>:

Simon Stewart

unread,
Nov 9, 2008, 1:58:34 PM11/9/08
to webd...@googlegroups.com
So long as the remote servers are on different machines or running as
different users :)

Simon

barryDev

unread,
Nov 9, 2008, 4:02:07 PM11/9/08
to webdriver
I've run into a bit of strange behaviour. I can't get two threads to
create a new WebDriver Object at the same time. I've tried explicitly
creating and using separate firefox profiles and ports but it still
fails. I'm using the webdriver-firefox 0.5.588 with java 1.5 with
firefox 3.0.3 installed on XP. Here's my runnable class used to
launch web driver:
package ie.barrydev.chatroom.integration;

import org.apache.commons.lang.StringUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class ChatUserRobot implements Runnable {

private Thread thread;
private WebDriver driver;
private String results;

ChatUserRobot(String name, int port) {
thread = new Thread(this);
thread.setName(name);
thread.start();
}

public void run() {

try {
driver = new FirefoxDriver();
driver.get("http://www.google.ca/search?q=" +
Thread.currentThread().getName());
results = driver.findElement(By.xpath("//div[@id='ssb']/
p")).getText();
//Don't close the driver if its thread one so that I can test two
drivers open at once
if (!StringUtils.equals(thread.getName(),
"one")){
driver.close();
}


} catch (Exception e) {
e.printStackTrace();
}

}

public Thread getThread() {
return thread;
}

public String getResults() {
return results;
}

}

and here's where I'm calling it from:
public void testTwoUsers() throws Exception {

ChatUserRobot one = new ChatUserRobot("one", 4440);
Thread.sleep(1000);
ChatUserRobot two = new ChatUserRobot("two", 4441);

one.getThread().join();
two.getThread().join();

assertTrue(StringUtils.contains(one.getResults(), "one"));
assertTrue(StringUtils.contains(two.getResults(), "two"));
}

The first thread completes successfullly and is left open but the
second thread fails with the following exception:

java.lang.RuntimeException: Unable to connect to Firefox. Is the
WebDriver extension installed, and is there a profile called
WebDriver?
To set up a profile for WebDriver, simply start firefox from the
command line with the "ProfileManager" switch
This will look like: firefox -ProfileManager. Alternatively, use the
FirefoxLauncher support class from this project
at
org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:
114)
at
org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:
64)
at
ie.barrydev.chatroom.integration.ChatUserRobot.run(ChatUserRobot.java:
23)
at java.lang.Thread.run(Unknown Source)

Am I making a basic error here that I'm not seeing or is it something
to do with my environment? Its not essential that I get two
simultaneous web driver sessions going but it would allow me to test a
user being added and removed from another users list upon joining/
leaving a room.

On Nov 9, 1:58 pm, "Simon Stewart" <simon.m.stew...@gmail.com> wrote:
> So long as the remote servers are on different machines or running as
> different users :)
>
> Simon
>
> On Sun, Nov 9, 2008 at 6:54 PM, Nat Pryce <nat.pr...@gmail.com> wrote:
>
> > You could set up two (or more) web driver servers, each acting as a
> > different user, and remotely control them from your tests.  We do that
> > to test our GUI on Windows browsers when our tests are run by our
> > TeamCity server on Unix.
>
> > --Nat
>
> > 2008/11/9 Simon Stewart <simon.m.stew...@gmail.com>:
>
> >> Hi,
>
> >> Thank you for your kind words: it's great to hear that you've managed
> >> to get up and running with it so quickly! :)
>
> >> In answer to your question: the only drivers where you'll see the
> >> expected behaviour you want is with the Firefox and HtmlUnit drivers.
> >> The reason for this is that we are tied to the behaviour of the
> >> underlying browsers. With Firefox we can simply create a new,
> >> temporary profile per instance, and HtmlUnit works in isolation too.
> >> OTOH, Internet Explorer quite reasonably expects that only one user
> >> will be using it. As a result it will share a lot of data, in
> >> particular in the form of cookies, between running instances. This
> >> makes the "side by side" testing hard to do with, though if you can
> >> live with the limitation that only one user can be logged in at a time
> >> (effectively chatting to themselves) the IE driver should work fine
> >> too.
>
> >> Regards,
>
> >> Simon
>

Simon Stewart

unread,
Nov 9, 2008, 5:15:01 PM11/9/08
to webd...@googlegroups.com
Hi,

Just call the normal FirefoxDriver constructor: you don't need to
specify the port yourself, even when you want multiple versions of the
Firefox driver running at the same time. Are you using the downloaded
JARs or running off the latest version built from source? We recently
closed a bug to do with starting multiple instances of Firefox at the
same time, but we've yet to do a full release.

One important thing to bear in mind is that WebDriver has not been
written to be thread safe. You should be okay using a driver per
thread, but not one driver for many threads.

The way that I've written the tests that you're attempting to write
before has been to instantiate both instances in the same thread and
then hand control back and forth between them, like so:

WebDriver first = new FirefoxDriver();
first.get(url);
WebDriver second = new FirefoxDriver();
second.get(url);

String original = first.findElement(by).getText();
second.findElement(by).click();
String newValue = first.findElement(by).getText();

assertTrue(!original.equals(newValue));

I hope that helps!

Regards,

Simon

barryDev

unread,
Nov 9, 2008, 8:21:57 PM11/9/08
to webdriver
Running your exact code above fails for me. I grabbed the latest
source and I'm getting two test failures, one seems like a simple
assert that didn't get updated:

1)
testChordControlCutAndPaste(org.openqa.selenium.TypingTest)java.lang.Assertio
nError:
Expected: is "!\"#$%&'()*+,-./0123456789:;<=>?@ ABCDEFG"
got: "!\"#$%&'()*+,-./0123456789:;<=>?@ ABCD"

The other one seems to relate to running two drivers together: first
the test fails with this exception:

java.io.IOException: Unable to bind to locking
port
at
org.openqa.selenium.firefox.internal.NewProfileExtensionConnection.ge
tLock(NewProfileExtensionConnection.java:51)
at
org.openqa.selenium.firefox.internal.NewProfileExtensionConnection.<i
nit>(NewProfileExtensionConnection.java:21)
at
org.openqa.selenium.firefox.internal.ExtensionConnectionFactory.conne
ctTo(ExtensionConnectionFactory.java:21)
at
org.openqa.selenium.firefox.FirefoxDriver.connectTo(FirefoxDriver.jav
a:139)
at
org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:1
11)
at
org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:6
4)
at
org.openqa.selenium.firefox.FirefoxDriverTest.testShouldBeAbleToStart
MoreThanOneInstanceOfTheFirefoxDriverSimultaneously(FirefoxDriverTest.java:
48)

and than its reported as in the test results as:

testShouldBeAbleToStartMoreThanOneInstanceOfTheFirefoxDriverSimultaneously(or
g.openqa.selenium.firefox.FirefoxDriverTest)java.lang.RuntimeException:
Unable t
o connect to Firefox. Is the WebDriver extension installed, and is
there a profi
le called WebDriver?
To set up a profile for WebDriver, simply start firefox from the
command line wi
th the "ProfileManager" switch
This will look like: firefox -ProfileManager. Alternatively, use the
FirefoxLaun
cher support class from this project
at
org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:1
14)
at
org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:6
4)
at
org.openqa.selenium.firefox.FirefoxDriverTest.testShouldBeAbleToStart
MoreThanOneInstanceOfTheFirefoxDriverSimultaneously(FirefoxDriverTest.java:
48)

I thought the "Unable to bind to locking port" might point to a
firewall problem but I've turned it off and its still failing. I'm
going to try dropping to firefox 2 to see if that sorts out the issue.

Barry

barryDev

unread,
Nov 9, 2008, 9:32:10 PM11/9/08
to webdriver
Test still failing with firefox 2.0.17. Anything else you think
that's worth trying to debug this?
> ...
>
> read more »

Simon Stewart

unread,
Nov 10, 2008, 5:43:28 AM11/10/08
to webd...@googlegroups.com
Hi Barry,

The "Unable to bind to locking port" is the key. The way that the
firefox driver works is that the java side of the code talks to a
firefox extension over a socket. Only one process at a time can be
bound to any particular (server) socket, and each instance of the
firefox driver is a new process; this means that we need some
synchronization mechanism that works not only within the same JVM, but
also across JVMs and, indeed, languages. To complicate matters
further, this process needs to be atomic and be available on every
supported OS.

The mechanism we use is bind to a "well known" port. Since we don't
know what ports you, as a user, have permission to bind to, we base
this off the port that you've told the profile to use. We do this by
subtracting one. In pseudo-code, the process to launch looks like:

lock = bindToWellKnownSocket();
port = findNextFreePortBasedOnProfilesPortProperty();
startFirefox(port);
releaseSocket(lock);

So, if you've told one profile to use "3000" and the next to use
"3001" the first profile will start. The second will attempt to bind
to port 3000 (3001 - 1) as the well-known port. Sadly, the first
profile is already running on this port, so the second profile times
out. The solution is to either not set the port on the profile (in
which case firefox binds to the first available port starting from
7055) or to set it to the same value for every profile.

In other words, the "port" argument is treated as a recommendation
rather than an absolute value.

Does that help clear things up? If you're still having trouble, then
it sounds like you've managed to find a bug, which I need to fix. If
so, could you please let me know which OS you're on? The Service Pack
number would be useful if you're on Windows too (I suspect you're on
XP SP3, but I've been wrong before :)

Regards,

Simon

barryDev

unread,
Nov 10, 2008, 10:19:07 PM11/10/08
to webdriver
Hey Simon,

I've tried without setting ports and by setting ports that aren't next
door to each other and its still failing. I've also written a simple
java server client and I was able to bind to all the ports I'm using
so I don't think its a dodgy firewall.

You were right I'm using XP sp3. Do you reckon that's the issue?

Barry
> ...
>
> read more »

Simon Stewart

unread,
Nov 11, 2008, 3:40:06 AM11/11/08
to webd...@googlegroups.com
Hi Barry,

No: that set up should be fine. If neither firefox window opens, I'd
suspect that java is being blocked from binding to ports at all. If
only one opens, however (and that sounds like what's happening) things
are very strange indeed, and I can't think of a good reason for it. Do
the new binaries that were released yesterday make any difference?
They contain a bug fix that someone encountered where if you create
two firefox driver at the same instant only one of them opens.

If that doesn't work, please let me know, and I'll build you a special
version of the firefox driver with copious debugging added.
Alternatively, if you can set a breakpoint in the code, it would be
useful to know which port it can't bind to, which driver is failing
and which profile directory they're using. I can send you the line
numbers and file names if that would make it easier.

Thanks for continuing to attempt to get webdriver working for you: I
realise that this must be deeply frustrating for you.

Regards,

Simon

barryDev

unread,
Nov 11, 2008, 8:36:01 PM11/11/08
to webdriver
Hey,

I grabbed the latest release and its still occuring. I've run through
it in the debugger like you suggested and here's what's happening:

First webdriver instance starts with this profile dir: C:
\DOCUME~1\CAITRI~1\LOCALS~1\Temp\webdriver-
custom-12264525542341006329265
it binds to the lock port 7054 which succeeds, and launches the binary
and everything works fine.

The second webdriver instance starts with this profile dir: C:
\DOCUME~1\CAITRI~1\LOCALS~1\Temp\webdriver-
custom-12264529412811006329266
it than attempts to bind to lock port 7054 and this fails with the
exception: "Address already in use: JVM_Bind" This will continue to
fail until I close the first firefox window.

Checking netstat -a nothing appears to be bound to port 7054 while the
second web driver instance is attempting to bind to the lock port.

Also the first webdriver instance calls releaseLock() in
NewProfileExtensionConnection on port 7054 and it doesn't throw an
exception.

The weird thing is even after the first instance does a successful
bind on port 7054 I'm not seeing it in netstat -a but the 7055 port is
showing up once connectToBrowser(TIMEOUT_IN_SECONDS *
MILLIS_IN_SECONDS); completes.

On Nov 11, 3:40 am, "Simon Stewart" <simon.m.stew...@gmail.com> wrote:
> Hi Barry,
>
> No: that set up should be fine. If neither firefox window opens, I'd
> suspect that java is being blocked from binding to ports at all. If
> only one opens, however (and that sounds like what's happening) things
> are very strange indeed, and I can't think of a good reason for it. Do
> the new binaries that were released yesterday make any difference?
> They contain a bug fix that someone encountered where if you create
> two firefox driver at the same instant only one of them opens.
>
> If that doesn't work, please let me know, and I'll build you a special
> version of the firefox driver with copious debugging added.
> Alternatively, if you can set a breakpoint in the code, it would be
> useful to know which port it can't bind to, which driver is failing
> and which profile directory they're using. I can send you the line
> numbers and file names if that would make it easier.
>
> Thanks for continuing to attempt to get webdriver working for you: I
> realise that this must be deeply frustrating for you.
>
> Regards,
>
> Simon
>
> ...
>
> read more »

Simon Stewart

unread,
Nov 12, 2008, 1:28:57 PM11/12/08
to webd...@googlegroups.com
Hi Barry,

Would it be possible for you to send me the failing code off-list? I'm
having trouble duplicating this problem, and I'd like to find out
what's happening.

Regards,

Simon

Vvek

unread,
Dec 18, 2013, 5:11:46 AM12/18/13
to webd...@googlegroups.com
Hi Simon, How do I focus a single needed window? I can't focus a single browser window when multiple browsers are opening. could you please help me.

darrell

unread,
Dec 18, 2013, 8:07:29 AM12/18/13
to webd...@googlegroups.com
Wow. I think this might be a record for reviving an old thread. Until now, no one posted to this thread for 5 years.

For each browser you open you should have a different WebDriver instance. For example, within the same code I might have:

    WebDriver driver1 = new ChromeDriver();
    WebDriver driver2 = new ChromeDriver();

All interaction with driver1 will go to the first Chrome browser and all interaction with driver2 will go to the second Chrome browser. In manual testing, if I want focus on the first browser, I click on the first browser. If I want to switch focus to the second browser then I click on the second browser. With WebDriver I would use driver1 to click on something and it would put focus on the first browser. If I wanted to put focus back on the second browser I would use driver2 to click on something in the second browser.

Vivek Soundararajan

unread,
Dec 18, 2013, 8:33:18 AM12/18/13
to webd...@googlegroups.com
wow. thank you so much darrell. 


--
You received this message because you are subscribed to a topic in the Google Groups "webdriver" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/webdriver/LNfQ-0HPEh0/unsubscribe.
To unsubscribe from this group and all its topics, send an email to webdriver+...@googlegroups.com.
To post to this group, send email to webd...@googlegroups.com.
Visit this group at http://groups.google.com/group/webdriver.
For more options, visit https://groups.google.com/groups/opt_out.

selenium...@gmail.com

unread,
Feb 4, 2014, 11:52:24 AM2/4/14
to webd...@googlegroups.com


HI Vivek /darrell

I am also facing similar issue. I could open my application on two browsers using 2 webdriver instances. And also I am able to do some action on both browsers . But problem is I am not able to focus the browser on which webdriver is doing action.means I opened Firefox first then Chrome. So I can see the action which are performing on Chrome Browser But I can't see the actions on FireFox. My question is how to switch the focus to the browser where webdriver is doing the actions.


darrell

unread,
Feb 5, 2014, 7:42:12 AM2/5/14
to webd...@googlegroups.com
If you have two separate instances of WebDriver then all commands sent to one instance should go to Chrome and the other go to Firefox. For example:

    WebDriver chrome = new ChromeDriver();
    WebDriver firefox = new FirefoxDriver();

all commands sent to the chrome object should go to the Chrome browser and all commands sent to the firefox object should go to the Firefox browser. I know that Actions might be using native events and commands might go to which every window has focus rather than which instance you are calling, In that case you need to use the chrome object to put focus on the Chrome browser then all Actions will end up going to the Chrome browser regardless of whether you use the chrome or firefox instances.

selenium...@gmail.com

unread,
Feb 10, 2014, 9:55:42 AM2/10/14
to webd...@googlegroups.com
Hi Darrell
Thanks for the quick reply.

I can able to do the action by switching the browser. means Action1 on Firefox then Action2 on Chrome.But my question is how to put the focus on the browser where actions are performed? like If Action1 is performing focus should be on FireFox and Action2 is performing focus should be on chrome browser. ?
But currently in my case focus is always on chrome as it was opened last.
Reply all
Reply to author
Forward
0 new messages