Usage Of "maxInstances" In Grid 2 To Set Allowed Max Number Of Browser Instances

"maxInstances" is configuration parameter which is used during selenium grid 2 node configuration. Using "maxInstances" in selenium grid 2, We can set "how many max number of same browser instances are allowed to open and run software test at same time". "maxInstances" tells node machine to not allow more than allowed instances on that specific machine at the same time. Lets discuss one example to cleat your doubts.

If you launch selenium grid 2 node with default configuration as described in THIS POST, It will set 1 IE, 5 Firefox and 5 chrome browsers instances on node machine as bellow.



With this config, you can run your software automation tests in parallel on 1 IE browser, 5 Firefox browsers and 5 chrome browser at a time. That means node is not capable to run your tests in 2 IE browsers or 6 Firefox browsers or 6 chrome browsers at a time. Let's see it practically to verify how it works if not set required browser instances on node.


Example Scenario:  I have two software automation test cases as bellow and i wants to execute both of them in parallel on 2 IE, 2 Google chrome and 2 Firefox browsers at the same time. Means it should open 6 browser at a time on node machine to execute both software automation test cases in all three browsers concurrently. Supposing i am using default node configuration (1 IE, 5 Chrome and 5 Firefox instances at a time) to run above scenario. It will start executing test in all 6 browser instances at the same time? No.. First it will launch 5 browser instances(1 IE, 2 Google chrome and 2 Firefox) only and execute test on them. Remaining 1 IE browser instance will be launched and executes test once previous IE instance completes test execution and getting closed. That means your test will not run on 2 IE browser instances at the same time using this node configuration.

Launch selenium grid hub as described in THIS POST and selenium grid node as described in THIS POST and then execute bellow given tests. 

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.WINDOWS);
  }

  // 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[3][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("internet explorer");
   cap.setPlatform(Platform.WINDOWS);
  }

  // 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[3][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="15">
 <test name="Selenium Grid Test">
  <classes>
   <class name="Grid2.fillingForm" />
   <class name="Grid2.Calc" />
  </classes>
 </test>
</suite>

Create above given 2 software automation tests in eclipse and observe its execution with above testng.xml file. It can launch 1 IE browser at a time but i wants to execute my both tests on 2 IE browsers concurrently.

To launch your above 2 software automation tests on 6 concurrent browser at same time, you need to restart your node with bellow given command where i have set maxInstances=2 for all three browser instances.
  • Close current node using CTRL+c key press
  • Run bellow given command to register node with hub with maxInstances=2 for all three browser instances.
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

  • maxInstances=2 : It will tell node to prepare 2 browsers instances for that specific browser.
Now access URL = http://localhost:4444/grid/console in browser. It will looks like bellow.



Rerun your testng.xml file using above node configuration. It will launch 6 browsers (2 FF, 2 Chrome and 2 IE) at a time and run both tests on all of them concurrently.

Note : You can set any value for maxInstances fro any browser. 
Example : -browser browserName=firefox,maxInstances=25 will tell node to set 25 firefox browser instances.

This way you can control max number of allowed browser instances of same browser using maxInstances configuration parameter of grid 2.

No comments:

Post a Comment