gpt4 book ai didi

testng - TestNG 中的硬断言和软断言

转载 作者:行者123 更新时间:2023-12-02 09:33:40 27 4
gpt4 key购买 nike

我在 TestNG 中尝试了一些使用硬断言和软断言的示例。虽然软断言按预期工作,但我遇到了硬断言的问题。

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

由于 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

最佳答案

主要区别在于:

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

SoftAssert - 在 @Test 期间收集错误(不抛出异常),如果您调用 org.testng.asserts.SoftAssert#assertAll@Test 的末尾,如果有任何异常,则抛出异常并再次继续下一个 @Test

关于testng - TestNG 中的硬断言和软断言,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30030995/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com