Friday, August 31, 2018

Set Selenium Grid Node Timeout When Running Webdriver Test

| Friday, August 31, 2018
"timeout" is node configuration parameter using which you lot tin set timeout for selenium grid node browser session. If you lot laid upwardly "-timeout 20000" as well as run test, If node browser non receiving whatsoever ascendency as well as rest ideal for 20 seconds then it volition endure closed automatically past times -timeout parameter. That agency it tells node browser -> "wait max xx seconds to have whatsoever ascendency else unopen browser as well as clear session". Lets accept practical representative to understand "timeout" clearly.

Earlier nosotros learnt usage of "maxInstances" in THIS POST and "maxSession" in THIS POST to configure selenium grid node hence i promise you lot are good aware close usage of both these parameters equally nosotros are going to utilization them inwards this representative too. Now let's practise 2 selenium software automation bear witness cases equally bellow as well as and hence elbow grease to run them without timeout as well as and hence 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;  populace shape fillingForm {   @Test(dataProvider = "getNames")  populace 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("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);    // Commented driver.quit(); to verify browser is existence closed automatically past times timeout or not.   // driver.quit();  }   @DataProvider(parallel = true)  populace 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;  populace shape Calc {   @Test(dataProvider = "getCalcData")  populace 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("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();    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 existence closed automatically past times timeout or not.   // driver.quit();  }   @DataProvider(parallel = true)  populace 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 hence 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 as well as also it should run max 2 browsers at a fourth dimension hence nosotros volition launch node amongst -maxSession 2. 
Running bear witness without timeout for node
First of all nosotros volition launch node without timeout parameter as well as execute higher upwardly bear witness on it to cheque node browser condition on completion of test.
  • Start grid hub equally described inwards THIS POST.
  • Open ascendency prompt as well as navigate to D: elbow grease inwards ascendency prompt where selenium server jolt file, IEDriver server file as well as chromedriver file is stored.
  • Start grid node with -maxSession 2 to trammel 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 upwardly bear witness from testng.xml file. It volition launch 2 browsers to run both software automation bear witness cases parallel. Other 2 pending requests on node volition hold back for electrical flow session to endure closed equally bellow. 


But hither you lot know, We conduct maintain commented driver.quit(); from both bear witness cases hence tests volition endure 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) hence remaining 2 tests volition rest inwards queue for five minutes equally no slot is complimentary on node to run test. When default timeout fourth dimension reached, both electrical flow driver instances volition endure closed as well as and hence remaining 2 requests volition kickoff execution past times launching novel driver instances on node.

Running bear witness amongst timeout for node
To reset node timeout, We tin utilization timeout parameter when launching node. You tin laid upwardly your desired timeout for node hence that it tin kickoff side past times side bear witness if driver instance rest steady for given time. Let's configure node amongst timeout to cheque how it plant inwards higher upwardly scenario.
  • Restart grid hub.
  • Restart grid node using bellow command. Here nosotros conduct maintain used -timeout 20000 (20 seconds) at the destination of command. You tin laid upwardly timeout equally per your requirement.
Note : Meaning of -timeout 20000 : During bear witness execution if browser volition remain steady for xx seconds, Node volition unopen it forthwith afterward 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 upwardly tests from testng.xml file. Both software automation tests volition endure executed, as well as hence browsers volition rest steady for xx seconds as well as and hence both of them volition endure closed automatically as well as novel requests volition kickoff execution on complimentary slots.

This way, node timeout tin aid you lot to unopen browser instance if it is stuck during bear witness execution.

Related Posts