22

I would like to run multiple Selenium Tests (on a Jenkins server) at the same time.

It currently runs only a single test at a time cause ChromeDriver seems to communicate over a special port. So somehow I guess I have to pass some kind of port settings via Selenium to the ChromeDriver to start up multiple tests.

The Selenium website unfortunately is empty for that topic: http://docs.seleniumhq.org/docs/04_webdriver_advanced.jsp#parallelizing-your-test-runs

From my point of view it makes no difference if the Test runs locally or on Jenkins, the problem is the same. We need to somehow configure ChromeDriver. The question is just how.

Anybody has some ideas or pointers where to look at and what files are involved to get this done?

seba.wagner
  • 3,800
  • 4
  • 28
  • 52
  • possible duplicate of [python selenium multiple test cases](http://stackoverflow.com/questions/11049505/python-selenium-multiple-test-cases) – Paulo Scardine Jun 24 '14 at 22:24

4 Answers4

21

You can run multiple instances of chromedriver locally quite easily, just instantiate multiple driver objects, chromedriver will keep the profiles separate and find a port to run on all by itself.

Here a link to an example that can run multiple tests using TestNG and Maven:

https://github.com/Ardesco/Selenium-Maven-Template

Just clone the above project and run the following in the command line:

mvn verify -Pselenium-tests -Dbrowser=chrome -Dthreads=2

It takes advantage of TestNG's ability to manage the thread pool and will open up multiple instances if specified. You can do the same thing with jUnit but you'll need to write a custom test runner to fire the tests off into individual threads.

If you decide to use gradle it can deal with managing the thread pools for you with both TestNG and jUnit and a lot of people prefer it to maven.

Ardesco
  • 7,281
  • 26
  • 49
  • This worked using your example. Is it possible to do the same thing with Firefox or is only the Chrome driver that supports multiple threads? – nettie Aug 15 '16 at 14:02
  • All drivers support multiple threads (If you look at the linked projects README.md it will show you all the browser options), however YMMV when using Ghostdriver as PhantomJS versions < 2.0 do not seem to be completely thread safe. I've not really looked at the 2.x releases because chrome has got so fast these days PhantomJS is not really worth it IMHO. – Ardesco Aug 16 '16 at 17:03
  • Does the answer applicable for Java only or can be used for other languages like c# ? – Michael Freidgeim Jun 11 '20 at 20:55
  • as long as you are keeping you instances separate it will work for C# as well – Ardesco Jun 17 '20 at 16:03
10

This is an old question, but for anyone still reading along, it is very possible to run multiple Selenium WebDriver instances in parallel without using Grid. I have successfully tested this using Chrome, FireFox, and PhantomJs (up to 5). Each WebDriver instance uses an isolated context, so session conflict should not be an issue. Be wary of server side conflicts though, depending on the requirements of your website!

For NUnit users, NUnit 3.2.1 now has a 'TestContext.Current.WorkerId' property that will allow you to isolate one WebDriver instance per NUnit worker.

  • Running multiple browsers on the same machine will often hinder performance, so be careful not to use too many browsers instances, or you may actually increase your testing time!
Null511
  • 418
  • 6
  • 7
  • Neat...any idea on how to do this with MSTest? – BlackjacketMack Sep 07 '16 at 17:00
  • @BlackjacketMack - Sorry, I do not use MSTest, since it has very limited functionality compared to NUnit (or any other framework really). If you have not tried NUnit, I highly recommend it. – Null511 Sep 07 '16 at 19:34
6

What you are looking for is Selenium Grid 2.

Grid allows you to :

  • scale by distributing tests on several machines ( parallel execution )
  • manage multiple environments from a central point, making it easy to run the tests against a vast combination of browsers / OS.
  • minimize the maintenance time for the grid by allowing you to implement custom hooks to leverage virtual infrastructure for instance.
sokkyoku
  • 2,161
  • 1
  • 20
  • 22
Amey
  • 8,470
  • 9
  • 44
  • 63
  • 4
    Yes, but Grid looks for me like a cloud computing solution. I actually would like to run multiple tests on a single machine. So instead of buying new hardware I would rather just like to run parallel tests on a single node. Is Grid still the right thing for that? – seba.wagner Apr 15 '13 at 04:38
  • 2
    As far as I understood from https://code.google.com/p/selenium/wiki/ScalingWebDriver what I want is simply not possible. That is why people scale to a Grid and run only one test per machine at a time. – seba.wagner Apr 15 '13 at 04:46
  • 1
    Well what you want can be achieved with Grid. You could setup the Hub and Node on the same machine. Each Node by default is capable to handle 5 FF, 5 Chrome and 1 IE browser instances. – Amey Apr 15 '13 at 04:52
  • 5
    This is massive overkill for what is being asked. Ardesco's answer should be the accepted one. – makhdumi May 05 '16 at 20:23
2

I agree using grid in combination with Maven parallelized class, you can run multiple instance in one PC. Jenkins is possible when you are using Ant for your build ,then you can specify which test can be run parallel. Its quite easy to set it up though ;)

buddy
  • 183
  • 6
  • I doubt somehow that it practically is possible. Don't those browser instances for example share then the same session? For example if you authenticate in one browser, all other browsers will be logged in (no matter if you are using a JSession or PHPSession). The only way to prevent that might be to use the incognito mode (private browsing). However I am not sure in what sense Selenium will help me to prevent effects like that. – seba.wagner Apr 15 '13 at 22:15
  • 1
    you can work with "profile" and set your browser to clean cookies and everyhing, when it is closed. I think it might be your solution – buddy Apr 16 '13 at 08:16