Sunday 14 April 2013

Run Selenium tests in parallel on multiple browsers

This blog is follow up from my previous one where I worked out an example to take screenshots on test failure. In this blog I am going to attempt to run the same tests however on multiple browsers and in parallel.


To achieve multi-browser parallel run of the test cases:

  • I have updated the testng.xml and added config to run the tests in parallel with browser_type parameters each with its own thread, one for firefox and one for chrome.
  • Updated pom.xml to start using testng.xml file to kickoff the test cases.
  • Updated WebDriverManager class to be thread aware and return correct WedDriver (Firefox or Chrome) instance so that in case of test failure screenshot of correct browser can be taken.
  • Minor tweak in the test case so that its aware of which browser type to run with.
The entire project is uploaded on github for reference: https://github.com/njaiswal/council-tax-band

Right, lets get to the nuts and bolts now.

testng.xml


Two test cases in the suite, TestNG will run these 2 test case with a threadpool size of 2, which means that both browsers will fire-up at almost the same time and go through the test cases in parallel which saves us time!

pom.xml (snippet)


Above pom.xml snippet tells maven to start using testng.xml to kick off the test cases. Please refer full pom.xml on github

WebDriverManager


WebDriverManager is a bit more meaty than previous blog. Instead of keeping the WebDriver instance as static variable it has a static HashMap in which it will store thread Id as the key and the WebDriver instance as value. Since our tests are now going to run on more than 1 thread we need this arrangement so that WebDriverManager knows which instance to return to onTestFailure() method based on the Thread Id.

CTaxBandTest (snippet)


As you can see above I have used the @Parameters annotation along with @BeforeClass. This basically fetches parameter browser_type from the testng.xml file and passes it as method parameter to onTimeSetUp() method. This is a key step where we call WebDriverManager.startDriver() and get correct browser instance created. As mentioned above, WebDriverManager will store the instance reference in a HashMap with thread Id as key so that any future calls to WebDriverManager.getDriverInstance() can identify which browser instance to return.

Reports

Run the tests using 'mvn test' command and you should see 2 browser instances (Firefox and Chrome) getting started at the same time and the tests being run in parallel. 

Two reports get generated (one for each browser type) with total of 4 test cases being run and more importantly both contain correct screenshots of test failures.








Again the entire project is on git hub for reference, happy testing!

16 comments:

  1. I have multi module project, one of them is a common project that contains WebDriver setup. in each module I have different testng.xml file.
    When running 'mvn test' I'm getting error "Parameter 'browser' is required by @Configuration on method setUp but has not been marked @Optional or defined"

    It look like it did not get parameters from testng.xml file

    Can you please help me ? Thanks

    ReplyDelete
  2. The blog gave me idea to Run Selenium tests in parallel on multiple browsers My sincere thanks for sharing this post and please continue to share this post
    Selenium Training in Chennai

    ReplyDelete
  3. really nice blog has been shared by you. before i read this blog i didn't have any knowledge about this but now i got some knowledge. so keep on sharing such kind of an interesting blogs.
    selenium training in chennai

    ReplyDelete
  4. This is my first time i visit here. I found so many interesting stuff in your blog. keep up the good work. Thanks for sharing this useful information for our vision.
    Software Testing Training in Chennai
    Selenium Training in Chennai

    ReplyDelete
  5. Thanks for sharing this valuable information to our vision. You have posted a trust worthy blog keep sharing.Nice article i was really impressed by seeing this article, it was very interesting and it is very useful for me.
    Dot Net Training in Chennai | Python Training in Chennai | Web Designing Training in Chennai

    ReplyDelete
  6. Great posting! Nice to know the performance difference between scrapy and selenium. Thanks.


    Selenium Training in Bangalore

    ReplyDelete
  7. This is my first time i visit here. I found so many interesting stuff in your blog. keep up the good work. Thanks for sharing this useful information for our vision.


    Selenium Training in Chennai

    ReplyDelete
  8. Thanks for sharing such an informative blog. I have read your blog and I gathered some needful information from your post. Keep update your blog. Awaiting for your next update.
    sap abap online training india

    ReplyDelete
  9. Sap fico training institute in Noida

    Sap fico training institute in Noida - Webtrackker Technology is IT Company which is providing the web designing, development, mobile application, and sap installation, digital marketing service in Noida, India and out of India. Webtrackker is also providing the sap fico training in Noida with working trainers.


    WEBTRACKKER TECHNOLOGY (P) LTD.
    C - 67, sector- 63, Noida, India.
    F -1 Sector 3 (Near Sector 16 metro station) Noida, India.

    +91 - 8802820025
    0120-433-0760
    0120-4204716
    EMAIL: info@webtrackker.com
    Website: www.webtrackker.com

    ReplyDelete
  10. Visit Bharat Go Digital Academy to learn the digital marketing skills in India.

    ReplyDelete