当前位置: 首页 > 知识库问答 >
问题:

在评估断言之前失败testNG@test

祁嘉言
2023-03-14

如果在测试运行期间,任何测试步骤抛出一个异常(如NosuChelementException),我希望testNG测试(@test)失败。(仅举个例子)

在当前场景中,我正在检查是否单击了特定的任务,然后在UI上的元素中出现了一些文本。但是最近AUT在单击任务时抛出了一个网络错误,因此包含我正在验证的文本的元素没有出现在UI上。因此webdriver引发了NosuChelementException但是,由于我的所有断言都在测试步骤后面的某个地方,TestNG将此测试标记为pass,因此继续执行依赖的测试用例。

那么,如果没有执行测试中的所有步骤,或者抛出任何异常,那么有没有一种方法可以使测试失败呢?

下面是示例代码:

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无法找到element,在这种情况下,我特意在最大化代码之后省略了一些文本,它将抛出NoSuchElementExeception,并且不会执行更多的代码,这会导致断言无法得到评估,并且测试用例的执行会停止,而更多的测试用例依赖于该元素的执行。下面是执行结果:-

org.openqa.selenium.nosuchelementException:没有这样的元素:找不到元素:{“method”:“XPath”,“selector”:“//h1[text()='practice Automation']”}

***元素信息:{using=XPath,value=//h1[text()='实践自动化']

默认套件

运行得测试总数:1,失败数:0,跳过数:0

进程已完成,退出代码为0

共有1个答案

刘奇
2023-03-14

我怀疑测试里面的代码,被try-catch块包围了。如果将throwsexception添加到方法定义中,而没有try-exception,则可以获得标记为fail的测试方法的行为,而不是在抛出任何异常时pass

我尝试了以下抛出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将其标记为失败。这是你所期待的行为吗?

以下是完整的日志:

[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
 类似资料:
  • 我尝试使用“evaluate()”在量角器中消除角变量并得到值。如何验证 但是它说 打字错误:“厨房”不是一个可读的。 < code>kitchen是< code>angularObj.type中的值 如何在量角器中断言值?

  • 我是JMeter和断言概念的新手。当我试图执行一个JMX文件(包含JMeter中的断言)时,遇到了以下错误消息: 断言错误:错误断言失败:真断言失败消息:测试失败:变量(搜索结果)不相等/接收:找不到[[[]]]]比较:找不到[[[]]]]] 脚本的执行方式如下:$java-jar./apache-jmeter-2.10/bin/apachejmeter.jar-t./jmeter-master/

  • 我目前在一个项目中使用TestNG启动Selenium测试套件,并处理maven项目中所有结果的日志记录,该项目在Jenkins的工作中每晚运行。 当前,当一个测试失败了一个断言时,TestNG会杀死我们正在运行的所有东西,它不仅会使当前测试失败,还会继续运行套件中的其他测试。我不明白为什么TestNG不只是使当前的测试失败,然后启动剩余的尚未运行的测试。这使得TestNG在这一点上成为一个无用的

  • 我正在努力学习树视图。但是,在运行代码时,我在执行一行时得到了一个错误- 可能的原因是什么? 编辑:当我在view类中编写AddressContentProvider类时,它工作得非常好。但是,我在其他文件中编写这个类,它不起作用:(

  • 我在flutter上使用firebase做了一个简单的婴儿名字项目,在成功完成教程后,该应用程序出现了这样的错误:“package:firebase_demo/main.dart':Failed assertion:line 86 pos 16:'map['votes]!=null':is not true。” 同样的帮助。

  • 如果断言失败,我如何在TestNG中继续测试执行?如何在TestNG的HTML报告中报告失败? 当我运行以下代码时,断言后的行将被执行,但在报告中没有列出断言失败: