TestNG中的硬和软断言

时间:2015-05-04 13:07:38

标签: testng

我在TestNG中尝试了一些软硬断言的例子。而softassertions正如预期的那样工作,我面临着硬断言的问题。

根据我从互联网上读取的信息 - 如果硬断言失败,则不会执行其他测试。

由于ffPageElementsTestHardAssert()失败,因此应该跳过

以下的code-printTest3()测试。有人可以帮助我理解这种行为。谢谢

@BeforeTest
  public void beforeTest() {
      DOMConfigurator.configure("log4j.xml");  

       TestBase.startBrowser("firefox","http://newtours.demoaut.com/index.php");
       Assert.assertEquals(TestBase.getBasePageTitle(), "Welcome: Mercury Tours");
       driver.manage().window().maximize();         
       APP_LOGS.info("Page displayed");  
       lp = PageFactory.initElements(driver, LoginPage.class);
       ffPage = lp.doLogin("test", "test");
       sa = new SoftAssert();
  }  


  @Test(priority = 2)
  public void ffPageElementsTestHardAssert() {
     Assert.assertTrue(ffPage.ff_Type_oneway.isDisplayed());
     Assert.assertTrue(ffPage.ff_Type_roundtrip.isDisplayed());
     Assert.assertTrue(ffPage.ff_PassengerCount.isDisplayed());
     Assert.assertTrue(ffPage.ff_DepartingFrom.isDisplayed());
     Assert.assertTrue(ffPage.ff_DepartingMonth.isDisplayed());
     Assert.assertTrue(ffPage.ff_DepartingDay.isDisplayed());
     Assert.assertTrue(ffPage.ff_ArrivingIn.isDisplayed());
     Assert.assertTrue(ffPage.ff_ReturningMonth.isDisplayed());
     Assert.assertTrue(ffPage.ff_ReturningDay.isDisplayed());
     Assert.assertTrue(ffPage.ff_ServiceClass_EC.isDisplayed());
     Assert.assertTrue(ffPage.ff_ServiceClass_BC.isDisplayed());
     Assert.assertTrue(ffPage.ff_ServiceClass_FC.isDisplayed());
     Assert.assertTrue(ffPage.ff_Airline.isDisplayed());
     Assert.assertTrue(ffPage.ff_Continue.isDisplayed());   
  }


  @Test(priority = 1)
  public void printTest1(){
      Assert.assertEquals(5, 5);
  }

  @Test(priority = 3)
  public void printTest3(){
      System.out.println("Hello3");
  }



   Output:
    -------
    Hello3
    PASSED: printTest1
    PASSED: printTest3
    FAILED: ffPageElementsTestHardAssert
    org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"name","selector":"airlin"}
    Command duration or timeout: 65 milliseconds
    For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
    Build info: version: '2.43.1', revision: '5163bce', time: '2014-09-10 16:27:58'
    System info: host: 'PSHYS0091', ip: '10.233.18.60', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.7.0_51'
    Session ID: da04681e-4918-4d75-94ee-ff590464c7c5
    Driver info: org.openqa.selenium.firefox.FirefoxDriver
    Capabilities [{platform=WINDOWS, databaseEnabled=true, cssSelectorsEnabled=true, javascriptEnabled=true, acceptSslCerts=true, handlesAlerts=true, browserName=firefox, webStorageEnabled=true, nativeEvents=true, rotatable=false, locationContextEnabled=true, applicationCacheEnabled=true, takesScreenshot=true, version=31.6.0}]
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
        at java.lang.reflect.Constructor.newInstance(Unknown Source)
        at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:204)
        at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:156)
        at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:599)
        at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:352)
        at org.openqa.selenium.remote.RemoteWebDriver.findElementByName(RemoteWebDriver.java:425)
        at org.openqa.selenium.By$ByName.findElement(By.java:299)
        at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:344)
        at org.openqa.selenium.support.pagefactory.DefaultElementLocator.findElement(DefaultElementLocator.java:59)
        at org.openqa.selenium.support.pagefactory.internal.LocatingElementHandler.invoke(LocatingElementHandler.java:37)
        at com.sun.proxy.$Proxy8.isDisplayed(Unknown Source)
        at com.nt.tests.FlightFinderTest.ffPageElementsTestHardAssert(FlightFinderTest.java:74)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.lang.reflect.Method.invoke(Unknown Source)
        at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:84)
        at org.testng.internal.Invoker.invokeMethod(Invoker.java:714)
        at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:901)
        at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1231)
        at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127)
        at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111)
        at org.testng.TestRunner.privateRun(TestRunner.java:767)
        at org.testng.TestRunner.run(TestRunner.java:617)
        at org.testng.SuiteRunner.runTest(SuiteRunner.java:334)
        at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329)
        at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)
        at org.testng.SuiteRunner.run(SuiteRunner.java:240)
        at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
        at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
        at org.testng.TestNG.runSuitesSequentially(TestNG.java:1224)
        at org.testng.TestNG.runSuitesLocally(TestNG.java:1149)
        at org.testng.TestNG.run(TestNG.java:1057)
        at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111)
        at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204)
        at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175)
    Caused by: org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"name","selector":"airlin"}
    For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
    Build info: version: '2.43.1', revision: '5163bce', time: '2014-09-10 16:27:58'
    System info: host: 'PSHYS0091', ip: '10.233.18.60', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.7.0_51'



    ===============================================
        Default test
        Tests run: 3, Failures: 1, Skips: 0
    ===============================================


    ===============================================
    Default suite
    Total tests run: 3, Failures: 1, Skips: 0

1 个答案:

答案 0 :(得分:5)

主要区别是:

HardAssert - 立即抛出AssertException,测试被标记为失败,测试套件继续下一个@Test

SoftAssert - 在@Test期间收集错误(不会抛出异常),如果您在 org.testng.asserts.SoftAssert#assertAll @Test >如果有任何和测试套件再次继续下一个@Test

,则抛出异常