Reputation: 21
I currently have a loop that goes through a total of 16 website URL's, each time checking for specific text within each home page. There's sometimes a case when a few of the websites takes longer than the specified amount of time to load, thereby failing and stopping execution. What I would like is for the loop to continue until completion, then Fail the entire test if there's at least one failure that occurred during execution and pass the entire test if there were no failures.
Below is the actual code being used to set and check the load time against. Please advise how to modify the below code such that I can get the desired result above?
public static Boolean isTextPresentAfterWaitOnServer(final String strStringToAppear, RemoteWebDriver rwd, String strLoc){
try{
Wait<WebDriver> wait = new FluentWait<WebDriver>(rwd)
.withTimeout(30, TimeUnit.SECONDS)
.pollingEvery(2, TimeUnit.SECONDS)
.ignoring(NoSuchElementException.class);
Boolean foo = wait.until(new ExpectedCondition<Boolean>() {
public Boolean apply(final WebDriver webDriver) {
return (webDriver.getPageSource().indexOf(strStringToAppear) >= 0);
}
});
return foo;
}
catch(TimeoutException e){
throw new RuntimeException("Could not find text " + strStringToAppear +" on server "+strLoc +" - " + e);
}
};
Upvotes: 2
Views: 610
Reputation: 1738
I think in your case simple Asser.fail(String message); is very helpful. Please try this code:
public static Boolean isTextPresentAfterWaitOnServer(final String strStringToAppear, RemoteWebDriver rwd, String strLoc){
(...)
}
catch(TimeoutException e){
throw new RuntimeException("Could not find text " + strStringToAppear +" on server "+strLoc +" - " + e);
}
};
and catch this RuntimeExecption in test method:
@Test
public void someTestMethod(){
try{
(...)
isTextPresentAfterWaitOnServer("strStringToAppear", rwd, "strLoc");
}catch(RuntimeException re){
Assert.fail(re.getMessage());
} //you can specify another exceptions
}
I use fail in JUnit and works fine (all test cases running). May be has same behavior in testNG.
Upvotes: 0
Reputation: 1525
I haven't used FluentWait so not sure if you can ignore the timeout exception as well like you are doing for nosuchelementexception. If it has then i guess you can ignore that as well. OR instead of raising the runtimeexception, create an errorCounter, keep incrementing it in the catch block and in finally block raise an exception based on its value.
Upvotes: 1