First install Java JRE and JDK.
In Advanced system settings/ Environment Variables setup java path:
Create a new system variable (if it is not created):

Name: JAVA_HOME
Value(the location of the jdk):   C:\Program Files\Java\jdk1.7.0_25

In to the Path variable add:
The location of jre:  c:\Program Files (x86)\Java\jre7\bin\;

Install selenium and testing to Eclipse.
Download (from  code.google.com) the selenium-server-standalone-2.33.0.jar file (or the newest).
Copy this file to a simple place, for example C:\

Now open command prompt (with administrator privileges).
Go to c:\ and enter the following line to start Selenium Grid:
java -jar selenium-server-standalone-2.33.0.jar -role hub -port 4444

Open a new command prompt window, go to c:\ and enter the following line to register the browser to the Selenium Grid:
java -jar selenium-server-standalone-2.33.0.jar -role node -hub http://localhost:4444/grid/register -browser browserName=firefox,maxInstances=3

The maxInstance will determine the max number of the browsers. If it is set to 3 than maximum 3 browsers(tests) are able to run parallel.
(If you want to know more about Selenium Grid visit the Selenium site: http://docs.seleniumhq.org/docs/07_selenium_grid.jsp )

Now in Eclipse create a new project, a new package and a new class. Name it TestBase.
To this file create:
•    a new RemoteWebDriver,
•    to the @BeforeMethod the setUp() function
•    to the @AfterMethod the tearDown() function
•    getDriver() function

Here is the full code:

package testNG.Parallel;
import java.net.MalformedURLException;
import java.net.URL;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;

public class TestBase {

	protected ThreadLocal<RemoteWebDriver> threadDriver = null;

	@BeforeMethod
	public void setUp() throws MalformedURLException {

		threadDriver = new ThreadLocal<RemoteWebDriver>();
		DesiredCapabilities dc = new DesiredCapabilities();
		FirefoxProfile fp = new FirefoxProfile();
		dc.setCapability(FirefoxDriver.PROFILE, fp);
		dc.setBrowserName(DesiredCapabilities.firefox().getBrowserName());
		threadDriver.set(new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), dc));
	}

	public WebDriver getDriver() {
		return threadDriver.get();
	}

	@AfterMethod
	public void closeBrowser() {
		getDriver().quit();

	}
}

The URL is set to localhost:4444, to the same location and port as the Selenium Grid. Now create for example 5 java class file. These files would contain the tests.
For example here are my sample test files:

package testNG.Parallel;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.testng.annotations.Test;

public class Test01 extends TestBase{

	@Test
	public void testLink()throws Exception{
		getDriver().get("http://facebook.com");
		WebElement textBox = getDriver().findElement(By.xpath("//input[@value='Your Email']"));
		textBox.click();
		textBox.sendKeys("Just a test!");
		Thread.sleep(2000);
	}
}
package testNG.Parallel;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.testng.annotations.Test;

public class Test02 extends TestBase{

	@Test
	public void testLink()throws Exception{
		getDriver().get("https://twitter.com");
		WebElement textBox = getDriver().findElement(By.xpath("//label[text()='Full name']"));
		textBox.click();
		textBox.sendKeys("Just another test!");
		Thread.sleep(2000);
	}

}
package testNG.Parallel;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.testng.annotations.Test;

public class Test03  extends TestBase{

	@Test
	public void testLink()throws Exception{
		getDriver().get("http://facebook.com");
		WebElement textBox = getDriver().findElement(By.xpath("//input[@value='Re-enter Email']"));
		textBox.click();
		textBox.sendKeys("Test three!");
		Thread.sleep(2000);
	}
}
package testNG.Parallel;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.testng.annotations.Test;

public class Test04  extends TestBase{

	@Test
	public void testLink()throws Exception{
		getDriver().get("https://twitter.com");
		WebElement textBox = getDriver().findElement(By.xpath("//label[text()='Full name']"));
		textBox.click();
		textBox.sendKeys("Test 4!");
		Thread.sleep(2000);
	}
}
package testNG.Parallel;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.testng.annotations.Test;

public class Test05 extends TestBase{

	@Test
	public void testLink()throws Exception{
		getDriver().get("http://facebook.com");
		WebElement textBox = getDriver().findElement(By.xpath("//input[@value='Re-enter Email']"));
		textBox.click();
		textBox.sendKeys("Test 5!");
		Thread.sleep(2000);
	}
}

Important things:
Every test file need to be extended with TestBase.
In the test files doesn’t need the @BeforeMethod or @AfterMethod. These methods will be executed from TestBase.
From the Tests the webdriver will be reached with getDriver() function which is created in TestBase.

Now we need to create two (for the better visibility) xml file in Eclipse to the project root.
One of the files must be named to testng.xml.
Here is the content of the testng.xml file:

<suite name="My Test Suite">
	 <suite-files>
		<suite-file path="./testFiles.xml" />
	 </suite-files>
</suite>

Write the following lines to the other xml file (the paths of the test files):

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Parallel test runs" parallel="tests" thread-count="2">

<test name="T_01">
    <classes>
        <class name="testNG.Parallel.Test01" ></class>
    </classes>
</test>

<test name="T_02">
    <classes>
        <class name="testNG.Parallel.Test02" ></class>
    </classes>
</test>

<test name="T_03">
    <classes>
        <class name="testNG.Parallel.Test03" ></class>
    </classes>
</test>

<test name="T_04">
    <classes>
        <class name="testNG.Parallel.Test04" ></class>
    </classes>
</test>

<test name="T_05">
    <classes>
        <class name="testNG.Parallel.Test05" ></class>
    </classes>
</test>

</suite>

The test file locations are added by the following logic:
Package name (testNG.Parallel) dot fileName (Test01).

In this file the second line is very interesting. There is the “parallel” parameter. Set to “tests” because we want to run tests parallel. The other parameter is the “thread-count”. If it is set to 2, than two browsers will be opened and the first two tests will run from the list. If the thread-count is 5 than five browsers will be opened and all the five tests will executed parallel!

To run the tests just right click to testng.xml , select Run As-> TestNG Suite.
That’s it!
Have fun! 🙂

