webdriver - 在 TestNG 中自定义故障报告

标签 webdriver automated-tests testng selenium-webdriver

背景:

我创建了一个基本的 Playground 项目,其中包含:

  1. 一个包含以下内容的 testLogin.java 文件:
    一种。 testng 包导入 (org.testng.*)
    b. selenium webdriver 导入 (org.openqa.selenium.*)
    C。 5 个带有 testng 注释的测试方法:
  1. @Test(groups={"init"}) public void openURL()
    Contains webdriver code to initiate the webdriver and open a chrome >instance with a given url.

  2. @Test(dependsOnGroups={"init"}) public void testLogin()
    Contains webdriver code to:
    1. Locate username password text-input elements, enter the username password from a properties file.
    2. Locate the "log in" button and click the button to log-in
    3. Manage a login-forcefully scenario if someone else has already logged in using the credentials.

  3. @Test(dependsOnMethods={"testLogin"}) public void testPatientsScheduleList()
    Contains webdriver code to check if any patients have been scheduled. If yes, then fetch the names and display in console.

  4. @Test() public void testLogout()
    Contains webdriver code to locate the logout button and click on the button to logout of the app.

  5. @AfterTest() public void closeConnection()
    Contains webdriver code to dispose the webdriver object and close the chrome instance.

目前我只是运行包装为来自 ANT 的 testng 方法的测试脚本,并生成一个 testng-xslt 报告。

问题:

<强>1。对测试方法中 webdriver 脚本的每一行代码执行验证:

我知道:
1. Selenium webdriver 脚本包含 API 方法(findElement() 和其他方法),这些方法由于执行默认断言/验证而抛出异常。当测试方法失败时,这些异常会显示在生成的报告中。
2. TestNG 提供了具有许多断言方法的 Assert 类,但我还没有弄清楚如何使用它们对 webdriver 脚本的每一行代码执行验证/断言。我尝试在每行 webdriver 脚本代码之后添加断言方法。输出中出现的只是测试方法的 AssertionError 异常。

<强>2。由于 try..catch block 而导致通过的某个测试方法失败。
如果我在一组 2 个或更多测试驱动脚本步骤周围使用 try catch block ,并且如果测试用例在任何步骤(脚本行)中失败,则 try..catch block 会处理它,从而显示测试方法在执行报告中显示为“通过”,但实际上失败了。

<强>3。创建一个自定义报告,它将显示所需的测试执行结果而不是堆栈跟踪!
当我执行上面的脚本时,会生成一个 testng-xslt 报告,其中包含测试套件中每个测试方法的通过/失败状态(在 testng.xml 中配置)。 测试结果只告诉我测试方法是通过还是失败,并提供异常的堆栈跟踪,实际上不提供任何有用的信息。 我不想要这种抽象级别的测试执行结果,而是类似:

Name | Started | Duration | What-really-went-wrong (Failure)


任何人都可以就以下方面提出建议/给出一些指示:
1. 如何在测试方法中对 webdriver 脚本的每一行代码执行验证/断言,而无需在每行脚本行之后编写断言?
2.由于try catch block ,我如何使某个测试方法失败?
3. 如何自定义失败报告,以便我可以发送失败结果,例如“预期元素“button”的 ID 为“bnt12”,但在测试方法的第 3 步未找到该元素”到 testng 的报告实用程序?
4. 在 testng-xslt 报告中,我想显示测试方法中发生故障的确切位置。因此,例如,如果我的测试方法由于测试方法第 3 行的 webelement = driver.findElement() 而失败,我想在“真正出错的地方”的测试报告中显示此问题柱子。我已经阅读了有关 testng teSTListeners TestListenerAdapter/ITestListener/IReporter 的信息,但在检查了 testng 的 javadoc 后我不明白如何使用它们。
5. 此外,完成自定义测试报告后,我必须实现 PageObject 模式。在页面对象模式中执行断言的正确位置在哪里?断言应该写在页面对象测试方法中还是写在将使用 PageObject 类的更高级别的测试方法中?

P.S:我对 testng 框架和 webdriver 脚本完全陌生。文中如有技术错误或观察错误,敬请谅解。

最佳答案

How can I perform validation/assertion against every line of code of webdriver script in a test-method without writing asserts after every script line?

我不这么认为。进行比较的是断言。所以你需要它。

How can I fail a certain test method which gets passed due to try catch block?

try-catch 将屏蔽断言失败。(因为在断言失败时,会抛出断言异常,因此如果您的 catch block 类似于 (catch(Exception e))捕获 block 。

How can I customize the failure reporting so that I can send a failure result like "Expected element "button" with id "bnt12" but did not find the element at step 3 of test-method" to testng's reporting utility?

您需要使用测试监听器。 TestNG TestListenerAdapter 是一个好的开始

Also, I have to implement PageObject pattern once I am done with customizing the test report. Where would be the right place to perform assertions in a page-object pattern? Should assertions be written in the page object test methods or in the higher level test methods that will use the PageObject classes?

我个人的选择是在测试方法中使用断言,因为这是我们进行实际测试的地方。页面对象包含用于在网页内导航的脚本。

关于webdriver - 在 TestNG 中自定义故障报告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14353570/

相关文章:

multithreading - 使用Spock测试线程并发

selenium - 如何在数据提供程序循环之外制作 @test 注释?

java - Selenium 网络驱动程序 : Java: NoSuchElementException: Unable to locate element: {"method" :"xpath" ,"selector" :"//div[@id=' manage_area']/ul/li/div[2]"}

java - Selenium2 和 webdriver 的一个很好的工作示例

linux - WebDriver Selenium API : FirefoxDriver on linux?

tomcat - 部署项目war时出现Tomcat错误

javascript - NightwatchJS "freezes"

python - 无法从 python 搜索标签,但可以在 chrome 中查看

validation - 从数据输入到验证,在测试之间传递变量的最佳方法是什么?

java - TestNg xml 显示没有错误 - 但在运行时它给出了运行总测试数 : 0