Selenium 试图通过抛出跳过异常从测试方法中跳过异常,但测试用例仍然通过
我试图在测试中跳过测试方法中的异常。使用数据提供程序从Excel获取数据,并基于我想要跳过测试的条件,我抛出跳过异常,但这些测试用例也显示为已通过。我的代码如下:Selenium 试图通过抛出跳过异常从测试方法中跳过异常,但测试用例仍然通过,selenium,testng-dataprovider,Selenium,Testng Dataprovider,我试图在测试中跳过测试方法中的异常。使用数据提供程序从Excel获取数据,并基于我想要跳过测试的条件,我抛出跳过异常,但这些测试用例也显示为已通过。我的代码如下: @Test(dataProvider = "postProperty") public static void postProperty(Object testCaseNo , Object propertyFor , Object testCaseFlag , Object propertyType , Object propert
@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();
}
}
您正在捕获catch(Exception…)中的SkipException,并只是打印一个stacktrace。因此testNG不知道抛出了这个异常。实际上,在抛出
SkipException
之后,您正在捕获整个代码中的异常
您知道Exception
是一个基类,而SkipException
是它的子类,因此如果您使用catch(Exception e)
,它将捕获代码中抛出的所有异常
因此,如果您想抛出SkipException
并捕获除此之外的所有异常,则需要捕获可能在代码中抛出的单个子异常,而不是exception
我给你举了一个例子,捕捉一些可能会在代码中抛出的异常:-
try {
// Your code here
throw new SkipException("Skipping test case for "+ppPropertyType);
}catch (WebDriverException e){
}catch (IllegalStateException e){
}......
不需要个别例外。在catch(Exception…)中,检查Exception是否为SkipException的实例。如果为真,则再次抛出。其他例外情况可根据需要处理want@Grasshopper是的,你完全正确??但这有意义吗?捕获然后再次抛出?我认为如果您知道可能的个别异常,最好使用个别消息单独处理它,以便更好地了解实际发生的异常…:)如果有效,请也这样做…)