断言失败后如何继续测试执行?

How to continue test execution after assertion failed?

我知道这个问题是重复的。但我正在寻找昨天的结果。我对此没有任何解决方案.. 我正在使用 Selenium Webdriver 2.47.1 和 TestNG 进行自动化。在我的自动化脚本中,我有 12 组测试,我正在使用 TestNG 断言方法来比较预期结果和实际结果。我的代码格式如下...

@Test(priority = 6)
public void TestingeNote1() {
   cd.switchTo().frame("RTop");
   cd.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
   String TesteNote1 = cd.findElement(By.xpath("//table/tbody/tr[2]/td[5]")).getText();
   StringBuffer object1 = new StringBuffer(TesteNote1);
   String ActeNote1 = object1.substring(108);
   String ExpeNote1 = ex.getExcelValue(scenarioName, 75, 4);
   try {
       Assert.assertEquals(ExpeNote1, ActeNote1);
       ex.setExcelValue(scenarioName, 75, 8, "PASSED");
   }
   catch(Exception e) {
         ex.setExcelValue(scenarioName, 75, 8, "FAILED");
   }
   cd.switchTo().defaultContent();
}

一旦断言失败,测试脚本的执行就会停止。我也想在断言失败后继续执行。我也使用过 Verify(),它只是给出了通过的验证结果。但是上面的测试结果是Failed one.

我建议使用 try/finally 块。

。 . .

    try {
     //use IF condition to match Strings (ExpeNote1, ActeNote1)are equal 
     ex.setExcelValue(scenarioName, 75, 8, "PASSED");
     }
     catch(Exception e)
     {  ex.setExcelValue(scenarioName, 75, 8, "FAILED");}
     finally {  cd.switchTo().defaultContent();}

使用带有适当异常捕获器的 try catch 块。例如,当您尝试捕获正常异常时,在 catch 块中使用 exception,如果 DOM 中不存在该元素,则使用 NoSuchElementException 等...在您的情况下捕获您进入错误控制台的异常。方法如下 -

  try {
       Assert.assertEquals(ExpeNote1, ActeNote1);
       ex.setExcelValue(scenarioName, 75, 8, "PASSED");
   }
   catch(AssertionError e) {
       ex.setExcelValue(scenarioName, 75, 8, "FAILED");
   }

你的执行停止了,因为你没有捕捉到你的 assert 语句抛出的正确异常。我猜你得到了一个 AssertionError,如果没有替换你从上面的代码中得到的异常类型。希望这有帮助。

使用软断言。即使在一个断言失败后,它也会继续测试。

SoftAssert softAssert = new SoftAssert();
String ActualErrorMEssage = firstNameerrorXpath.getText;
String ActualErrorMEssage2 = secondNameNameerrorXpath.getText;
softAssert.assertEquals(ActualErrorMEssage,ExpectedErrorMEssage);
softAssert.assertEquals(ActualErrorMEssage2,ExpectedErrorMEssage);
softAssert.assertAll();