Example scenario : Let’s consider simple example. I wants to run 2 software automation test cases and both should run on 2 different browsers concurrently(Firefox and Google chrome) using selenium Grid. But my another condition is, Node should open max 2 browser at a time. In this case, only “maxInstances” will not works for me but i need to use maxSession” in node configuration. In this case i can set maxSession = 2 for node to restrict only 2 browsers at a time. Let’s understand how it works with practical example.
Note : Please remove Internet explorer related code stuff from bellow given script if face any related error as it is not very stable with selenium grid.
fillingForm.java
package Grid2;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.Platform;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
public class fillingForm {
// Used dataProvider parameter to get data from @DataProvider annotation method.
// Can accept object array data(browser name, First Name and Last Name) from getNames method.
@Test(dataProvider = "getNames")
public void gmailLogin(String browser, String fName, String lName) throws MalformedURLException, InterruptedException {
System.out.println(browser);
// Initialize DesiredCapabilities null.
DesiredCapabilities cap = null;
// Initialize browser driver as per data received from getNames().
if (browser.equals("firefox")) {
// Set firefox browser capabilities for windows platform.
cap = DesiredCapabilities.firefox();
cap.setBrowserName("firefox");
cap.setPlatform(Platform.WINDOWS);
} else if (browser.equals("chrome")) {
// Set chrome browser capabilities for windows platform.
cap = DesiredCapabilities.chrome();
cap.setBrowserName("chrome");
cap.setPlatform(Platform.WINDOWS);
} /*else if (browser.equals("iexplore")) {
// Set IE browser capabilities for windows platform.
cap = DesiredCapabilities.internetExplorer();
cap.setBrowserName("internet explorer");
cap.setPlatform(Platform.ANY);
cap.setVersion("8");
}*/
// Initialize RemoteWebDriver on grid 2 node with browser capability.
RemoteWebDriver driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), cap);
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
// Pause test for 20 minutes to check exactly how many concurrent browsers opening at same time.
Thread.sleep(20000);
// Open URL in requested browsers of node and execute test steps.
driver.get("http://only-testing-blog.blogspot.com/2014/05/form.html");
driver.findElement(By.name("FirstName")).sendKeys(fName);
driver.findElement(By.name("LastName")).sendKeys(lName);
// Close browser instance.
driver.quit();
}
// Created @DataProvider annotation method to supply data(browser name, First Name and Last Name) for test
@DataProvider(parallel = true)
public Object[][] getNames() {
Object data[][] = new Object[2][3];
data[0][0] = "firefox";
data[0][1] = "FirstName1";
data[0][2] = "LastName1";
data[1][0] = "chrome";
data[1][1] = "FirstName2";
data[1][2] = "LastName2";
/*
data[2][0] = "iexplore";
data[2][1] = "FirstName3";
data[2][2] = "LastName3";
*/
return data;
}
}
Calc.java
package Grid2;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.Platform;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.Assert;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
public class Calc {
// Used dataProvider parameter to get data from @DataProvider annotation method.
// Can accept object array data(browser name, num1, num2 and expected sum value) from getNames method.
@Test(dataProvider = "getCalcData")
public static void calcTest(String browser, String num1, String num2, String expSumNum) throws MalformedURLException, InterruptedException {
System.out.println(browser);
// Initialize DesiredCapabilities null.
DesiredCapabilities cap = null;
// Initialize browser driver as per data received from getCalcData().
if (browser.equals("firefox")) {
// Set firefox browser capabilities for windows platform.
cap = DesiredCapabilities.firefox();
cap.setBrowserName("firefox");
cap.setPlatform(Platform.WINDOWS);
} else if (browser.equals("chrome")) {
// Set chrome browser capabilities for windows platform.
cap = DesiredCapabilities.chrome();
cap.setBrowserName("chrome");
cap.setPlatform(Platform.WINDOWS);
} /*else if (browser.equals("iexplore")) {
// Set IE browser capabilities for windows platform.
cap = DesiredCapabilities.internetExplorer();
cap.setBrowserName("iexplore");
cap.setPlatform(Platform.ANY);
cap.setVersion("8");
}*/
// Initialize RemoteWebDriver on grid 2 node with browser capability.
RemoteWebDriver driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), cap);
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
// Pause test for 20 minutes to check exactly how many concurrent browsers opening at same time.
Thread.sleep(20000);
driver.get("http://only-testing-blog.blogspot.com/2014/04/calc.html");
driver.findElement(By.xpath("//input[@id='Resultbox']")).clear();
driver.findElement(By.xpath("//input[@id='" + num1 + "']")).click();
driver.findElement(By.xpath("//input[@id='plus']")).click();
driver.findElement(By.xpath("//input[@id='" + num2 + "']")).click();
driver.findElement(By.xpath("//input[@id='equals']")).click();
// Get actual result and compare with expected result.
String strResult = driver.findElement(By.xpath("//input[@id='Resultbox']")).getAttribute("value");
int actualResult = Integer.parseInt(strResult);
int expectedResult = Integer.parseInt(expSumNum);
Assert.assertEquals(actualResult, expectedResult);
// Close browser instance.
driver.quit();
}
// Created @DataProvider annotation method to supply data(browser name, num1, num2 and expected sum value) for test
@DataProvider(parallel = true)
public Object[][] getCalcData() {
Object data[][] = new Object[2][4];
data[0][0] = "firefox";
data[0][1] = "1";
data[0][2] = "3";
data[0][3] = "4";
data[1][0] = "chrome";
data[1][1] = "2";
data[1][2] = "5";
data[1][3] = "7";
/*
data[2][0] = "iexplore";
data[2][1] = "3";
data[2][2] = "5";
data[2][3] = "8";
*/
return data;
}
}
testng.xml
<suite name="My Test Suite" verbose="2" parallel="classes" thread-count="6">
<test name="Selenium Grid Test">
<classes>
<class name="Grid2.fillingForm" />
<class name="Grid2.Calc" />
</classes>
</test>
</suite>
First of all let’s run our software automation test without using “maxInstances” and then we will use “maxInstances” in node configuration to check how it works.
- Start grid 2 hub. You can view THIS POST to know how to start hub.
- Open command prompt and navigate to D: drive in command prompt where selenium server jar file, IEDriver server file and chromedriver file is stored.
- Start grid 2 node using bellow given command. You can view THIS POST to know prerequisite to start grid 2 node.
java -jar selenium-server-standalone-2.52.0.jar -role node -Dwebdriver.ie.driver="D:/IEDriverServer.exe" -Dwebdriver.chrome.driver="D:/chromedriver.exe" -hub http://localhost:4444/grid/register -port 5566 -browser browserName=firefox,maxInstances=2 -browser browserName=chrome,maxInstances=2 -browser browserName=iexplore,maxInstances=2
using “maxInstances”
- Close current running node using CTRL+c keys.
- Start node using bellow given command. Now we have used -maxSession 2 in bellow given command to limit max any 2 browser instances at a time.
java -jar selenium-server-standalone-2.52.0.jar -role node -Dwebdriver.ie.driver="D:/IEDriverServer.exe" -Dwebdriver.chrome.driver="D:/chromedriver.exe" -hub http://localhost:4444/grid/register -port 5566 -browser browserName=firefox,maxInstances=2 -browser browserName=chrome,maxInstances=2 -browser browserName=iexplore,maxInstances=2 -maxSession 2