SUBSCRIBE HERE TO GET POST UPDATES VIA EMAIL :

Delivered by FeedBurner

TestNG Timeout Test For Selenium WebDriver

Sometimes you will have a case where few of the @Test methods are taking very very long time to complete the execution or get stuck due to the some reason. In this case, Supposing you wants to mark such @Test methods as failed and go for executing next @Test method. TestNG has very good feature using which you can set time period to wait for a test to completely execute.

To get this feature, You needs to set timeout on test suite level In testng.xml file. Let's try to Implement It practically with example of selenium webdriver as bellow. Create bellow given test case In eclipse. In this test case, I have two @Test methods. In first @Test method I have used 5 seconds wait time (Thread.sleep(5000);) and In second @Test method I have used 1 second wait time (Thread.sleep(1000);) Intentionally.


timeoutTest.java
package Testing_Pack;

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class timeoutTest {
 
 WebDriver driver; 

 @BeforeTest
 public void setup() throws Exception {
  System.out.println("In @BeforeTest Of Test_One.");
  driver =new FirefoxDriver();     
  driver.manage().window().maximize();
  driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
  driver.get("http://only-testing-blog.blogspot.in/2014/04/calc.html");
 }
 
 @Test
 public void timeoutTestOne() throws InterruptedException {
  System.out.println("Executing timeoutTestOne.");
  //Wait for 5 seconds.
  Thread.sleep(5000);
  driver.findElement(By.xpath("//input[@id='2']")).click();
  driver.findElement(By.xpath("//input[@id='plus']")).click();
  driver.findElement(By.xpath("//input[@id='6']")).click();
  driver.findElement(By.xpath("//input[@id='equals']")).click();
  String Result = driver.findElement(By.xpath("//input[@id='Resultbox']")).getAttribute("value");
  System.out.println("Result of timeoutTestOne = "+Result);
 }
 
 @Test
 public void timeoutTestTwo() throws InterruptedException {
  System.out.println("Executing timeoutTestTwo.");
  //Wait for 1 second.
  Thread.sleep(1000);
  driver.findElement(By.xpath("//input[@id='Resultbox']")).clear();
  driver.findElement(By.xpath("//input[@id='3']")).click();
  driver.findElement(By.xpath("//input[@id='plus']")).click();
  driver.findElement(By.xpath("//input[@id='7']")).click();
  driver.findElement(By.xpath("//input[@id='equals']")).click();
  String Result = driver.findElement(By.xpath("//input[@id='Resultbox']")).getAttribute("value");
  System.out.println("Result of timeoutTestTwo = "+Result);
 }
}

Create testng.xml file as bellow. You can see that I have used 3000 milliseconds timeout (time-out="3000") on suite level that means If any @Test method will take more than 3000 milliseconds to complete execution then Immediately TestNG will mark that @Test method as failed and will go to execute next @Test method. That means, Particular method can take maximum 3 seconds to complete the execution otherwise It will be marked as failed. Based on that, timeoutTestOne method will fail and timeoutTestTwo will pass when we execute testng.xml file.

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Timeout Test Suite" time-out="3000" verbose="1">
  <test name="Timeout Test" >
    <classes>
      <class name="Testing_Pack.timeoutTest"/>      
    </classes>
  </test>
</suite>

Execution result of above test case will looks like bellow.



Also you can set timeout on Individual @Test method as bellow.
@Test(timeOut=3000)
public void timeoutTestTwo() throws InterruptedException {
 //Test code
}

No comments:

Post a Comment