115 thoughts to “How to run parallel tests with Selenium WebDriver and TestNG

  • dharmpal

    I found this is really good way of carrying out parallel execution. but need to have some more clarity of these spans and searchterms, as these things are written in java code and testng xml files. Please help.

    And also help me understand how it is different from conventional tests we write in testng.

    Thanks
    DP

    • István Lackó

      Hi,
      thanks the feedback. The Selenium/Java search terms are standard commands, you can reed more about it in the Selenium wiki site. There is “no” difference between conventional and parallel tests. The only trick is in the TestBase class in the BeforeMethode. In parallel tests the setup of the Selenium Driver is differ from the conventional tests setup. And of course you need to start the Selenium grid in parallel tests.
      The testng.xml:
      To the testng.xml file you can write the test suits name. For example:
      You have a folder with name myFirstParallelTests and in the folder you have a couple of test files(Test01 and Test02).
      The testng.xml file content will be the following:
      suite name=”My Test Suite”
      suite-files
      suite-file path=”./myFirstParallelTests.xml”
      /suite-files
      /suite

      Of course you need a myFirstParallelTests.xml file which contains the concrete test files path.
      For example:
      suite name=”Parallel test runs” parallel=”tests” thread-count=”2″
      test name=”T_01″
      classes
      class name=”myFirstParallelTests.Test01″ /class
      /classes
      /test
      test name=”T_02″
      classes
      class name=”myFirstParallelTests.Test02″ /class
      /classes
      /test
      /suite

      Hope this helped. If you have any more questions, please feel free to ask!
      Regards,
      Istvan

  • richard

    Hi,
    I tried your solution to implement parallel selenium web test, but the issue I met is that there is only one browser being opened, in theory there should be 3 browsers opened simultaneously, is the same issue happening to you? do you have multiple browsers opened in the same time?
    BTW I totally follow your idea and the hub and node are all in my local machine.

    • István Lackó

      Hi,
      I think the problem is that the tests are very small and fast. The test is done before the following browser window is opened. The solution (for these small tests) to see the parallel browsers that before every step (technically every other step) put a “Thread.sleep(5000);” line. Now you will see that the tests will open in parallel. In big tests there are no such problem.
      Hope this helped. If you have any more questions, please feel free to ask!
      Regards,
      Istvan

      • Rahul

        First of all thanks for the post.
        But i am also facing the same problem that the test is not running parallely. I have even added the wait for after each step, even after that test are executing sequentially. I haven tried it on other browser say IE but same issue i.e tests are running sequentially instead of parallely.

  • Prashant

    Hi,
    I am new in Selenium so may be my question can be very weird and simple ….
    Q1-what we need to do if we want to run test in a sequesnce( one after another), whct should be the configuration for that in xml file.
    Also,
    Q2 What is the significance of executing test parallely does that mean different test get executed at the same time?
    Also
    Q3 One more Question can we lock out machine during test execution, will that hamper its execution as in RFT it didnt work? Please suggest

    • István Lackó

      Hi,
      Q1: If want to run the tests one after another just erase the ‘ parallel=”tests” thread-count=”2″ ‘ part form the xml file.
      Q2: Yes, different tests are executed at the same time.
      Q3: It’s need to run machine normally.

      • nikesh

        Hi,
        My test suite has 15 test cases and when i am running it as TestNGTestSuite, 15 blank firefox browsers are getting opened first and finally it loads the URL in last window of the 15 blank browsers and this is making me unable to see the script execution happening in page. I am using TestNG, Selenium 2 and eclipse to run my tests. Testcases are in testng.xml file.
        Need solution…
        Thanks..
        Nikesh

        • Mano

          Same issue found as Nikesh mentioned above. Can you please explain

  • Prashant

    Q- If we run multiple test parallely, do we see replay of all five test .. or the execution can be seen? it just get executed in primary memory without display.

    • István Lackó

      Yes, you will get reply for all tests.

      • Pritee

        Hi,

        There is a problem with the multiple tab handelling while parallel execution like In my project i have to open multiple tabs in different java classes and individually my programm is working well but if m using parallel execution tabs are getting open abnormally .what would be the solution of it

        • Dane Dukić

          Hi Pritee,
          It is little tricky to do it using TestNG. We had the similar case and we used JUnit test framework to handle this. JUnit creates something like small sandbox for each test and all actions are related to this test.
          Regards.

  • Asaf

    Hello,

    I get the following error when running the testng.xml:

    Any idea what i am doing wrong?

    Thanks.

  • Asaf

    this is the error:

    [[TestNGClassFinder]] Unable to read methods on class Parallel.Test01 – unable to resolve class reference WebDriver

    Thanks again 🙂

    • István Lackó

      Hi,
      maybe the class path is not good. Are you sure that the tests paths are good?
      Regards,
      Istvan

  • Vaibhav L

    Hi,
    I m new to selenium2.
    Pls guide me.
    I want to test two modules seperately with selenium webdriver say “xyz” and “abc”.
    so why i cant able to create two .cs file (“xyz.cs” & “abc.cs”) and run those scripts independently.
    its showing error of multiple instance.
    What m doing currently is commenting “xyz” modules script and running “abc” modules script in a single file.

  • Vaibhav L

    I m writing scripts in visual studio by using C#

  • gShr

    Hi,
    The post is real good and for sure is very informative.
    I am a beginner in Selenium so wanted to know something:
    Remote webdriver uses Selenium standalone jar and the same is used by Webdriver. Also we can run jus 1 instance in one system and in a single browser(non parallel execution) using remote driver. So is it right practice to design the framework that supports Remote webdriver rather than for webdriver ? Does it makes a difference ?

    Thanks

    • István Lackó

      Hi,
      it depends on what is important. If you want to develop your test later than my opinion is that better if the framework is designed to Webdriver. If there is no further development than the Remote webdriver is good too.

  • Kris

    Hi,

    I am able to run tests in parallel using TestNG but the TestNG thread is still hanging around after the test execution is completed. It happens only when I set parallel on suite.

    Please let me know how to get rid of hanging TestNG thread.

    Thanks,
    Kris

    • István Lackó

      Hi,
      it’s maybe a setup issue.. after the @AfterMethod the thread is ended.

  • sarita

    Hello,

    I am new for selenium Automation.

    TestNG is using for parallel testing I believe, as u mentioned below.

    If I don’t give thread-count=2, still my tests will run parallel?

    Regards,
    Sarita

    • István Lackó

      Hi,
      no, the (parallel=”tests” thread-count=”2″) part is necessary to run the tests parallel.
      But don’t afraid to try it out 🙂
      Regards

  • Nick

    Hello,

    With my set up I am not seeing your code work as expected. I have 3 VM’s set up, one running as the hub, and two running as nodes. Each node is set up to run one instance for FF on Windows. When I run your code(only change is my hub URL) and I added a small amount of logging like this

    public class Test01 extends TestBase{

    @Test
    public void testLink()throws Exception{
    System.out.println(“Starting test 1”);
    getDriver().get(“http://facebook.com”);
    WebElement textBox = getDriver().findElement(By.xpath(“//input[@value=’Your Email’]”));
    textBox.click();
    textBox.sendKeys(“Just a test!”);
    Thread.sleep(2000);
    System.out.println(“Test 1 ended”);
    }
    }

    As you can see the system.out’s.

    When I run these tests I get this output

    Starting test 2
    Starting test 1
    Test 1 ended
    Starting test 3
    Starting test 4
    Test 3 ended
    Starting test 5
    Test 5 ended

    The issue is that when two tests are running, and one finishes and calls the @AfterMethod, it is completed and marked as such, but the second test which was running fails, and never even gets to the end of the test to state that it has ended. It is as if when the getDriver.quit() is called, it kills both drivers that are currently running.

    I am not sure if this is a set up issue with my gird or what, but hoped you may be able to help.

    Regards,

    Nick

    • István Lackó

      Hi,
      try maybe @AfterSuite against @AfterMethod.
      I will make a little research to improve this parallelisation.

      Regards,
      Istvan

      • Nick

        Unfortunately no luck with that change.

        • Nick

          Opp, I feel silly. The issue is that the xpath on twitter has changed. It was just moving to fast for me to see that it just could not find the element.

  • Yuri Weinstein

    Just wondering if I need to run paralel test against 5 different versions of IE and/or FF – how and where would define browser configurations?

    • István Lackó

      Well, it’s tricky because there are different versions of webdriver too. A concrete version of webdriver is support just a couple of versions of the browsers. For example the newest webdriver support the current version of Firefox (25.0), maybe the 26.0 and 27.0, backward maybe to 22.0. So if you want to test in Firefox 20 you will need another webdriver. The solution maybe to setup the different version of browsers in separate machines with the necessary webdrivers and you control the browsers with Selenium Grid.

  • Nilesh

    Hi István,
    Thanks for the post, I was just wondering about the Chrome and IE setup.Since FF has default driver info it has nothing to do with “ThreadLocal threadDriver ” and it can be easily accessible for getDriver() method, But when i did the chrome driver setup , my code is not reaching to “getDriver()” method to call the Test, Whereas chrome(Working on chrome now) is opening the instances of browser accordingly but not launching the *.get()URL.
    Kindly suggest me the solution for the related issue..

    Thanks in Advance!

  • Julia

    Good evening, István!
    Everything works just fine!
    However, I still have one question. I really need to set up the name for each thread running. Is there any way to do that?
    Thank you in advance!

    • István Lackó

      Hello Julia,

      Please describe your problem in details, I not really understand how you mean “set up the name for each thread running”. Thx.

  • Vaibhav S

    Istvan Hope you are doing good.
    Thanks for your post which guided me to start with
    I am a newb to TestNg
    I tried with executing multiple @Test one after another
    Just to share I set priority (which in real case should be avoided)
    @Test(priority=1),@Test(priority=2)…and it did execute so we can overcome creating multiple classes and have a class with reference to base.

    I have a query
    Can you please put some light on BeforeTest? with eg:- any link or reference appreciated

    • István Lackó

      Hi,
      I’m glad you liked the post.
      BeforeTest – This is a TestNG annotation.
      @BeforeTest: The annotated method will be run before any test method belonging to the classes inside the tag is run.
      Details: http://testng.org/doc/documentation-main.html

  • Rakesh

    Hi,

    I have designed five classes for five different test cases.I want to use WebDriver for it and run sequentially on the same machine.Don’t want to use grid.

    Please customize the code for the “TestBase” class in the comments section here.

    Also in case of testng.xml:

    suite name=”My Test Suite”>

    what should come in suite-file path.

    I have created a package “xyz” inside src.src is inside folder “abc”.

    Thanks & Regards,

    Rakesh

    • István Lackó

      Hi,
      If you don’t want to use grid than you don’t need the whole threadDriver part just setUp seleniumWebdriver normally.
      In the xml you can delete the “parallel” and the “thread-count” part.
      Regards

  • SB

    Thanks for taking this much time to write this nice article Istvan!

    I have a question.
    In this, for running two tests, we are getting the same webdriver object.
    Same object is used by 2 classes (Java objects).

    I believe it creates a problem.

    I created two different Driver objects in each class.
    public class A
    {
    WebDriver classOne= new FirefixDriver();
    …. some code…
    }

    public class B
    {
    WebDriver classTwo= new ChromeDriver();
    …. some code…
    }

    Any comments…

    Best regards

  • DP

    I could see follwing output. Please tell if same I should get. As I don’t see parallel even after using your code.

    Test1 started
    Test1 stopped
    Test3 started
    Test3 stopped
    Test2 started
    Test2 stopped

    • Tihomir Turzai

      Hi DP,
      Please recheck the grid configuration, and make sure you have the thread number set in the testng.xml.

      If the issue still persist, try to increase the timeout in the tests and add a couple more. It is possible to get this output,to be sure check the number of browsers opened at once.

  • Luvky

    What we have to do If we require to run same class file in different browsers at a time.
    & I m using data driven frame work So what command I have to use to run a same class with different test data, in several times, in same/different browser at a time………
    I m struggling to achieve this requirement please give me your precious suggestions.

    • Tihomir Turzai

      Hi Luvky,
      It is very hard to tell what could be your problem without knowing your code or knowing the technologies you use.
      If you use testNG you can create multiple xml-s and put browserName into them as parameter and during the test run you can read out the parameter and run the appropriate browser. This way you can have your tests modularized by browser and can easily remove a browser and add a new one to your test runs.

  • Sunny

    Hello,

    I have a selenium suite. I am using TestNG to run my tests in parallel. I have many test methods in ONE class.

    My testng.xml looks like

    When I run my tests in serial ( one by one ), they run fine ( all passed) BUT when i run them in parallel, Some of them fail ( in random order ). Do anyone knows why that could be ?

    Thanks a lot .

    • Tihomir Turzai

      Hi Sunny,

      A basic tip:
      Try to think over your cases and make them separated as much as possible. Eg.: create new users at the start of the tests if you can, etc.

      This could happen for lots of reasons:
      1. Be sure that the resources on the site are always available. For eg.: if you delete a resource from a page and you want it to use it in another the test will fail.
      2. There can be additional timeouts. If your machine is overloaded the JavaScript execution will slow down or if you overload the server other processes can be slowed down too.
      3. Recheck your code, think parallel. It is possible that you are relying on the contents of an Object, but meantime you are changing it in another test.

  • SAMI

    Hi,
    I am working on a project where i have 1000 users in an excel sheet and my program reads those users and login and do further processing..Right now its taking quite a bit of processing time to check all the 1000 users…I want to parallely run 10 browser instances with 100 users each…so that my processing time will significantly reduce…can it be done using TestNG…if yes then how to go ahead with it.

    Thanks

  • Tyler

    Great guide, thank you very much for breaking everything down step by step.

    The only problem I am having is with running the .xml…

    When you say “To run the tests just right click to testng.xml , select Run As-> TestNG Suite.”, I get stuck.

    1. Where should the .xml files be saved? Should they be saved to the Project? or to the package which I created?
    2. How exactly do I run it as a Suite? I have created suites before via Junit but that is the extent of what I know.

    Again, thank you for this write up.

    • István Lackó

      Hi Tyler,
      1. Save the .xml file(s) to the project root.
      2. As you see in the testng.xml file we give that we run a suite, and in the suite file() we set the tests paths.
      I hope now it’s clear.
      All the best! 🙂

  • Barbara

    Hello!

    Thank you for your previous comments, I’ve found them very useful! 🙂

    I am also new at Selenium and I would like to ask for a little help.
    What I would like to do is to create a class (named TestRunner.java) which I can tell which groups of @Test-s to run. I have 6 groups of them (now they are handled in separated classes), depending on what part of the web application they are executing. Basically I have a smoke test, and 5 more tests for testing the several applications.

    So I need to be able to select which of these 6 tests I would like to run (sometimes all of them, sometimes 2 of them, etc.) and to run them in order (according to the priorities), in one browser which is started and ended by the TestRunner.java class.

    I hope it makes sense, I tried to be clear. 🙂

    Thank you very much,
    Barbara

    • István Lackó

      Hello Barbara,
      If you have a couple of scenarios for test run than you can write a couple of xml files with the specific tests. Than you can put the actual one to the testng.xml file. For example: in testFiles1.xml add the testType1, testType3, testTyepe4 tests, to the testFiles2.xml add other tests, than to the testng.xml add for example , so you need to change only the test suite file path depending on what tests you want to run.

  • Sundar

    Hi,

    I have a set of 70 test cases and i want to run it parellely. I have my data’s in excel sheet.My code will read data’s before @Test and it will be given as the input.When i run it parellely in IE, only one test is executed and the remaining gets failed in the start-up itself.Please give me a quick solution.

    • István Lackó

      Hi,
      What is the situation with the other browsers? Do you tried with newer IE version? Other Selenium, iedriver version? Unfortunately the IE browser is sometimes tricky.

  • Abhi

    Hi,

    i’m getting ” Could not start a new session. Possible causes are invalid address of the remote server or browser start-up failure ” exception..

    i have copied the same code and running with testng but i’m getting this exception

    • István Lackó

      Hi,
      It can be caused by many things. Does the Selenium driver correctly added to the system? Does the Selenium version and browser version are compatible? You can also try to use “http://127.0.0.1:4444/wd/hub” instead of “http://localhost:4444/wd/hub”. Maybe it isn’t able to find the driver in your current Path settings. Is it possibly not in the standard location?

  • Akhil

    I am getting
    org.openqa.selenium.WebDriverException: Unable to bind to locking port 7054 within 45000 ms
    Build info: version: ‘2.33.0’, revision: ‘4e90c97’, time: ‘2013-05-22 15:32:38’
    System info: os.name: ‘Windows 7’, os.arch: ‘x86’, os.version: ‘6.1’, java.version: ‘1.6.0_22’
    Driver info: driver.version: FirefoxDriver
    Command duration or timeout: 47.04 seconds
    Build info: version: ‘2.41.0’, revision: ‘3192d8a’, time: ‘2014-03-27 17:18:15’
    System info: host: ‘CNU31897VW’, ip: ‘10.142.210.152’, os.name: ‘Windows 7’, os.arch: ‘x86’, os.version: ‘6.1’, java.version: ‘1.6.0_22’
    Driver info: org.openqa.selenium.remote.RemoteWebDriver

    this exception

  • Akhil

    i got this error on console..
    Parallel test runs
    Total tests run: 1, Failures: 0, Skips: 5
    Configuration Failures: 5, Skips: 5

    • Tihomir Turzai

      Hi,

      Try to update the webdriver libraries, old libraries can produce error messages like this.

  • Ashwin

    How to run the above test using Chrome. I tried with different settings but couldn’t. Help me is this regard. Pasting below the code:

    Scenario 1:
    threadDriver = new ThreadLocal();
    DesiredCapabilities dcs = DesiredCapabilities.chrome();
    ChromeOptions Chrop = new ChromeOptions();

    dcs.setCapability(ChromeOptions.CAPABILITY, Chrop);

    dcs.setBrowserName(DesiredCapabilities.chrome().getBrowserName());
    threadDriver.set(new RemoteWebDriver(new URL(“http://localhost:4444/wd/hub”), dcs));

    Scenario 2:
    ChromeOptions options = new ChromeOptions();
    //options.addExtensions(new File(“/path/to/extension.crx”));
    options.setBinary(new File(“E:\libraryFiles\chromedriver\chromedriver.exe”));
    DesiredCapabilities capabilities = DesiredCapabilities.chrome();
    capabilities.setCapability(ChromeOptions.CAPABILITY, options);
    RemoteWebDriver driver = new RemoteWebDriver( new URL(“http://localhost:4444/wd/hub”), capabilities);
    .
    ChromeOptions options = new ChromeOptions();
    //options.addExtensions(new File(“/path/to/extension.crx”));
    options.setBinary(new File(“E:\libraryFiles\chromedriver\chromedriver.exe”));
    DesiredCapabilities capabilities = DesiredCapabilities.chrome();
    capabilities.setCapability(ChromeOptions.CAPABILITY, options);
    RemoteWebDriver driver = new RemoteWebDriver( new URL(“http://localhost:4444/wd/hub”), capabilities);

    Can you please explain what is the use of CRX file?

  • Ashwin

    The following error is shown when tried to launch the Chrome browser.

    threadDriver = new ThreadLocal();
    DesiredCapabilities dcs = DesiredCapabilities.chrome();
    ChromeOptions Chrop = new ChromeOptions();

    dcs.setCapability(ChromeOptions.CAPABILITY, Chrop);

    dcs.setBrowserName(DesiredCapabilities.chrome().getBrowserName());
    threadDriver.set(new RemoteWebDriver(new URL(“http://localhost:4444/wd/hub”), dcs));

    ==================================================================
    org.openqa.selenium.remote.UnreachableBrowserException: Could not start a new session. Possible causes are invalid address of the remote server or browser start-up failure.
    Build info: version: ‘2.31.0’, revision: ‘1bd294d’, time: ‘2013-02-27 20:53:56’
    System info: os.name: ‘Windows 7’, os.arch: ‘x86’, os.version: ‘6.1’, java.version: ‘1.6.0_41’
    Driver info: driver.version: RemoteWebDriver
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:548)
    at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:216)
    at org.openqa.selenium.remote.RemoteWebDriver.(RemoteWebDriver.java:111)
    at org.openqa.selenium.remote.RemoteWebDriver.(RemoteWebDriver.java:129)
    at prallelExecutionTests.TestBase.setUp(TestBase.java:66)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:84)
    at org.testng.internal.Invoker.invokeConfigurationMethod(Invoker.java:564)
    at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:213)
    at org.testng.internal.Invoker.invokeMethod(Invoker.java:653)
    at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:901)
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1231)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111)
    at org.testng.TestRunner.privateRun(TestRunner.java:767)
    at org.testng.TestRunner.run(TestRunner.java:617)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:334)
    at org.testng.SuiteRunner.access$000(SuiteRunner.java:37)
    at org.testng.SuiteRunner$SuiteWorker.run(SuiteRunner.java:368)
    at org.testng.internal.thread.ThreadUtil$2.call(ThreadUtil.java:64)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
    at java.util.concurrent.FutureTask.run(FutureTask.java:138)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:895)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:918)
    at java.lang.Thread.run(Thread.java:662)
    Caused by: org.openqa.selenium.WebDriverException: Unable to convert: {desiredCapabilities=Capabilities [{platform=ANY, browserName=chrome, chromeOptions=org.openqa.selenium.chrome.ChromeOptions@e1b61, version=}]}
    Build info: version: ‘2.31.0’, revision: ‘1bd294d’, time: ‘2013-02-27 20:53:56’
    System info: os.name: ‘Windows 7’, os.arch: ‘x86’, os.version: ‘6.1’, java.version: ‘1.6.0_41’
    Driver info: driver.version: RemoteWebDriver
    at org.openqa.selenium.remote.BeanToJsonConverter.convert(BeanToJsonConverter.java:74)
    at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:284)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:527)
    … 27 more
    Caused by: org.openqa.selenium.WebDriverException: java.lang.reflect.InvocationTargetException
    Build info: version: ‘2.31.0’, revision: ‘1bd294d’, time: ‘2013-02-27 20:53:56’
    System info: os.name: ‘Windows 7’, os.arch: ‘x86’, os.version: ‘6.1’, java.version: ‘1.6.0_41’
    Driver info: driver.version: RemoteWebDriver
    at org.openqa.selenium.remote.BeanToJsonConverter.convertObject(BeanToJsonConverter.java:223)
    at org.openqa.selenium.remote.BeanToJsonConverter.convertObject(BeanToJsonConverter.java:166)
    at org.openqa.selenium.remote.BeanToJsonConverter.convertObject(BeanToJsonConverter.java:199)
    at org.openqa.selenium.remote.BeanToJsonConverter.convertObject(BeanToJsonConverter.java:166)
    at org.openqa.selenium.remote.BeanToJsonConverter.convert(BeanToJsonConverter.java:64)
    … 29 more
    Caused by: java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.openqa.selenium.remote.BeanToJsonConverter.convertObject(BeanToJsonConverter.java:217)
    … 33 more
    Caused by: java.lang.NoSuchMethodError: com.google.common.collect.ImmutableList.copyOf(Ljava/util/Collection;)Lcom/google/common/collect/ImmutableList;
    at org.openqa.selenium.chrome.ChromeOptions.toJson(ChromeOptions.java:168)
    … 38 more

    Code:

    threadDriver = new ThreadLocal();
    DesiredCapabilities dcs = DesiredCapabilities.chrome();
    ChromeOptions Chrop = new ChromeOptions();

    dcs.setCapability(ChromeOptions.CAPABILITY, Chrop);

    dcs.setBrowserName(DesiredCapabilities.chrome().getBrowserName());
    threadDriver.set(new RemoteWebDriver(new URL(“http://localhost:4444/wd/hub”), dcs));

  • Eric

    This is a very helpful post. I just wanted to add that if you want to use Netbeans instead of Eclipse you can just add a couple of plugins instead of creating the two files. Here are the plugins.

    org.apache.maven.plugins
    maven-compiler-plugin

    1.5
    1.5

    org.apache.maven.plugins
    maven-surefire-plugin

    methods
    4

  • Sumit

    I am getting the result as below:

    Parallel test runs:-
    Total tests run: 5, Failures: 0, Skips: 5
    Configuration Failures: 5, Skips: 5

    I am using selenium 2.41.0 library. Kindly suggest what could be wrong here.

    • István Lackó

      Hi!
      Maybe the Firefox version is not good, or if you use Chrome maybe the chrome driver.
      Are You use the latest grid driver? Other error message? What says the detailed error message?

  • Tiago Braga

    Hello!

    With parallel tests, can occurs conflicts when two browsers in the same machine and send keys at the same time?

    Thanks!

  • Nimesh

    Great! Thanks for sharing…It really helps.

  • Lokes

    Hi István Lackó,

    Nice post, I have tried your steps. but I was stuck on where to save xml file and how to give the pat.I don’t have any idea. Thankz in advance

    • István Lackó

      Hi,
      Save the .xml file(s) to the project root. “The test file locations are added by the following logic: Package name (testNG.Parallel) dot fileName (Test01).”
      So, You have a project, for example MyProject, under the project there is a folder “src”. In the src folder there is Your package, for example “testNG.Parallel”, where You put the test files, for example “Test01”.
      The .xml file is in the MyProject root.

  • Shessuky

    When i tried to execute 2 tests using selenium web driver, i got this error:
    Caused by: org.openqa.selenium.remote.ErrorHandler$UnknownServerException: TypeError: Cannot find function getBoundingClientRect in object [object HTMLDivElement]. )

    Any suggestions !

    Thanks in advance !

    • István Lackó

      Hi!
      It seems that you want to use a “getBoundingClientRect” function, but there is no such a function. Recheck your code and I think you will find the problem 🙂

  • vinny

    Hi István,

    Nice Post as it is having good clarification of concept. I have a question, what if we want to use ‘parallel’ at suite level. I mean I have 2 testng.xml both further contains path of the .xml file and these xml files have the tests and classes defined in it. So, I want to run both testng.xml files in parallel. I didn’t find any solution by giving paralle=tests or classes.

    Can you please help me on that.

    Thanks!!

    • István Lackó

      Hi Vinny!
      You need to use one testng.xml file. If you want to use at suite level that’s ok. Write the suite files names to the testng.xml, for example TestGroupX.xml.
      To the “TestGroupX.xml” files write what you want to test, classes, packages, etc.

      • Greg

        Hey just follow up related to this.

        Overview – you may have multiple xml files which contain different entries for organizational purposes. Then when you want to run everything, you will have a single testng.xml file which will contain entries to these individual xml suite files.

        For instance:

        When you run your testng.xml file it will run the individual xml suite files serially. Correct? You can’t run suites in parallel – correct? I believe this is what the original question was implying.

        Note: When SuiteA is called first, it will run as it is configured within it’s own xml file. So if it specifies parallel execution it will run in parallel. When it completes its run, then SuiteB will run and it will run as it is configured within it’s xml file.

  • Ekta

    Hi,

    I want to run grid with Junit. I can see various examples on net but all with TestNG. I need it with Junit. Can you help

  • Kumar Ankit

    Hi,
    This post is really helpful.
    I have a query, though the same concept is it possible to run the tests on FF,Chrome and IE at same time on a particular machine. If so how ?
    I am not able to register multiple browser on the same node.

    Many Thanks

    • István Lackó

      Hi,
      I wrote a blog about registering multiple browsers: How to register multiple browser types to selenium grid
      The problem when you want to run tests in different browsers in the same time is that you need to regiser the driver in the beginning in the test (firefoxfriver, chromedriver,iedriver).
      The test will get the driver and use it. If you want to run the tests in different browsers then you need to register different webdrivers.
      I have not tried this scenario yet, so you need to try and experiment it.
      Good luck!

  • Greg

    Hey, been researching and playing with parallel execution and wanted to get your insight.
    Focusing on TestNG implementation running against a Grid configuration.

    Parallel execution seems to be controlled by following areas:
    1) TestNG, the testng,xml file and its use of the parallel=tests(etc) and thread-count=# suite attributes.
    So basically: If parallel=tests and you have a thread-count set to any value greater than 1 (default seems to be 5), it will attempt to kickoff that many tests at once
    (assuming there are that many entries in your xml file).

    2) TestNG, the @DataProvider annotation and (parallel=true) setting

    Example:
    TestNG.xml contains reference to TestA and TestB. The xml file is set to parallel = tests and has a thread count of 5.
    So when I run the testng.xml file, 2 threads will kick off, one calling TestA and one calling TestB.
    Now say TestA has a dataprovider with 5 rows of data and its configured as parallel=true. Say TestB isn’t data driven.
    Question: At this point does the thread count setting in testng.xml file affect how TestA runs?
    If not, I assume 5 TestA executions get kicked off in parallel (one for each data row) and sent to the Grid Hub, and oneTestB execution is sent to Hub.
    So 6 entries being sent in parallel to Hub – seem right or not?

    3) Grid setup
    Then it just comes down to how many nodes and how their configured (OS/browser etc…) to determine how many tests are actually running in parallel or are pooled.

    When it comes to running the test automation in parallel (Focusing on TestNG and Grid) that’s it – right? Or are there other variables that I’m forgetting to take into account?

    thanks for you expertise.

  • Sivaprasad

    Hi,
    This post is really helpful.
    I have a one request,

    here i mentioned my code below

    package testNG.Parallel;

    import org.openqa.selenium.By;
    import org.openqa.selenium.NoSuchElementException;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.firefox.FirefoxDriver;
    import org.testng.Assert;
    import org.testng.annotations.AfterMethod;
    import org.testng.annotations.BeforeMethod;
    import org.testng.annotations.DataProvider;
    import org.testng.annotations.Test;

    public class ParallelExcecutionDemo {

    WebDriver driver=null;
    String baseUrl = “http://www.gmail.com/”;
    @BeforeMethod
    public void LogIN() throws InterruptedException
    {
    driver = new FirefoxDriver();
    driver.get(baseUrl);
    driver.manage().window().maximize();
    Thread.sleep(7000);

    }
    @DataProvider(name = “gmailaccount”)
    public Object[][] createData1()
    {
    return new Object[][] {
    { “********@gmail.com” },
    { “********@gmail.com” },
    { “*********@gmail.com” }
    };
    }

    @Test(dataProvider = “gmailaccount”)
    public void LoginToGmail(String userName) throws InterruptedException {

    driver.findElement(By.xpath(“//*[@id=’Email’]”)).sendKeys(userName);
    driver.findElement(By.xpath(“//*[@id=’Passwd’]”)).sendKeys(“********”);
    driver.findElement(By.xpath(“//*[@id=’signIn’]”)).click();
    Thread.sleep(35000);
    try{
    driver.findElement(By.xpath(“//*[@id=’gb’]/div[1]/div[1]/div[2]/div[5]/div[1]/a/span”)).click();
    }
    catch(NoSuchElementException e)
    { Thread.sleep(1000);
    driver.findElement(By.xpath(“//*[@id=’gb’]/div[1]/div[1]/div/div[3]/div[1]/a”)).click();
    }
    Thread.sleep(2000);
    try{
    Assert.assertEquals(userName, driver.findElement(By.xpath(“//*[@id=’gb’]/div[1]/div[1]/div[2]/div[5]/div[2]/div[1]/div/div[2]”)).getText());
    }
    catch(NoSuchElementException e)
    { Thread.sleep(1000);
    Assert.assertEquals(userName, driver.findElement(By.xpath(“//*[@id=’gb’]/div[1]/div[1]/div/div[3]/div[2]/div[1]/div/div[2]”)).getText());
    }
    }

    @AfterMethod
    public void Logout()
    {
    driver.quit();
    }

    }

    So, is it possible to run @test in parallel with out using Grid

    please share me the solution..

    Can you please help me on that.
    Thanks!!

    • István Lackó

      Yes, it is possible. TestNG have the capability to run methods, classes and tests parallel. However you will lose some benefits, like running your tests on different operating systems and machines all the same time. You don’t have to use Selenium Grid to run tests in parallel mode, if you don’t need these facilities and you want to run tests just on local machine.
      You can use any of WebDriver (InternetExplorerDriver, FirefoxDriver, ChromeDriver, HtmlDriver, OperaDriver) instead of RemoteWebDriver, if you want to ignore Selenium Grid. You don’t need to use DesiredCapabilites as well. Make sure that you configured correctly the attributes (parallel, thread-count) of element in .xml file. The parallel attribute on the tag can take one of following values: “methods”, “tests”, “classes”, “instances”.
      Your tests, methods or classes should start in parallel.
      However specifically your example can’t run parallel with this configuration because you have just one method in one class. If you want to run test parallel with DataProvider annotation, you need to replace your to .
      The parallel attribute of element in file has no influence to your test with data provider.

  • Raghavendra

    Hi,
    This post is really helpful.
    I have tried above mentioned example it is working fine for one node, if i want to connect with one more remote machine node,it will execute all testcases in two machine parallel or it will divide the testcases between two node?.

    And for second node how to pass the URL in TestBase class.

    • István Lackó

      Hi,
      You can simply use testng.xml to configure the number of parallel tests with parallel = true and thread-count=# while you do not use any @DataProvider annotation with parameter parallel = true. In that case Data Providers are run in their own thread pool, which is different from the thread pool used for test methods. You can also change the number of threads in data provider pool so the actual number of threads depend on the parameter thread-count=# and the number of threads from data provider pool.
      Otherwise there is one more thing that needs to highlight. In testng.xml beside the parallel and thread-count there is one more parameter that has effect on the number of threads and this is parallel. It has 4 values (from testNG site: http://testng.org/doc/documentation-main.html#parallel-running):
      parallel=”methods”: TestNG will run all your test methods in separate threads. Dependent methods will also run in separate threads but they will respect the order that you specified.
      parallel=”tests”: TestNG will run all the methods in the same tag in the same thread, but each tag will be in a separate thread. This allows you to group all your classes that are not thread safe in the same and guarantee they will all run in the same thread while taking advantage of TestNG using as many threads as possible to run your tests.
      parallel=”classes”: TestNG will run all the methods in the same class in the same thread, but each class will be run in a separate thread.
      parallel=”instances”: TestNG will run all the methods in the same instance in the same thread, but two methods on two different instances will be running in different threads.

      • Greg

        Follow up to what you stated here with @DataProvider and the parallel=true setting and data provider pool.

        Question, if you use a @DataProvider with the parallel=true setting, but you don’t specify the data provider pool size in your testng.xml file, do you know what the default number would be? I’ve searched, and I can’t seem to find an answer for it? thks

  • Greg

    First, thanks for the post. There aren’t too many good examples that demonstrate parallel execution /w Grid combined with TestBase class – Test class inheritence.

    I used your approach and I wanted to get your input about an issue I’ve run into.

    Setup – Java, TestNG running against Grid configuration (1 Hub, 3 nodes – 3 physical machines)

    I know this is about parallel execution so hang with me.
    In trying your approach I first setup a single @Test.

    This test does the following:
    Goes to url.
    Clicks link to go to login page.
    Enters username, password, clicks login button.
    Performs a simple assertion.
    Clicks link to logout.

    The @Test is datadriven, using the TestNG @DataProvider, which provides 2 rows of account (username/password)
    values – actually both rows contain the same values. So at this point we have a single test that is data-driven.

    I then modify the @DataProvider so that it is now set as (parallel=true). So at this point when the test executes, it should run in parallel. One for each row of data.

    When it runs I see 2 instances (parallel) being kicked off on different nodes, but their seems to be confusion.

    For example, I may see the username value typed in twice into one of the login pages, and the other one left blank. It’s like the commands are being sent to the wrong node.

    Wondering if you have any experience with this – running in parallel, when the parallel is triggered from the Data level?

    Is there something even more elaborate that has to be done with the code to keep everything straight?

    thanks for your input,

    Greg

    • Brajesh

      I also have same problem. Running parallel make driver confused. I see username/password value typed in twice into one of the login pages, and the other one left blank. Any solution to this how to avoid the confusion for Remote webdriver-browser combination. I am using ThreadLocal rDriver

  • amit

    Hi ,
    first thanks for your blog.

    I tried to use this info but I have lots of problems with this issue.
    my xml build in that way:

    in every class there are many tests that I want them to be run.
    my problem is that I defined the webdriver in Threadlocal like u .
    every test that run go to BeforeInvocation before it start to get a new webdriver and then need to use it in the test.

    my problem is that the Thread in the beforeInvocation is not the same thread in the test and this is why the browsers that opens don’t know what to do and don’t get the right url and stays blank.

    do u know how to solve it?

    the main idea is like this one:
    http://rationaleemotions.wordpress.com/2013/07/31/parallel-webdriver-executions-using-testng/

    thanks
    amit

  • deepu

    Hi,

    Can we run multiple xml files using parallel test?

    Description:
    in my case i have a testng.xml which has login class file with 3 methods of 3 customer logins and in each method i am calling a XMl file which data test cases for each customer.In the testng.xml i have given parallel=”tests” thread-count=”3″. and login to 3 customers works but the xml file give after each customer loing doesnt work.

    • Tihomir Turzai

      From this I presume that you have :
      1. A single class with 3 methods which call Login and call data from another XML file.
      2. You want to call several XML files from testng.xml.

      Answer under number 1: At this point there is no way to make methods share a single instance of webdriver, it would have to have some
      extra coding to control, that only one method can work on the instance at a time.
      Solution might be answered here:
      TestThreadCountTest.java but it’s not tested by us yet.

      Suggestions for one of the tips above:

      1) Split tests to separate classes with own login data and call them in testng.xml :

      &lt;!DOCTYPE suite SYSTEM &quot;http://testng.org/testng-1.0.dtd&quot; &gt;
      &lt;suite name=&quot;MySuite&quot; parallel=&quot;tests&quot; thread-count=&quot;3&quot;&gt;
        &lt;test name=&quot;First Login Test&quot;&gt;
          &lt;classes&gt;
             &lt;class name=&quot;packageName.Login1&quot;/&gt;
          &lt;/classes&gt;
        &lt;/test&gt;
        &lt;test name=&quot;Second Login Test&quot;&gt;
          &lt;classes&gt;
             &lt;class name=&quot;packageName.Login2&quot;/&gt;
          &lt;/classes&gt;
        &lt;/test&gt;
        &lt;test name=&quot;Third Login Test&quot;&gt;
          &lt;classes&gt;
             &lt;class name=&quot;packageName.Login3&quot;/&gt;
          &lt;/classes&gt;
        &lt;/test&gt;
      &lt;/suite&gt;
      

      2) Call the class multiple times with entire package, after separated the tests into classes:

      &lt;!DOCTYPE suite SYSTEM &quot;http://testng.org/testng-1.0.dtd&quot; &gt;
      &lt;suite name=&quot;MySuite&quot; parallel=&quot;classes&quot; thread-count=&quot;3&quot;&gt;
        &lt;test name=&quot;Login tests&quot;&gt;
              &lt;packages&gt;
                  &lt;package name=&quot;listOfNestedPackages.loginTestPackage&quot; /&gt;
              &lt;/packages&gt;
          &lt;/test&gt;
      &lt;/suite&gt;
      

      3) If you are calling XML files from the testng.xml we suggest:

      &lt;suite name=&quot;LoginTest Suite&quot; verbose=&quot;1&quot; parallel=&quot;classes&quot; thread-count=&quot;3&quot; allow-return-values=&quot;true&quot;&gt;
      
      	 &lt;suite-files&gt;
      		&lt;suite-file path=&quot;./LoginTests.xml&quot; /&gt; 
      	 &lt;/suite-files&gt;
      	 
      	 &lt;!-
      		//add suits by choice
      	 --&gt;
      	 
      &lt;/suite&gt;
      

      There are several approaches a you can see.
      But if you could provide more details about your problem we might give you a more concrete answer.

  • Alexandra

    Hello Istvan,
    While trying to implement your idea for JUnit I get this errror:
    org.openqa.selenium.remote.UnreachableBrowserException: Could not start a new session. Possible causes are invalid address of the remote server or browser start-up failure.

    at this line: threadDriver.set(new RemoteWebDriver(new URL(“http://localhost:4444/wd/hub”), dc));

    I’m not using a local server, I’m running my tests locally from my IDE and then in the CI. What I need to do is be able to run them in parallel in the CI but it seems that I need to create separate threads for that, right?

    Any suggestions are very apreciated.

    • Tihomir Turzai

      Hi Alexandra,

      If you want to run the test locally you have to create a local webdriver instead of creating a remote one.
      Instead of the RemoteWebDriver you can safely create a FirefoxDriver or ChromeDriver.

      UnreachableBrowserException can appear in multiple situations:
      1. The selenium client-server connection is not properly configured. You have to have selenium running on both of the machines, and connect from the client to the server.
      2. The browser you asked for is not available through grid. Recheck the configurations, and makes sure that the browser you asked for is available on the grid.
      3. Webdriver is not updated. In the past i met with this exception when i used older webdriver libraries.

  • raju

    Hi All,

    i have a scenario where i need to execute test case for 365 urls. suppose say my first url is http://www.abc.com/a/b/c.html. for the first url it will ask user name and password. and for 2nd url (www.abc.com/cd/ht.html) it wont ask for user name and password. how to execute all the urls ? my first url is executing fine but for 2nd it is failing because it already got username and password. can anyone give the sample code for executing this scenario? please help

    • Tihomir Turzai

      Hi,

      I think the easiest way is to put in a small check in the beginning of the test.
      Here is an idea:
      if (userNameInput exists){
      fill username and password fields;
      login;
      }

      or you can make two groups of test cases and you can earn second by avoiding the wait when there is no authentication dialog.

  • Srikanth

    Hi I have a scenario where each test class contains more than one independent test methods.

    Say:
    BaseTest{
    @BeforeMethod void beforeMethod(){}
    @AfterMethod void afterMethod(){}
    }

    TestClass1{
    @Test void test1(){}
    @Test void test2(){}
    }

    TestClass2{
    @Test void test3(){}
    @Test void test4(){}
    }

    Now I want to run these 4 test cases parallel so that each test method have its own browser window and driver object. and I also want to make sure that driver is available for TestLister so that whenever a failure occurs driver object is readily available to take screenshot.

    How can we pass driver object to these independent tests?

    • Tihomir Turzai

      Hi!

      Thanks for your comment on our blog. You can pass your Webdriver object with parametrized constructor or in another beforeMethod in related test classes. First of all you need to extend your test classes (TestClass1, TestClass2) with your BaseTest class. You need to define one Webdriver object per test class (also do this with your TestLister class), create a @BeforeMethod for each test class, and instantiate your webdriver object by calling the method getDriver(). For your TestLister class just create a parametrized constructor with Webdriver parameter. Here is a code snippet for these to solutions:

      private Webdriver driver;

      .
      .
      .

      @BeforeMethod
      public void beforeMethod(){
      this.driver = getDriver();
      }

      ———————————–

      private Webdriver driver;

      .
      .
      .

      public TestLister(Webdriver driver){
      this.driver = driver;
      }

      To run your test methods in parallel mode, just change the value of “parallel” attribute to “methods” in your testng.xml file.

  • Bijal

    Hello,
    Thanks for the detailed blog. I tried the steps in this blog, however I get some errors. I am running hub and node on my mac. Looks like node is not able to register and hence the test cases are failing. I get following errors in eclipse:

    org.openqa.selenium.WebDriverException: Error forwarding the new session Error forwarding the request Connect to 10.82.237.175:5555 [/10.82.237.175] failed: Operation timed out
    Command duration or timeout: 75.65 seconds
    Build info: version: ‘2.45.0’, revision: ’32a636c’, time: ‘2015-03-05 22:01:35’
    System info: host: ‘N/A’, ip: ‘N/A’, os.name: ‘Mac OS X’, os.arch: ‘x86_64’, os.version: ‘10.9.5’, java.version: ‘1.7.0_67’
    Driver info: org.openqa.selenium.remote.RemoteWebDriver
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
    at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:204)
    at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:156)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:599)
    at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:240)
    at org.openqa.selenium.remote.RemoteWebDriver.(RemoteWebDriver.java:126)
    at org.openqa.selenium.remote.RemoteWebDriver.(RemoteWebDriver.java:153)
    at com.cisco.test.load.TestBase.setUp(TestBase.java:24)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:84)
    at org.testng.internal.Invoker.invokeConfigurationMethod(Invoker.java:564)
    at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:213)
    at org.testng.internal.Invoker.invokeMethod(Invoker.java:653)
    at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:901)
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1231)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111)
    at org.testng.TestRunner.privateRun(TestRunner.java:767)
    at org.testng.TestRunner.run(TestRunner.java:617)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:334)
    at org.testng.SuiteRunner.access$000(SuiteRunner.java:37)
    at org.testng.SuiteRunner$SuiteWorker.run(SuiteRunner.java:368)
    at org.testng.internal.thread.ThreadUtil$2.call(ThreadUtil.java:64)
    at java.util.concurrent.FutureTask.run(FutureTask.java:262)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:745)
    Caused by: org.openqa.grid.common.exception.GridException: Error forwarding the new session Error forwarding the request Connect to 10.82.237.175:5555 [/10.82.237.175] failed: Operation timed out
    at org.openqa.grid.web.servlet.handler.RequestHandler.process(RequestHandler.java:114)
    at org.openqa.grid.web.servlet.DriverServlet.process(DriverServlet.java:83)
    at org.openqa.grid.web.servlet.DriverServlet.doPost(DriverServlet.java:67)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:820)
    at org.seleniumhq.jetty7.servlet.ServletHolder.handle(ServletHolder.java:565)
    at org.seleniumhq.jetty7.servlet.ServletHandler.doHandle(ServletHandler.java:479)
    at org.seleniumhq.jetty7.server.session.SessionHandler.doHandle(SessionHandler.java:225)
    at org.seleniumhq.jetty7.server.handler.ContextHandler.doHandle(ContextHandler.java:1031)
    at org.seleniumhq.jetty7.servlet.ServletHandler.doScope(ServletHandler.java:406)
    at org.seleniumhq.jetty7.server.session.SessionHandler.doScope(SessionHandler.java:186)
    at org.seleniumhq.jetty7.server.handler.ContextHandler.doScope(ContextHandler.java:965)
    at org.seleniumhq.jetty7.server.handler.ScopedHandler.handle(ScopedHandler.java:117)
    at org.seleniumhq.jetty7.server.handler.HandlerWrapper.handle(HandlerWrapper.java:111)
    at org.seleniumhq.jetty7.server.Server.handle(Server.java:349)
    at org.seleniumhq.jetty7.server.AbstractHttpConnection.handleRequest(AbstractHttpConnection.java:452)
    at org.seleniumhq.jetty7.server.BlockingHttpConnection.handleRequest(BlockingHttpConnection.java:47)
    at org.seleniumhq.jetty7.server.AbstractHttpConnection.content(AbstractHttpConnection.java:894)
    at org.seleniumhq.jetty7.server.AbstractHttpConnection$RequestHandler.content(AbstractHttpConnection.java:948)
    at org.seleniumhq.jetty7.http.HttpParser.parseNext(HttpParser.java:857)
    at org.seleniumhq.jetty7.http.HttpParser.parseAvailable(HttpParser.java:235)
    at org.seleniumhq.jetty7.server.BlockingHttpConnection.handle(BlockingHttpConnection.java:66)
    at org.seleniumhq.jetty7.server.bio.SocketConnector$ConnectorEndPoint.run(SocketConnector.java:254)
    at org.seleniumhq.jetty7.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:599)
    at org.seleniumhq.jetty7.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:534)
    … 1 more

    • Tihomir Turzai

      Hi!

      Thanks for your comment on our blog. It’s hard to say what could be the issue here, but you can try the following:

      – turn off your firewall
      – make sure that you have direct access to the IP
      – make sure that you are running your eclipse and eclipse project with the same java version what you use for selenium server
      – if you want to run chrome browser, you need to add the chromedriver manually to your path variable

    • Diego Molina

      Hi,

      I am running into the same problem currently, did you find a solution?

  • Seddo

    Hey,

    thank you very much for this helpful article!
    You mentioned that we have to change the path variable in the advanced settings. When I do this, it’s not possible anymore to use Apache jMeter, which I want to use in parallel.
    So my question is: Do we necessarily have to change that path variable?

    Regards,
    Seddo

    • Tihomir Turzai

      Hi!

      Thanks for your comment on our blog. It could depend on which Java version do you use and how much Java instances do you have on your machine.
      If you are using JDK 7 and above it’s not necessary to put in the path variable. I tried out here on my machine (Win 7, JDK 1.7.0.79, without JAVA_HOME and path variable insertion) and it’ works like charm.
      A possible solution is to the java path in the jmeter batch file.

  • Alexandria

    wedoqa.com has potential, you can make your site go
    viral easily

    Your content is astonishing.

  • nitin raj

    Hi,
    As you mentioned above, I am trying to write the test suits, but at line getdriver().get(http://www.facebook.com); its showing error”create a method”.
    Please tell me how can i solve it.

  • Bhuvan

    I have single test with all the actions. I want to execute this test(code reusability) with multiple instances of same browser where I need to pass different credentials for each instance.

    Is this possible?
    if so, please guide me.

    Thanks in advance.

  • more information

    Hi mates, its fantastic paragraph concerning educationand completely
    explained, keep it up all the time.

  • Imran Babar

    Hi. I am just gonna start testing with selenium. Before I start I would like to know how will i test my web app with, say, 1000 user/load rather than just 3-5. Hiw do i ramp up the tests?

    • Tihomir Turzai

      Hi Imran,
      We recommend using JMeter for loads high as 100 or more.

      Selenium webdriver use actual browsers which will use up significant resources on your machine and the safe number of parallel test execution on an average machine is 5-6 browsers.
      You can try connect multiple machines together using grid, but it will still handle 5-6 browsers per machine, which can be quite costly.

  • Nicole Jess

    Hi mates,

    1) I am able to execute a class file well when Run as TestNG but not able to execute the same class file when placed in XML file and run as

    2) Moreover, I am able to execute when “Thread.Sleep” is placed but not able to execute when I use “Implicit Wait”

    Can you explain me the reasons for above two questions

  • Eran

    Hi,
    I use intellij and I try to use what you explained , i have 2 problems:
    1.it doesn’t recognize @BeforeMethod, can i use BeforeClass instead?
    2.when I try to run the testing.xml it gives me exception:
    Exception in thread “main” java.lang.NoClassDefFoundError: org/testng/TestNG
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:760)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:467)
    at java.net.URLClassLoader.access$100(URLClassLoader.java:73)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:368)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:362)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:361)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    at org.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:117)
    Caused by: java.lang.ClassNotFoundException: org.testng.TestNG
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    … 13 more

    help please

  • QA

    Hi. Thanks for this post. Had implemented the same before. Just wanted an extra clarification. How do we initializer page factory? I am getting null pointer exception by doing
    PageFactory.initElements(getDriver(),this);

  • Radhika

    Hi István,

    Your post has been very useful to me. But could you please tell me how I could replace firefox with phantomJs with this code?

  • Gopika

    Thank you for such a Great Explanation. I tried this and its really got work. You save my whole day. Thanks Again. I would like to visit your blog often. Keep Rocking!!!

  • haribabu

    Hi Tihomir,

    I am facing a problem with grid, as i am able to launch browser and navigating pages on websites,

    but when i try to given test data its only passed to firefox browsers as duplicate hyderabad hyderabad,

    my test data is : hyderabad, for locality text field,

    I tried with data provider too, but no use, Could you please please help me, Appropriated for your reply…

    • Dane Dukić

      Hi haribabu,
      I’m little confused with your problem, can you please provide part of your code when you send keys to certain field. If you use something like this:

      driver.findElement(By.id("someID")).sendKeys(Username);

      with data from excel file for example (Username is column title with different values under it), then problem might occur when data is read from the file.
      Regards

  • davismiller

    Needed to compose you a very little word to thank you yet again
    regarding the nice suggestions you’ve contributed here.

Comments are closed.