Reputation: 11
I'm trying to skip exception from a test method in testing. Using data provider to get data from Excel and based on a condition I want to skip tests, I'm throwing skip exception but these test cases are also shown as passed. My code is as follows:
@Test(dataProvider = "postProperty")
public static void postProperty(Object testCaseNo , Object propertyFor , Object testCaseFlag , Object propertyType , Object propertyCode){
int ppTestCaseNo = (int) testCaseNo;
String ppPropertyFor = propertyFor.toString();
String ppTestCaseFlag = testCaseFlag.toString().trim();
String ppPropertyType = propertyType.toString();
int ppPropertyCode = (int) (double) propertyCode;
boolean propertyIdFlag = true;
try {
if (ppTestCaseFlag.equalsIgnoreCase("Yes")){
GenericFunctions.excelDataForTestCase(ppTestCaseNo);
driver = new ChromeDriver();
Robot robot = new Robot();
String browserType = workSheet.getRow(ppTestCaseNo).getCell(5).getStringCellValue().trim();
switch (browserType) {
case "Deployment" :
driver.get("http://deployment.magicbricks.com/postproperty/post-property-for-sale-rent/residential-commercial");
break;
case "Live" :
driver.get("http://post.magicbricks.com/postproperty/post-property-for-sale-rent/residential-commercial");
break;
case "Staging" :
driver.get("http://staging.magicbricks.com/postproperty/post-property-for-sale-rent/residential-commercial");
break;
case "NewMB" :
driver.get("http://newMB.magicbricks.com/postproperty/post-property-for-sale-rent/residential-commercial");
break;
}
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.manage().window().maximize();
if (ppPropertyType.equalsIgnoreCase("Multistorey Apartment")){
boolean propertyIdPostedFlag = PostProperty.functionPostPropertyMultiStoreyApartment(ppPropertyType , ppPropertyCode ,driver , robot, ppTestCaseNo ,log);
Assert.assertEquals(propertyIdFlag, propertyIdPostedFlag);
}
else{
boolean propertyIdPostedFlag =PostProperty.functionPostPropertyOtherPropertyType(ppPropertyType , ppPropertyCode ,driver , robot, ppTestCaseNo ,log);
Assert.assertEquals(propertyIdFlag, propertyIdPostedFlag);
}
}
else if (ppTestCaseFlag.equalsIgnoreCase("No")){
throw new SkipException("Skipping test case for "+ppPropertyType);
}
} catch (Exception e) {
e.printStackTrace();
}
}
Upvotes: 1
Views: 1510
Reputation: 23805
Actually you are catching Exception
in your whole code after throwing SkipException
.
You know Exception
is a base class while SkipException
it's child, so if you use catch (Exception e)
, it will catch all the exception which would be throw in your code.
So If you want to throw SkipException
and catch all exception except this, you need to catch possible individual child exception which could be throw in your code instead of Exception
.
I'm giving you example with catching some possible exception which could be throw in your code :-
try {
// Your code here
throw new SkipException("Skipping test case for "+ppPropertyType);
}catch (WebDriverException e){
}catch (IllegalStateException e){
}......
Upvotes: 1
Reputation: 9058
You are catching the SkipException in the catch (Exception...) and just printing a stacktrace. Thus testNG has no idea about this exception being thrown.
Upvotes: 1