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

时间:2016-03-03 07:04:15

标签: java selenium selenium-webdriver testng assertion

我正在自动化一个网站,我正在使用软断言来使我的测试用例失败。但是现在我得到了在特定情况下失败的测试方法之后出现的所有测试方法的失败报告。以下是示例代码

    //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和方法1 2失败了。附加方案的TestNG报告。测试在客户定价失败但报告显示所有测试方法失败后。应该应用哪些更改来解决此问题。?

TestNG Report Image File

2 个答案:

答案 0 :(得分:0)

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

Class A
{ 
  public:
    A();
    A(int x , int y);
  Private:
    int x;
    int y;
}

Class B
{
   public:
   B();
   A getApointerobject() const;

   Private:
   A *APointerObject;

int main()
{ 
  B bObj;
    cout << bObj.getApointerobject(); //i overloaded the << so that i can //output B objects but it crushes  

}

//Class B implementation (This is where i struggle)

  A getApointerobject() const { 

   return *getApointerobject;
}

请在@Test中自行启动SoftAssert对象,并在@Test结束时使用assertAll,以便它仅提供该特定测试的失败详细信息。

谢谢你, 穆拉利

答案 1 :(得分:0)

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

相关问题