How to continue test execution after assertion failed?

被刻印的时光 ゝ 提交于 2020-01-13 18:00:54

问题


I know that this question is duplicate one. But I am searching for the result from yesterday. I didn't got any solution for that.. I am using Selenium Webdriver 2.47.1 & TestNG for automation. In my automation script I have 12 set of tests & I am using TestNG Assert method to compare Expected Result & Actual Result. My code format is given below...

@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();
}

Execution of test script stops once assertion got failed. I want to continue the execution after assertion fail also. I have used Verify() also, It just gives the verify result as passed. But the above test result is Failed one.


回答1:


I'd recommend using a try/finally block.

. . .

    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();}



回答2:


Use try catch block with proper exception catcher. For example when you try to catch a normal exception use exception in the catch block, if the element is not present in the DOM then use NoSuchElementException etc... In your case catch the exception that you are getting in your error console. Here's how -

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

Your execution stops because you are not catching the proper exception that your assert statement throws. I guess you are getting an AssertionError, if not replace the exception type you get from your code above. Hope this helps.




回答3:


Use soft asserts. It will continue test even after one assertion failed.

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


来源:https://stackoverflow.com/questions/32694542/how-to-continue-test-execution-after-assertion-failed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!