In the previous lesson(How to run parallel tests with Selenium WebDriver and TestNG) we used Firefox browser to run the tests.
Now we introduce how to run the tests parallel in Chrome and in Internet Explorer browsers.

Firstly download the latest chromedriver and IEDriverServer files.
You can download the IEDriverServer file from the http://code.google.com/p/selenium/downloads/list site. My opinion is that use the 32-bit version, because the 64-bit version is in some cases are too slow.
The chromedriver download site is https://sites.google.com/a/chromium.org/chromedriver/downloads .
In this case my opinion is that use the chromedriver_win_26.0.xxxx version (this version is released in Jan 2013). There are newer versions, like chromedriver_win32_2.1 (Jul 9 2013) and maybe this version is good, but the previous versions, like 2.0, are not worked perfectly.

Put the downloaded and unzipped files to a folder.
In Advanced system settings/ Environment Variables update the Path:
In to the Path variable add:
–    The location of the two file (in my case):      C:\SeleniumDrivers;

If you want to use Chrome browser, follow the steps:
Now (as in the previous lesson) 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=chrome,maxInstances=3
Here is the code for it:


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

import org.openqa.selenium.WebDriver;
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();

dc.setBrowserName(DesiredCapabilities.chrome().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();
}
}

Now we didn’t used a profile, as in Firefox, just set the browser name.

If you want to use Internet Explorer, do the followings:
Open Internet Explorer, click to Tools-> Internet Options.
In Internet Options windows click to Security tab. In all four cases (Internet, Local intranet, Trusted sites, Restricted sites) check/or uncheck the Enable Protected Mode checkbox. The point is that be all the same.

Here is the code for IE:


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

import org.openqa.selenium.WebDriver;
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();

dc.setBrowserName("iexplorer");
threadDriver.set(new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), dc));
}

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

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

}
}

You can use the same test as used in (How to run parallel tests with Selenium WebDriver and TestNG) lesson, just rewrite the TestBase code with the previously shown codes. Or you can put all the codes in the TestBase and use a parameter and the if/else if functions to choose the right browser.
That’s it! Have fun 🙂

Similar Posts from the author:

10 thoughts to “How to run parallel tests with Selenium WebDriver and TestNG in Chrome browser and in Internet Explorer browser

  • Elease Schmerer

    Pretty much, Java is quite cool, annoying concerning what number of posts they’ve however awesome information!

    • István Lackó

      Thanks, glad you liked it.

  • Rusty Basher

    Your web page has plenty of good guidelines. Can I link to you with nofollow link?

    • István Lackó

      Thanks, of course you can link it.

  • Venkat

    I liked all your posts under selenium, coming to this post it works perfectly fine local machine. Can you please help me on how to do this exercise in remote machines using TestNG and WebDriver.

    Thanks in advance.

  • Sridhar

    Hi, I tried your code for ie and chrome. I am getting the exception, the path to driver executable must be set, though i have set it properly. Please help! Here is the code.

    File file = new File(“C:\Automation\Selenium\chromedriver.exe”);
    System.setProperty(“webdriver.chrome.driver”, file.getAbsolutePath());

    ThreadLocal threadDriver = null;

    threadDriver = new ThreadLocal();
    DesiredCapabilities dc = new DesiredCapabilities();

    dc.setBrowserName(DesiredCapabilities.chrome().getBrowserName());
    threadDriver.set(new RemoteWebDriver(new URL(“http://localhost:4444/wd/hub”), dc));
    threadDriver.get().get(“http://www.google.com”);

  • Sridhar

    Sorry, the code for initializing the thread local driver was not getting pasted properly because of the brackers . Here is the code for initializing the threadlocal driver. Please help.

    ThreadLocal threadDriver = null;

    threadDriver = new ThreadLocal ();

    • Tihomir Turzai

      Hi Sridhar!

      Thank you for reading and commenting our blog post.
      Did you add your folder with Selenium webdrivers to the PATH variable? I tried out your code and it works for me after I put the folder with drivers into PATH variable. I got the exact same error without this.

      Also please notice that when you add a new path to your PATH variable you need to restart your cmd.exe and your eclipse too.

  • raj

    Hi,

    I liked this post very much. I have one question. To run tests in parallel is it mandatory to use RemoteWebDriver along with Grid? TestNG itself run tests in parallel right? By that we can use Browser specific driver itself instead of RemoteWebDriver and Grid. What is the advantage of this approach over TestNG?

Comments are closed.