2017-07-26 29 views
0

在Selenium中,如果测试用例的某个步骤失败,是否可以仅报告失败并继续执行其余步骤?目前执行暂停,如果有例外。这是我的测试案例看什么喜欢 -在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"); 
    } 

} 
+0

只要捕获抛出的异常,如果其中一个步骤失败,请确保其他步骤不依赖于该失败的测试,因为它可能会改变结果。您可以尝试将测试分解为更易于管理的小型测试,这意味着如果其中一个测试失败,则其他测试可以继续,每个测试应该能够独立运行。 – redp

+0

我不是在谈论不同的TC。相同的测试用例,可以说,如果我的第4步失败,那么执行暂停,其余步骤将被跳过。我希望它报告第4步的失败并继续执行第5步。可能吗?目前,如果在第4步引发异常,它将被捕获并报告失败,但执行不会继续。 –

+0

TC仅用于UI验证。所以我不想运行停在中间只是一个元素没有在网页上找到。如果有任何不清楚的地方,请告诉我。 –

回答

0

由于您使用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"); 
    } 
} 

让我知道,如果这有助于。

+0

如果返回一个布尔值和断言void。所以我得到错误 - 不能隐藏无效布尔 –

+0

我没有尝试正常断言。并没有解决问题 –

相关问题