Set Selenium Grid Node timeout When Running WebDriver Test

"timeout" is node configuration parameter using which yous tin set timeout for selenium grid node browser session. If yous gear upwards "-timeout 20000" together with run test, If node browser non receiving whatever ascendance together with remain ideal for 20 seconds then it volition locomote closed automatically past times -timeout parameter. That way it tells node browser -> "wait max xx seconds to have whatever ascendance else unopen browser together with clear session". Lets accept practical instance to understand "timeout" clearly.

Earlier nosotros learnt usage of "maxInstances" in THIS POST and "maxSession" in THIS POST to configure selenium grid node therefore i promise yous are good aware almost usage of both these parameters equally nosotros are going to usage them inwards this instance too. Now let's exercise 2 selenium software automation evidence cases equally bellow together with therefore endeavour to run them without timeout together with therefore amongst timeout parameter.

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 course of pedagogy fillingForm {   @Test(dataProvider = "getNames")  world void gmailLogin(String browser, String fName, String lName) throws MalformedURLException, InterruptedException {      System.out.println(browser);      DesiredCapabilities cap = null;    if (browser.equals("firefox")) {    cap = DesiredCapabilities.firefox();    cap.setBrowserName("firefox");    cap.setPlatform(Platform.WINDOWS);   } else if (browser.equals("chrome")) {    cap = DesiredCapabilities.chrome();    cap.setBrowserName("chrome");    cap.setPlatform(Platform.WINDOWS);   }    RemoteWebDriver driver = novel RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), cap);   driver.manage().window().maximize();   driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);    driver.get(" ");   driver.findElement(By.name("FirstName")).sendKeys(fName);   driver.findElement(By.name("LastName")).sendKeys(lName);    // Commented driver.quit(); to verify browser is beingness closed automatically past times timeout or not.   // driver.quit();  }   @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";    provide 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 course of pedagogy Calc {   @Test(dataProvider = "getCalcData")  world static void calcTest(String browser, String num1, String num2, String expSumNum) throws MalformedURLException, InterruptedException {    System.out.println(browser);    DesiredCapabilities cap = null;    if (browser.equals("firefox")) {    cap = DesiredCapabilities.firefox();    cap.setBrowserName("firefox");    cap.setPlatform(Platform.WINDOWS);   } else if (browser.equals("chrome")) {    cap = DesiredCapabilities.chrome();    cap.setBrowserName("chrome");    cap.setPlatform(Platform.WINDOWS);   }    RemoteWebDriver driver = novel RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), cap);   driver.manage().window().maximize();   driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);    driver.get(" ");   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();    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);    // Commented driver.quit(); to verify browser is beingness closed automatically past times timeout or not.   // driver.quit();  }   @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";    provide 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>


Test Configuration
  • 2 Software Automation Test cases : fillingForm.java and Calc.java
  • driver.quit(); is commented from both software automation test cases therefore webdriver volition non unopen driver instance.
  • Both tests are configured to run inwards parallel using selenium grid 2.
  • Our aim is to run both software automation test cases inwards parallel inwards 2 browsers together with also it should run max 2 browsers at a fourth dimension therefore nosotros volition launch node amongst -maxSession 2. 
Running evidence without timeout for node
First of all nosotros volition launch node without timeout parameter together with execute higher upwards evidence on it to depository fiscal establishment gibe node browser condition on completion of test.
  • Start grid hub equally described inwards THIS POST.
  • Open ascendance prompt together with navigate to D: stimulate inwards ascendance prompt where selenium server jounce file, IEDriver server file together with chromedriver file is stored.
  • Start grid node with -maxSession 2 to bound 2 max browsers only at a time. timeout is non used. Launch node using bellow given command.
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 higher upwards evidence from testng.xml file. It volition launch 2 browsers to run both software automation evidence cases parallel. Other 2 pending requests on node volition hold off for electrical flow session to locomote closed equally bellow. 


But hither yous know, We own got commented driver.quit(); from both evidence cases therefore tests volition locomote executed successfully on both driver instances but driver instances volition non closed. It volition remain equally it is for five minutes (node default timeout period) therefore remaining 2 tests volition remain inwards queue for five minutes equally no slot is costless on node to run test. When default timeout fourth dimension reached, both electrical flow driver instances volition locomote closed together with therefore remaining 2 requests volition showtime execution past times launching novel driver instances on node.

Running evidence amongst timeout for node
To reset node timeout, We tin usage timeout parameter when launching node. You tin gear upwards your desired timeout for node therefore that it tin showtime adjacent evidence if driver instance remain steady for given time. Let's configure node amongst timeout to depository fiscal establishment gibe how it industrial plant inwards higher upwards scenario.
  • Restart grid hub.
  • Restart grid node using bellow command. Here nosotros own got used -timeout 20000 (20 seconds) at the goal of command. You tin gear upwards timeout equally per your requirement.
Note : Meaning of -timeout 20000 : During evidence execution if browser volition remain steady for xx seconds, Node volition unopen it similar a shot afterwards 20 seconds.


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 -timeout 20000

Now run higher upwards tests from testng.xml file. Both software automation tests volition locomote executed, therefore browsers volition remain steady for xx seconds together with therefore both of them volition locomote closed automatically together with novel requests volition showtime execution on costless slots.

This way, node timeout tin assistance yous to unopen browser instance if it is stuck during evidence execution.
More interesting articles here :Generation Enggelmundus Internet Marketing Tool here :Zeageat IM http://www.software-testing-tutorials-automation.com/

1 Comments

Post a Comment
Previous Post Next Post