【问题标题】:In Selenium, if a step fails for a test case, is it possible to just report the failure and continue with remaining steps?在 Selenium 中,如果测试用例的某个步骤失败,是否可以只报告失败并继续执行剩余步骤?
【发布时间】: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


【解决方案1】:

如果测试用例的步骤失败,是否可以只报告 失败并继续其余步骤?

简短回答:是的 长答案:是的

Selenium 是建立在 JUnit 或 TestNG 等测试引擎之上的框架。在这些引擎上,如果您什么都不做,该工具将解释为通过。换句话说,在没有断言的情况下,引擎将假定测试通过。由于 Selenium 是在此之上构建的,因此 Selenium 也是如此。下面的代码 sn-p 表示 Cucumber 步骤的样子。

@When("my test step here")
public void myTestStep(...) {
    boolean result = false;
    try {
            result = myTest(...);
        }
    } catch (Exception e) {
        // log your exception (don't rethrow)
    }
    if (result) {
        // log your passing test
    } else {
        // log your failing test
        Assert.fail(); // This is what prevents subsequent steps to be executed. Remove it, and you should be able to continue to test.
}

对于 JUnit 或 TestNG 风格的方法基本相同。你可能有一个@AfterClass@AfterTest 钩子可以处理告诉测试框架通过失败测试。 通常,传递断言是隐含的(不做任何事情 - 即执行一个空方法)。但是,失败的断言是显式的,必须包含在某处。只需查找那些 Assert.fail() 方法并将其删除。更好的选择是向您的测试套件添加一个可配置的属性,该属性将打开或关闭它。

    } else {
        // log your failing test
        if (skip_off) {
            Assert.fail(); // This is what prevents subsequent steps to be executed. Remove it, and you should be able to continue to test.
        }
}

在这种情况下,skip_off 是您可能存储在配置文件中的布尔属性的值,当设置为 true 时,它​​将跳过执行失败断言。

【讨论】:

    【解决方案2】:

    由于您使用的是 TestNG,因此请实现软断言

    public void VerifyTitle(String title)
    {
    
         SoftAssert assertion = new SoftAssert();
         String returnedTitle = driver.getTitle();
    
         if (assertion.assertTrue(returnedTitle.contains(title)))
         {
    
              Reporter.reportStep("Page is successfully loaded :" + title, "PASS");
    
         } else
         {
    
             Reporter.reportStep("Page Title :" + driver.getTitle() + " did not match with :" + title, "FAIL");
         }
    }
    

    如果这有帮助,请告诉我。

    【讨论】:

    • 如果返回布尔值和断言无效。所以那里出现错误 - 无法将 void 转换为布尔值
    猜你喜欢
    • 1970-01-01
    • 2020-08-16
    • 1970-01-01
    • 1970-01-01
    • 2014-11-05
    • 2018-06-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多