Running tests in parallel

Now, we will turn our attention to the changes in the code. Let's start with testng.xml.

The suite tag needs to be changed, as shown here:

<suite name="Suite" parallel="tests">

This indicates that the test tags have to be executed in parallel. We are executing the same program, Driver_Script, three times, with different parameters for the browser name.

The next change has to be done at the place where we instantiate the various drivers. First of all, we create a DesiredCapabilities reference, static DesiredCapabilities capabilities;

The openBrowser(List<String> browserName) method should be changed; the code snippet of one if condition is shown as follows:

  public WebDriver openBrowser(List<String> browserName)
throws MalformedURLException {
if (browserName.get(0).equalsIgnoreCase("chrome")) {
log.info("Executing openBrowser");
System.setProperty("webdriver.chrome.driver",
System.getProperty("user.dir")
+ "\src\main\resources\chromedriver.exe");
try {
driver = new RemoteWebDriver(new URL(
"http://localhost:4444/wd/hub"),
DesiredCapabilities.chrome());
} catch (MalformedURLException e) {
e.printStackTrace();
}
} else if (browserName.get(0).equalsIgnoreCase("ie")) {
System.setProperty("webdriver.ie.driver",
System.getProperty("user.dir")
+ "\src\main\resources\IEDriverServer.exe");

capabilities.setBrowserName("IE");
try {
driver = new RemoteWebDriver(new URL(
"http://localhost:4444/wd/hub"),
DesiredCapabilities.internetExplorer());
} catch (MalformedURLException e) {
e.printStackTrace();
}
} else if (browserName.get(0).equalsIgnoreCase("firefox")) {
System.setProperty("webdriver.gecko.driver",
System.getProperty("user.dir")
+ "\src\main\resources\geckodriver.exe");
driver = new RemoteWebDriver(
new URL("http://localhost:4444/wd/hub"),
DesiredCapabilities.firefox());
}
return driver;
}
}

The capabilities object is instantiated to a Chrome capability. Next, we change the ChromeDriver constructor to RemoteWebDriver. The first argument to this is a URL object which accepts the HUB URL as the first argument and the capabilities object as the second argument.

Then, we execute the testng.xml by right clicking on Run As TestNG suite. Once the browser instances start opening, notice the changes that happen in the node console:

Notice how the Internet Explorer instances have become disabled. This indicates that we currently have one Chrome and one Internet Explorer session open.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.253.62