Java 调用SoftAssertobj.assertAll()函数后,SoftAssert使我的所有测试方法失败

Java 调用SoftAssertobj.assertAll()函数后,SoftAssert使我的所有测试方法失败,java,selenium,selenium-webdriver,testng,assertion,Java,Selenium,Selenium Webdriver,Testng,Assertion,我正在自动化一个网站,我正在使用软断言来失败我的测试用例。但是现在我得到了所有testmethods的失败报告,这些方法都是在特定场景中失败的测试方法之后出现的。下面给出的是示例代码 //Function Call commFunction.backnavigation(driver, props,"Item",ExcelResult_Field, className,"CustomerPricing"); //Function Declaration publi

我正在自动化一个网站,我正在使用软断言来失败我的测试用例。但是现在我得到了所有testmethods的失败报告,这些方法都是在特定场景中失败的测试方法之后出现的。下面给出的是示例代码

    //Function Call 

    commFunction.backnavigation(driver, props,"Item",ExcelResult_Field, className,"CustomerPricing");

//Function Declaration

    public void backnavigation(WebDriver driver, Properties props,
            String MenuName, boolean TestStatus, String className,
            String MethodName) throws InterruptedException,
            EncryptedDocumentException, InvalidFormatException, IOException {



        Boolean bool_backButton = ValidationHelper.isElementPresent(driver,
                By.xpath(LocatorModule(props, "BackNavigationButton")));

        if (bool_backButton.equals(true)) {
            ExecutionHelper.ElementTobeClicked_Xpath(driver,
                    By.xpath(LocatorModule(props, "BackNavigationButton")));

          ListClassView = ExecutionHelper
                    .waitForElementVisible(
                            driver,
                            By.xpath("//ul[@id='settings_menu']//li/a[text()='"
                                    + MenuName
                                    + "']/ancestor::li/following-sibling::li[1]/ul/li"));

            if (ListClassView.equals(true)) {
                 writeResulttoExcel(TestStatus, className,
                        MethodName);
                  if(TestStatus!=true)
                  {
                        // softAssert.assertEquals(false, true,"TEST STATUS Fail--assert fail"); 
                      softAssert.fail("TEST STATUS Fail--assert fail");
                  }
            }

            else {
                Thread.sleep(3000);
                driver.findElement(
                        By.xpath(".//*[@id='settings_menu']/li[@class='left width_fluid']/a[text()='"
                                + MenuName + "']")).click();
                 writeResulttoExcel(false, className, MethodName);
                 //softAssert.assertEquals(false, true,"ListClassView FALSE--assert fail"); 
                   softAssert.fail("ListClassView FALSE--assert fail");
                 Thread.sleep(2000);
            }

            Thread.sleep(3000);
        }

        else {
            driver.navigate().back();
            Boolean ListClassView = ExecutionHelper
                    .waitForElementVisible(
                            driver,
                            By.xpath("//ul[@id='settings_menu']//li/a[text()='"
                                    + MenuName
                                    + "']/ancestor::li/following-sibling::li[1]/ul/li"));
            if (ListClassView.equals(true))
            {
                 writeResulttoExcel(false, className, MethodName);
                // softAssert.assertEquals(false, true,"BACK BUTTON FALSE LIST VIEW AVAILABLE--assert fail");

                   softAssert.fail("BACK BUTTON FALSE LIST VIEW AVAILABLE--assert fail");

            }

            else {
                Thread.sleep(3000);
                driver.findElement(
                        By.xpath(".//*[@id='settings_menu']/li[@class='left width_fluid']/a[text()='"
                                + MenuName + "']")).click();
                 writeResulttoExcel(false, className, MethodName);
            //   softAssert.assertEquals(false, true,"BACK BUTTON FALSE LIST VIEW NOT AVAILABLE--assert fail"); 
                 softAssert.fail("BACK BUTTON FALSE LIST VIEW NOT AVAILABLE--assert fail");

            }

            Thread.sleep(3000);
        }
       softAssert.assertAll();
    }
在这里,方法1应该在我的实际场景中失败,方法2应该通过。但是在调用softAssert.assertAll()之后;方法1和2都失败了。附加场景的TestNG报告。客户定价后的测试应该失败,但报告显示所有测试方法都失败。应该应用什么更改来解决此问题


在提供的代码中,您的@Test方法在哪里?您是否在@Test中调用预定义的反向导航方法?您在哪里创建了softAssert对象?下面的简单示例将帮助您

  public class SoftAsert
{
@Test
public void test()
{
    SoftAssert asert=new SoftAssert();
    asert.assertEquals(false, true,"failed");
    asert.assertEquals(0, 1,"brokedown");
    asert.assertAll();
}
}
请在@Test it self和@Test use assertAll结束时启动SoftAssert对象,以便它仅提供该特定测试的失败详细信息

谢谢,,
Murali

您需要在每个@Test中声明并初始化SoftAssert对象。但是,如果希望使用HardAssert,则无需在每个@Test中声明和初始化HardAssert。只需全局声明和初始化Assert(每个测试类一次)。

粘贴实际代码。如果调用同一个函数,则两者都将失败。此外,asserttrue只接受一个参数。@niharika_neo更新了实际代码。请查看一下