【发布时间】:2018-01-01 10:29:29
【问题描述】:
在 Selenium 中,如果测试用例的某个步骤失败,是否可以只报告失败并继续执行剩余步骤?当前,如果出现异常,则执行停止。这就是我的测试用例的样子-
public class TC002_abc extends OpentapWrappers
{
@Test (description="Test")
public void main()
{
try
{
WebDriverWait wait=new WebDriverWait(driver, 60);
VerifyTitle(Constant.HomePage_Title);
Click(HomePage.link_Login(driver), "Login Link");
wait.until(ExpectedConditions.urlContains(Constant.LoginURL));
VerifyTextPopulated(CommunicationPref.lbl_EmailAddress_Input(driver), Constant.EmailAddress);
/* Validate Email Communications */
Click(CommunicationPref.link_EditEmailCommunications(driver),"Edit Email Communications");
VerifyText(CommunicationPref.lbl_UnCheckedEmailCommunications(driver), Constant.UnCheckedEmailCommunications_Text);
Click(CommunicationPref.btn_EmailCommunicationsSave(driver), "Save");
VerifyText(CommunicationPref.lbl_CheckedEmailCommunications(driver), Constant.CheckedEmailCommunications_Text);
}
catch (NoSuchElementException e)
{
e.printStackTrace();
Reporter.reportStep("NoSuchElementException" , "FAIL");
}
}
@BeforeClass
public void beforeClass()
{
browserName="firefox";
testCaseName = "TC002_abc";
testDescription = "Test";
}
}
样本方法-
public static void VerifyTitle(String title){
try
{
if (driver.getTitle().equalsIgnoreCase(title))
{
Reporter.reportStep("Page is successfully loaded :"+title, "PASS");
}
else
Reporter.reportStep("Page Title :"+driver.getTitle()+" did not match with :"+title, "FAIL");
}
catch (Exception e)
{
e.printStackTrace();
Reporter.reportStep("The title did not match", "FAIL");
}
}
【问题讨论】:
-
只需捕获抛出的异常,如果其中一个步骤确实失败,请确保其他步骤不依赖于该失败的测试,因为它可能会改变结果。您可以尝试将测试拆分为更易于管理的较小测试,这意味着如果一个测试失败,其他测试可以继续,每个测试应该能够独立运行。
-
我不是在谈论不同的 TC。相同的测试用例,假设我的第 4 步失败,执行将在那里停止,其余步骤将被跳过。我希望它报告第四步的失败并继续执行第五步。可能吗?目前,如果在第 4 步抛出异常,则会被捕获并报告失败,但不会继续执行。
-
TC 仅用于 UI 验证。所以我不希望运行在中间停止,只是因为页面上没有找到其中一个元素。如果有什么不清楚的地方请告诉我。
-
你需要在每个方法(步骤)周围放置 try catch 块,而不是整个测试
-
在测试中移除 try catch,不需要它以及导致测试结束的原因
标签: java selenium automation ui-automation