TestNG timeout:
================
The “timeout” means if a unit test is taking longer than a specified number of milliseconds to finish, TestNG will abort it and market it as failed.
This “timeout” can also use for performance test, to ensure the method is returned within a reasonable time.
TestNG allows user to configure a time period to wait for a test to completely execute. Timeout can be configured in two ways:
1.At suite level: This will be applicable for all the tests in the said TestNG test suite
2.At each test method level: This will be applicable for the said test method and will override the time period if configured at the suite level
To specify timeout duration, use “timeOut” attribute of @Test annotation.
@Test(timeout=800)
Example on method level:
-----------------------------------
package testngdemo;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;
public class TimeoutSuiteDemo {
WebDriver webDriver;
@Test(timeOut = 150)
public void testScript1() throws InterruptedException {
System.setProperty("webdriver.chrome.driver",
"D:\\Ramesh\\ChromeDriver.exe");
webDriver = new ChromeDriver();
// webDriver=new FirefoxDriver();
webDriver.manage().window().maximize();
webDriver.manage().deleteAllCookies();
webDriver.manage().timeouts().implicitlyWait(45, TimeUnit.SECONDS);
webDriver.get("https://google.com");
webDriver.findElement(By.name("q")).sendKeys("selenium by ramesh");
webDriver.findElement(By.name("btnG")).click();
Thread.sleep(3000);
webDriver.findElement(By.linkText("Selenium By Ramesh Anapati"))
.click();
webDriver.quit();
}
@Test(timeOut = 150)
public void testScript2() throws InterruptedException {
System.out.println("test script2");
}
}
Example of timeout at suite level:
------------------------------------------
TimeoutSuiteDemo.java:
--------------------------------
package testngdemo;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;
public class TimeoutSuiteDemo {
WebDriver webDriver;
@Test
public void testScript1() throws InterruptedException
{
System.setProperty("webdriver.chrome.driver", "D:\\Ramesh\\ChromeDriver.exe");
webDriver = new ChromeDriver();
//webDriver=new FirefoxDriver();
webDriver.manage().window().maximize();
webDriver.manage().deleteAllCookies();
webDriver.manage().timeouts().implicitlyWait(45, TimeUnit.SECONDS);
webDriver.get("https://google.com");
webDriver.findElement(By.name("q")).sendKeys("selenium by ramesh");
webDriver.findElement(By.name("btnG")).click();
Thread.sleep(3000);
webDriver.findElement(By.linkText("Selenium By Ramesh Anapati")).click();
webDriver.quit();
}
@Test
public void testScript2() throws InterruptedException
{
System.out.println("test script2");
}
}
================
The “timeout” means if a unit test is taking longer than a specified number of milliseconds to finish, TestNG will abort it and market it as failed.
This “timeout” can also use for performance test, to ensure the method is returned within a reasonable time.
TestNG allows user to configure a time period to wait for a test to completely execute. Timeout can be configured in two ways:
1.At suite level: This will be applicable for all the tests in the said TestNG test suite
2.At each test method level: This will be applicable for the said test method and will override the time period if configured at the suite level
To specify timeout duration, use “timeOut” attribute of @Test annotation.
@Test(timeout=800)
Example on method level:
-----------------------------------
package testngdemo;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;
public class TimeoutSuiteDemo {
WebDriver webDriver;
@Test(timeOut = 150)
public void testScript1() throws InterruptedException {
System.setProperty("webdriver.chrome.driver",
"D:\\Ramesh\\ChromeDriver.exe");
webDriver = new ChromeDriver();
// webDriver=new FirefoxDriver();
webDriver.manage().window().maximize();
webDriver.manage().deleteAllCookies();
webDriver.manage().timeouts().implicitlyWait(45, TimeUnit.SECONDS);
webDriver.get("https://google.com");
webDriver.findElement(By.name("q")).sendKeys("selenium by ramesh");
webDriver.findElement(By.name("btnG")).click();
Thread.sleep(3000);
webDriver.findElement(By.linkText("Selenium By Ramesh Anapati"))
.click();
webDriver.quit();
}
@Test(timeOut = 150)
public void testScript2() throws InterruptedException {
System.out.println("test script2");
}
}
Example of timeout at suite level:
------------------------------------------
TimeoutSuiteDemo.java:
--------------------------------
package testngdemo;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;
public class TimeoutSuiteDemo {
WebDriver webDriver;
@Test
public void testScript1() throws InterruptedException
{
System.setProperty("webdriver.chrome.driver", "D:\\Ramesh\\ChromeDriver.exe");
webDriver = new ChromeDriver();
//webDriver=new FirefoxDriver();
webDriver.manage().window().maximize();
webDriver.manage().deleteAllCookies();
webDriver.manage().timeouts().implicitlyWait(45, TimeUnit.SECONDS);
webDriver.get("https://google.com");
webDriver.findElement(By.name("q")).sendKeys("selenium by ramesh");
webDriver.findElement(By.name("btnG")).click();
Thread.sleep(3000);
webDriver.findElement(By.linkText("Selenium By Ramesh Anapati")).click();
webDriver.quit();
}
@Test
public void testScript2() throws InterruptedException
{
System.out.println("test script2");
}
}
testng.xml:
--------------
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Time test Suite" time-out="500" >
<test name="Test">
<classes>
<class name="testngdemo.TimeoutSuiteDemo"/>
</classes>
</test> <!-- Test -->
</suite> <!-- Suite -->
No comments:
Post a Comment