Friday, August 31, 2018

Usage Of Maxsession Inwards Grid Two To Laid Upward Publish Of Max Session Of Browser Instances

| Friday, August 31, 2018
We learnt close "maxInstances" inward my PREVIOUS POST. "maxSession" is to a greater extent than or less other configuration parameter which helps to gear upwardly max allowed sessions to run at a fourth dimension on that specific node. Here session way publish of concurrent browsers inward price of all browser. It volition non allow that specific node to opened upwardly browser to a greater extent than than value of "maxSession". Let's sympathise alongside really uncomplicated instance scenario.
Example scenario : Let's see uncomplicated example. I wants to run two software automation attempt out cases as well as both should run on two dissimilar browsers concurrently(Firefox as well as Google chrome) using selenium Grid. But my to a greater extent than or less other status is, Node should opened upwardly max two browser at a time. In this case, only "maxInstances" volition non industrial plant for me but i take to exercise maxSession" inward node configuration. In this instance i tin gear upwardly maxSession = two for node to confine only two browsers at a time. Let's sympathise how it industrial plant alongside practical example.

Create bellow given software automation  test cases inward eclipse alongside given testng.xml file.

Note : Please take Internet explorer related code materials from bellow given script if facial expression upwardly whatever related fault every bit it is non really stable alongside 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;  world shape fillingForm {   // Used dataProvider parameter to acquire information from @DataProvider notation method.  // Can convey object array data(browser name, First Name as well as Last Name) from getNames method.  @Test(dataProvider = "getNames")  world void gmailLogin(String browser, String fName, String lName) throws MalformedURLException, InterruptedException {   System.out.println(browser);    // Initialize DesiredCapabilities null.   DesiredCapabilities cap = null;    // Initialize browser driver every bit per information 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 two node alongside browser capability.   RemoteWebDriver driver = novel RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), cap);   driver.manage().window().maximize();   driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);    // Pause attempt out for xx minutes to banking concern tally precisely how many concurrent browsers opening at same time.   Thread.sleep(20000);    // Open URL inward requested browsers of node as well as execute attempt out steps.   driver.get("aa/search?q=usage-of-maxinstances-in-grid-2-to-set");   driver.findElement(By.name("FirstName")).sendKeys(fName);   driver.findElement(By.name("LastName")).sendKeys(lName);    // Close browser instance.   driver.quit();  }   // Created @DataProvider notation method to provide data(browser name, First Name as well as Last Name) for test  @DataProvider(parallel = true)  world Object[][] getNames() {   Object data[][] = novel 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"; */   render 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;  world shape Calc {   // Used dataProvider parameter to acquire information from @DataProvider notation method.  // Can convey object array data(browser name, num1, num2 as well as expected total value) from getNames method.  @Test(dataProvider = "getCalcData")  world 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 every bit per information 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 two node alongside browser capability.   RemoteWebDriver driver = novel RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), cap);   driver.manage().window().maximize();   driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);    // Pause attempt out for xx minutes to banking concern tally precisely how many concurrent browsers opening at same time.   Thread.sleep(20000);    driver.get("aa/search?q=usage-of-maxinstances-in-grid-2-to-set");   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 consequence as well as compare alongside 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 notation method to provide data(browser name, num1, num2 as well as expected total value) for test  @DataProvider(parallel = true)  world Object[][] getCalcData() {   Object data[][] = novel 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"; */   render 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>

without using "maxInstances"
First of all let's run our software automation  test without using "maxInstances" as well as therefore nosotros volition exercise "maxInstances" inward node configuration to banking concern tally how it works.
  • Start grid two hub. You tin persuasion THIS POST to know how to inaugural of all hub.
  • Open ascendency prompt as well as navigate to D: campaign inward ascendency prompt where selenium server jolt file, IEDriver server file as well as chromedriver file is stored.
  • Start grid two node using bellow given command. You tin persuasion THIS POST to know prerequisite to inaugural of all grid two 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"
Now execute inward a higher house given 2 software automation  test cases using testng.xml file as well as detect attempt out execution sequence. It volition opened upwardly iv browsers at the same fourth dimension as well as execute both attempt out cases parallel inward all iv browsers.
  • Close electrical current running node using CTRL+c keys.
  • Start node using bellow given command. Now nosotros stimulate got used -maxSession two inward bellow given ascendency to bound max whatever two 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

Now run testng.xml file to execute inward a higher house given 2 software automation  test cases as well as detect attempt out execution sequence. It volition opened upwardly only two browsers at a fourth dimension as well as two requests volition hold off for slot to travel costless every bit shown inward bellow given image. You tin meet it past times refreshing console page when your tests inaugural of all execution.

Once previous two requests volition consummate execution as well as unopen browser instance, Remaining two requests volition travel executed past times opening novel two browser instances. Means y'all volition meet max two browser instances at a fourth dimension on your grid node machine to execute tests.

This way, maxSession volition gear upwardly max allowed browsers at a fourth dimension to execute attempt out on grid node.

Related Posts