Reputation: 15
I'm automating a web application using Selenium-web driver. Once the script breaks at any point I want to stop execution of the code instead of continuing with the next method so that I can know where the automation is breaking. Could anyone please help me how can I do that.
Here is the snippet of the catch block(i.e. the exception I'm using now)
try {
//code
}
catch(Exception e){
System.out.println("Exception occurred *** " +e.getMessage());
}
Upvotes: 1
Views: 17650
Reputation: 61
that is my way control all exeptions.
create method which visible for all classes
protected void exeption(String variable){
driver.manage().timeouts().implicitlyWait(1, TimeUnit.SECONDS);
System.out.println(" ");
System.out.println("stop test ____ something your code - exeption ______");
System.out.println(" ");
System.out.println("Check Variables: " + variable);
System.out.println("____________________________________________");
driver.findElement(By.xpath("//ooppss"));
}
private void testName() {
String variable = "something";
if(1!=2){
exeption(variable);
}
}
Upvotes: 0
Reputation: 11443
Well, tests are supposed to be independent by design, so the fail of the one should not affect the others. If you stop the execution on the first failed test , then how do you know how many more errors you have in your code?
That's a kind of a general idea explaining why testng doesn't contain desired feature out of the box. You can, however, still get an advantage of test dependency mechanism provided by testng:
@Test
public void serverStartedOk() {}
@Test(dependsOnMethods = { "serverStartedOk" })
public void method1() {}
So you need to chain your methods this way to ensure execution is broken at the first fail.
Upvotes: 1
Reputation: 133
If I understand what you're asking, I would put this in the catch block to help you trace the problem.
catch(Exception e)
{
e.printStackTrace();
System.exit(1)
}
or alternatively you could put a print statement in each catch block that identifies which block failed.
ie.
try{
//make a cake
}
catch(CakeException e)
{
System.out.println("Failed at cake baking);
}
try{
//make a salad
}
catch(LettuceException e)
{
System.out.println("Failed at lettuce chopping);
}
Upvotes: 2