Set Selenium Grid Node "timeout" When Running WebDriver Test

"timeout" is node configuration parameter using which you can set timeout for selenium grid node browser session. If you set "-timeout 20000" and run test, If node browser not receiving any command and stay ideal for 20 seconds then it will be closed automatically by -timeout parameter. That means it tells node browser -> "wait max 20 seconds to receive any command else close browser and clear session". Lets take practical example to understand "timeout" clearly.

Earlier we learnt usage of "maxInstances" in THIS POST and "maxSession" in THIS POST to configure selenium grid node so i hope you are well aware about usage of both these parameters as we are going to use them in this example too. Now let's create two selenium software automation test cases as bellow and then try to run them without timeout and then with 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;

public class fillingForm {

 @Test(dataProvider = "getNames")
 public 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 = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), cap);
  driver.manage().window().maximize();
  driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

  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);

  // Commented driver.quit(); to verify browser is being closed automatically by timeout or not.
  // driver.quit();
 }

 @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";

  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 {

 @Test(dataProvider = "getCalcData")
 public 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 = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), cap);
  driver.manage().window().maximize();
  driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

  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();

  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 being closed automatically by timeout or not.
  // driver.quit();
 }

 @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";

  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>


Test Configuration
  • 2 Software Automation Test cases : fillingForm.java and Calc.java
  • driver.quit(); is commented from both software automation test cases so webdriver will not close driver instance.
  • Both tests are configured to run in parallel using selenium grid 2.
  • Our aim is to run both software automation test cases in parallel in 2 browsers and also it should run max 2 browsers at a time so we will launch node with -maxSession 2. 
Running test without timeout for node
First of all we will launch node without timeout parameter and execute above test on it to check node browser status on completion of test.
  • Start grid hub as described in THIS POST.
  • 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 node with -maxSession 2 to restrict 2 max browsers only at a time. timeout is not 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 above test from testng.xml file. It will launch 2 browsers to run both software automation test cases parallel. Other 2 pending requests on node will wait for current session to be closed as bellow. 


But here you know, We have commented driver.quit(); from both test cases so tests will be executed successfully on both driver instances but driver instances will not closed. It will remain as it is for 5 minutes (node default timeout period) so remaining 2 tests will stay in queue for 5 minutes as no slot is free on node to run test. When default timeout time reached, both current driver instances will be closed and then remaining two requests will start execution by launching new driver instances on node.

Running test with timeout for node
To reset node timeout, We can use timeout parameter when launching node. You can set your desired timeout for node so that it can start next test if driver instance stay steady for given time. Let's configure node with timeout to check how it works in above scenario.
  • Restart grid hub.
  • Restart grid node using bellow command. Here we have used -timeout 20000 (20 seconds) at the end of command. You can set timeout as per your requirement.
Note : Meaning of -timeout 20000 : During test execution if browser will remain steady for 20 seconds, Node will close it immediately after 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 above tests from testng.xml file. Both software automation tests will be executed, then browsers will stay steady for 20 seconds and then both of them will be closed automatically and new requests will start execution on free slots.

This way, node timeout can help you to close browser instance if it is stuck during test execution.

1 comment:

  1. If you are debugging a test this 20 seconds "idle" will guarantee that as a developer if you don't send any commands on the wire while you are at a breakpoint, that your test will die? I'm pretty sure that this will make reproducing issues for devs, harder than it needs to be. Work out why your tests are hanging, or why your app is hanging, otherwise all you are doing is swapping the fuses in the fusebox in your house out, as quickly as they blow. A page taking >20 seconds wont cause this, sure, but it's not the only tactic. Nobody really wins.

    ReplyDelete