Sometimes you lot volition guide maintain a instance where few of the @Test methods are taking real very long fourth dimension to consummate the execution or larn stuck due to the around reason. In this case, Supposing you lot wants to mark such @Test methods equally failed together with larn for executing next @Test method. TestNG has real good feature using which you lot tin john set fourth dimension period to hold off for a test to completely execute.
To larn this feature, You needs to set timeout on bear witness suite level In testng.xml file. Let's endeavor to Implement It practically alongside instance of selenium webdriver equally bellow. Create bellow given bear witness instance In eclipse. In this bear witness case, I guide maintain two @Test methods. In get-go @Test method I guide maintain used v seconds hold off fourth dimension (Thread.sleep(5000);) together with In second @Test method I guide maintain used i mo hold off fourth dimension (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; world shape timeoutTest { WebDriver driver; @BeforeTest world 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(" "); } @Test world void timeoutTestOne() throws InterruptedException { System.out.println("Executing timeoutTestOne."); //Wait for v 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 world void timeoutTestTwo() throws InterruptedException { System.out.println("Executing timeoutTestTwo."); //Wait for i 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 equally bellow. You tin john encounter that I guide maintain used 3000 milliseconds timeout (time-out="3000") on suite grade that agency If any @Test method volition accept to a greater extent than than 3000 milliseconds to consummate execution together with hence Immediately TestNG volition grade that @Test method equally failed together with volition larn to execute next @Test method. That means, Particular method tin john accept maximum iii seconds to consummate the execution otherwise It volition hold upwardly marked equally failed. Based on that, timeoutTestOne method volition neglect and timeoutTestTwo volition transcend when nosotros 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 consequence of higher upwardly bear witness instance volition looks similar bellow.
Also you lot tin john laid upwardly timeout on Individual @Test method equally bellow.
@Test(timeOut=3000) world void timeoutTestTwo() throws InterruptedException { //Test code }