如果在测试运行期间任何测试步骤抛出异常(例如NoSuchElementException),我想失败一个testNG测试(@Test)。 (仅举例)
在当前场景中,我正在检查是否单击特定任务,然后某些文本出现在UI上的元素中。但是最近AUT在点击任务时出现网络错误,因此包含我正在验证的文本的元素没有出现在UI上。所以webdriver抛出了NoSuchElementException。但由于我的所有断言都在测试步骤的后期,TestNG将此测试标记为通过,因此也继续执行依赖测试用例。
如果测试中的所有步骤都没有执行或者抛出任何异常,有没有办法让测试失败?
以下是示例代码:
public class DummyTest {
@Test
public void testThis(){
try{
testThisSteps();
}catch(Exception e){
e.printStackTrace();
}
}
public void testThisSteps() throws Exception{
System.setProperty("webdriver.chrome.driver","C:\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("http://toolsqa.com/automation-practice-form/");
driver.manage().window().maximize();
driver.findElement(By.xpath("//h1[text()='Practice Automation']"));
int i = driver.findElements(By.name("firstname")).size();
Assert.assertTrue(i!=0,"Element does not exist even though page is loaded");
}
}
现在的问题是,如果webdriver在这种情况下无法找到元素,我故意在最大化代码之后遗漏了一些文本,它将抛出NoSuchElementExeception并且不会执行更多代码,导致断言没有得到评估和测试用例执行停止,进一步的测试用例依赖于此执行。这是执行结果: -
org.openqa.selenium.NoSuchElementException:没有这样的元素:无法定位元素:{“method”:“xpath”,“selector”:“// h1 [text()='Practice Automation']”} < / p>
***元素信息:{使用= xpath,值= // h1 [text()='练习自动化']
默认套件
总测试运行:1,失败:0,跳过:0
处理完成,退出代码为0
答案 0 :(得分:0)
这个问题实际上取决于OP所需的行为。
您的选择是: a)对未能在UI上显示的元素添加另一个断言 (可能不理想,因为您想要根据给定原因而不是单个元素验证您的测试,即成功登录而不是点击登录按钮&#39;
b)使用testng注释来控制流量和所需行为dependsOnMethods="yourMethodContainingTheElementThatOccassionallyAppears"
c)避免捕获异常,因此testng将失败该步骤(如果您也使用解决方案(b)将跳过其余的测试(以黄色表示)
d)捕获SeleniumNoEmentException,然后在try / catch块中对elementPresent进行断言。通过相应地调整依赖关系,您可以决定是希望脚本保留还是跳过其余步骤。
e)使用softAssertion(意味着如果元素将来不再出现,您的测试将失败但继续执行其余步骤
希望这有帮助!
答案 1 :(得分:0)
我怀疑测试中的代码被try-catch
块包围。如果您将throws Exception
添加到方法定义而不添加try-except
,那么当任何异常被抛出时,您可以将行为标记为Fail
,而不是Pass
来自Assertions
。
我尝试了以下代码抛出NoSuchElementException
(模拟你的问题):
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.Test;
import org.testng.annotations.Test;
import org.testng.asserts.Assertion;
public class SeleniumTests {
@Test()
public void testmethod() throws Exception{
WebDriver driver = new ChromeDriver();
driver.get("http://www.google.com");
driver.findElement(By.className("something")); // throws NoSuchElementException
Assert.assertEquals(true, false);
driver.quit();
}
}
此处,由于不存在类名"something"
的元素,因此抛出NoSuchElementException
。
Assert.assertEquals
未执行。
TestNG将其标记为Failed
。这是你期待的行为吗?
以下是完整的日志:
[TestNG] Running:
C:\Users\USER_HP_2013_03\AppData\Local\Temp\testng-eclipse--1064112655\testng-customsuite.xml
Starting ChromeDriver 2.25.426923 (0390b88869384d6eb0d5d09729679f934aab9eed) on port 10078
Only local connections are allowed.
Jan 04, 2017 5:10:03 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Attempting bi-dialect session, assuming Postel's Law holds true on the remote end
log4j:WARN No appenders could be found for logger (org.apache.http.client.protocol.RequestAddCookies).
log4j:WARN Please initialize the log4j system properly.
Jan 04, 2017 5:10:06 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: OSS
FAILED: testmethod
org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"class name","selector":"something"}
(Session info: chrome=55.0.2883.87)
(Driver info: chromedriver=2.25.426923 (0390b88869384d6eb0d5d09729679f934aab9eed),platform=Windows NT 6.1.7601 SP1 x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 42 milliseconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '3.0.1', revision: '1969d75', time: '2016-10-18 09:48:19 -0700'
System info: host: 'HP201303', ip: '192.168.56.1', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_73'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities [{applicationCacheEnabled=false, rotatable=false, mobileEmulationEnabled=false, networkConnectionEnabled=false, chrome={chromedriverVersion=2.25.426923 (0390b88869384d6eb0d5d09729679f934aab9eed), userDataDir=C:\Users\USER_H~1\AppData\Local\Temp\scoped_dir5260_26036}, takesHeapSnapshot=true, pageLoadStrategy=normal, databaseEnabled=false, handlesAlerts=true, hasTouchScreen=false, version=55.0.2883.87, platform=XP, browserConnectionEnabled=false, nativeEvents=true, acceptSslCerts=true, locationContextEnabled=true, webStorageEnabled=true, browserName=chrome, takesScreenshot=true, javascriptEnabled=true, cssSelectorsEnabled=true}]
Session ID: 92cbb27a874d7d2215eee51fc6a77819
*** Element info: {Using=class name, value=something}
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:216)
at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:168)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:635)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:368)
at org.openqa.selenium.remote.RemoteWebDriver.findElementByClassName(RemoteWebDriver.java:457)
at org.openqa.selenium.By$ByClassName.findElement(By.java:391)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:360)
at SeleniumTests.testmethod(SeleniumTests.java:14)
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.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:132)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:236)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:81)
===============================================
Default test
Tests run: 1, Failures: 1, Skips: 0
===============================================
===============================================
Default suite
Total tests run: 1, Failures: 1, Skips: 0
===============================================
[TestNG] Time taken by org.testng.reporters.JUnitReportReporter@35bbe5e8: 9 ms
[TestNG] Time taken by org.testng.reporters.SuiteHTMLReporter@2a18f23c: 12 ms
[TestNG] Time taken by org.testng.reporters.jq.Main@4ec6a292: 27 ms
[TestNG] Time taken by org.testng.reporters.EmailableReporter2@3f0ee7cb: 9 ms
[TestNG] Time taken by [FailedReporter passed=0 failed=0 skipped=0]: 4 ms
[TestNG] Time taken by org.testng.reporters.XMLReporter@66d33a: 8 ms
答案 2 :(得分:0)
尝试一下:
try {
driver.findElement(by.xpath("//h1[text()='Practice Automation']");
}catch (org.openqa.selenium.NoSuchElementException e){
assert.fail("the element I was attempting to location was not found.");
}
这是一种廉价的hacky方式,可用来做您想做的事情,但是它可以工作并且会触发一个断言。
您的try catch块也是测试未失败的原因。您正在通过捕获来处理测试中的失败
try{
testThisSteps();
}catch(Exception e){
// you caught the failure. No error will pop up because it was caught
e.printStackTrace();
}
抓住您实际上是在迫使测试通过。