Selenium 简明教程
Selenium - TestNG
当我们使用 Selenium 或任何其他工具运行任何自动化测试时,我们需要查看和分析执行结果,以总结已经执行、通过、失败、失败数据等的测试数量,以报告的形式。
While we are running any automation tests using Selenium or any other tool, we will be required to view and analyze the results of the execution to conclude the number of tests which got executed, passed, failed, failure data, and so on in the form of reports.
有时,测试失败的屏幕截图也会被捕获到报告中。还需要定期与项目利益相关者分享测试报告。为此,我们可以借助 TestNG 报告。
Sometimes, the screenshots where the tests have undergone a failure are also captured in the reports. The test reports are also required to be shared with the project stakeholders on a periodic basis. For this purpose, we can take the help of the TestNG reports.
TestNG 报告是一个 html 报告,在测试用例在 TestNG 的帮助下构建并运行后自动生成。它是一个单元测试框架,可以与 Selenium 测试集成并用于报告目的。
A TestNG report is a html report generated automatically once a test case built and run with the help of the TestNG. It is a unit testing framework which can be integrated with Selenium tests and used for reporting purposes.
除此之外,TestNG 还有一个名为 Reporter 的默认报告类,它有助于记录。这有助于检测失败的根本原因以调试失败的测试。
In addition to this, the TestNG has a default reporting class called the Reporter which helps to log. This is useful in detecting the root cause of failure to debug the failed test.
Prerequisites to Create A TestNG Report
-
Install Java(version above 8) in the system and check if it is present with the command: java -version. The java version installed will be visible if installation has been completed successfully.
-
Install maven in the system and check if it is present with the command: mvn -version. The maven version installed will be visible if installation has been completed successfully.
-
Install any IDE like Eclipse, IntelliJ, and so on.
-
Add the TestNG dependencies from the link below: https://mvnrepository.com/artifact/.
-
Add the Selenium Java dependencies from the below link: selenium-java
-
Save the pom.xml with all the dependencies and update the maven project
What are the Different Ways to Generate TestNG Reports?
生成 TestNG 报告的不同方法如下所示 -
The difference ways to generate a TestNG report are listed below −
-
emailable-report.html
-
index.html report
-
Reporter class report
emailable-report.html
创建 emailable-report.html 的步骤如下 -
The steps to create an emailable-report.html are listed below −
Step 1 - 创建 TestNG 测试类,并实现以下示例,其中我们将首先单击 New User button 上的 Welcome Page 。
Step 1 − Create a TestNG test class with the implementation of the below example where we will first click on the New User button on the Welcome Page.
请注意,我们将在 testng.xml 文件中运行测试。
Please note, we will run the test through the testng.xml file.
单击 New User 按钮时,我们将导航到注册页面,其中包含 Welcome, Register 文本,如下面的图像所示。
On clicking the New User button, we will be navigating to the Registration page, having the Welcome, Register text as shown in the below image.
Example
package Report;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.*;
import java.util.concurrent.TimeUnit;
import static org.testng.Assert.assertEquals;
public class TestNGTest {
WebDriver driver;
@BeforeTest
public void setup() throws Exception{
// Initiate browser driver
driver = new ChromeDriver();
// adding implicit wait of 12 secs
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
// Opening the webpage
driver.get("https://www.tutorialspoint.com/selenium/practice/login.php");
}
@Test(priority = 1)
public void verifyWelcomePageHeading() {
// identify header then get text
WebElement header = driver.findElement
(By.xpath("//*[@id='signInForm']/h1"));
String text = header.getText();
// assertion to verify login page header
assertEquals("Welcome, Login In", text);
}
@Test(priority = 2)
public void moveToRegisterPage() {
// identify button then click
WebElement btn = driver.findElement
(By.xpath("//*[@id='signInForm']/div[3]/a"));
btn.click();
}
@Test(priority = 3)
public void verifyRegisterPageHeading() {
// identify header then get text
WebElement heder = driver.findElement
(By.xpath("//*[@id='signupForm']/h1"));
String text = heder.getText();
// assertion to verify register page header
assertEquals("Welcome,Register", text);
}
@AfterTest
public void teardown() {
// quitting browser
driver.quit();
}
}
testng.xml 文件中的配置。
Configurations in testng.xml file.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="All Test Suite">
<test verbose="2" preserve-order="true" name="TestNGTest.java">
<classes>
<class name="Report.TestNGTest">
<methods>
<include name="verifyWelcomePageHeading"/>
<include name="moveToRegisterPage"/>
<include name="verifyRegisterPageHeading"/>
</methods>
</class>
</classes>
</test>
</suite>
pom.xml 文件中的依赖项。
Dependencies in pom.xml file.
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>SeleniumJava</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>16</maven.compiler.source>
<maven.compiler.target>16</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
<dependencies>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.11.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.testng/testng -->
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.9.0</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
上述实现的项目结构如下图所示 −
Project structure of the above implementation is shown in the below image −
我们将通过 testng.xml 运行测试。
We will run the test through the testng.xml.
Output
===============================================
All Test Suite
Total tests run: 3, Passes: 3, Failures: 0, Skips: 0
===============================================
Process finished with exit code 0
我们借助 TestNG 测试框架创建了测试,并检索了页面标头并使用断言对它们进行了验证。
We had taken help of the TestNG test framework, to create tests and retrieved the page headers and verified them with assertions.
控制台中的结果显示 Total tests run: 3 ,因为有三个带 @Test 注释的方法 - verifyWelcomePageHeading()、moveToRegisterPage() 和 verifyRegisterPageHeading()。
The result in the console shows Total tests run: 3, as there are three methods with @Test annotations - verifyWelcomePageHeading(), moveToRegisterPage(), and verifyRegisterPageHeading().
最后,接收到消息 Passes: 3 和 Process finished with exit code 0 ,表示代码执行成功。
Finally, the message Passes: 3, and Process finished with exit code 0 was received, signifying successful execution of the code.
Step 2 - 刷新项目,项目结构中应该生成一个名为 test-output 的新文件夹。
Step 2 − Refresh the project and a new folder called the test-output should get generated in the project structure.
Step 3 - 右键单击 emailable-report.html 并选择在浏览器中打开的选项。
Step 3 − Right-click on the emailable-report.html and select the option to open in a browser.
报告将在浏览器中打开,显示测试类名称 - TestNGTest.java,其中包括已通过、已跳过、已失败的总数、测试持续时间等。此外,测试方法名称 moveToRegisterPage、verifyRegisterPageHeading 和 verifyWelcomePageHeading 也包含在报告中。
The report will be opened in the browser showing the test class name - TestNGTest.java with total number passed, skipped, failed, duration of test, and so on.. Also, the test method names moveToRegisterPage, verifyRegisterPageHeading, and verifyWelcomePageHeading are also included in the report.
index.html report
创建 index.html 报告的步骤如下 -
The steps to create an index.html report are listed below −
Step 1 − 按照创建 emailable-report.html 的步骤执行步骤 1。
Step 1 − Follow Step1 of the steps described to create emailable-report.html.
Step 2 - 刷新项目,项目结构中应该生成一个名为 test-output 的新文件夹。
Step 2 − Refresh the project and a new folder called the test-output should get generated in the project structure.
Step 3 − 右键单击 index.html 并选择在浏览器中打开的选项。
Step 3 − Right-click on the index.html and select the option to open in a browser.
报告将在浏览器中打开,显示 testng.xml、测试数量、组、时间、Reporter 输出、忽略的方法和时间顺序视图。此外,“Results”部分包含带有方法数量、通过和失败的方法名称 moveToRegisterPage、verifyRegisterPageHeading 和 verifyWelcomePageHeading。
The report will be opened in the browser showing the testng.xml, number of test, groups, Times, Reporter output, ignored methods, and Chronological view. Also, the Results section contains the test method names moveToRegisterPage, verifyRegisterPageHeading, and verifyWelcomePageHeading with the number of methods, passed, and failed.
Report Generated From Reporter Class
TestNG 提供 Reporter 类用于记录目的。它有四种不同的方法来处理用于记录的信息:
TestNG gives the Reporter class for logging purposes. It has four different ways to handle the information for logging −
-
Reporter.log( String str);
-
Reporter.log( String str, Boolean logToOut);
-
Reporter.log( String str, int l);
-
Reporter.log( String str, int l, Boolean logToOut);
下面列出了使用 Reporter 类在报告中生成日志消息的步骤:
The steps to generate log messages in report with Reporter class are listed below −
Step 1 - 创建 TestNG 测试类,并实现以下示例,其中我们将首先单击 New User button 上的 Welcome Page 。
Step 1 − Create a TestNG test class with the implementation of the below example where we will first click on the New User button on the Welcome Page.
请注意,我们将通过 testng.xml 文件运行测试。此外,一旦执行测试,我们将在报告中得到以下日志消息:
Please note, we will run the test through the testng.xml file. Also, once the test is executed, we will have the below log messages in the reports −
-
Moving to Registration Page
-
Verified Login Page Header
-
Verified Register Page Header
单击 New User 按钮时,我们将导航到注册页面,其中包含 Welcome, Register 文本,如下面的图像所示。
On clicking the New User button, we will be navigating to the Registration page, having the Welcome, Register text as shown in the below image.
请注意,我们将通过 testng.xml 文件运行测试。此外,一旦执行测试,我们将在报告中得到以下日志消息:
Please note, we will run the test through the testng.xml file. Also, once the test is executed, we will have the below log messages in the reports −
-
Moving to Registration Page
-
Verified Login Page Header
-
Verified Register Page Header
Example
package Report;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Reporter;
import org.testng.annotations.*;
import java.util.concurrent.TimeUnit;
import static org.testng.Assert.assertEquals;
public class TestNGTest {
WebDriver driver;
@BeforeTest
public void setup() throws Exception{
// Initiate browser driver
driver = new ChromeDriver();
// adding implicit wait of 12 secs
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
// Opening the webpage
driver.get("https://www.tutorialspoint.com/selenium/practice/login.php");
}
@Test(priority = 1)
public void verifyWelcomePageHeading() {
// identify header then get text
WebElement header = driver.findElement(By.xpath("//*[@id='signInForm']/h1"));
String text = header.getText();
// assertion to verify login page header
assertEquals("Welcome, Login In", text);
Reporter.log("Verified Login Page header");
}
@Test(priority = 2)
public void moveToRegisterPage() {
// identify button then click
WebElement btn = driver.findElement(By.xpath("//*[@id='signInForm']/div[3]/a"));
btn.click();
Reporter.log("Moving to Registration Page");
}
@Test(priority = 3)
public void verifyRegisterPageHeading() {
// identify header then get text
WebElement heder = driver.findElement(By.xpath("//*[@id='signupForm']/h1"));
String text = heder.getText();
// assertion to verify register page header
assertEquals("Welcome,Register", text);
Reporter.log("Verified Register Page header");
}
@AfterTest
public void teardown() {
// quitting browser
driver.quit();
}
}
Step 2 - 刷新项目,项目结构中应该生成一个名为 test-output 的新文件夹。
Step 2 − Refresh the project and a new folder called the test-output should get generated in the project structure.
Step 3 − 右键单击 emailable-report.html 和 index.html 以选择在浏览器中打开的选项。
Step 3 − Right-click on the emailable-report.html and index.html to select the option to open in a browser.
以下 emailable-report.html 报告应在浏览器中打开,显示 TestNGTest.java 测试类名称,以及通过、跳过、失败、测试持续时间等的总数。此外,报告中还包括 test 方法名称 moveToRegisterPage、verifyRegisterPageHeading 和 verifyWelcomePageHeading。
The below emailable-report.html report should open in the browser showing the test class name - TestNGTest.java with total number passed, skipped, failed, duration of test, and so on. Also, the test method names moveToRegisterPage, verifyRegisterPageHeading, and verifyWelcomePageHeading are also included in the report.
在报告的较低部分,可以看到记录消息以及 test 方法名称。
At the lower section in the report, the logging messages along with the test method names were visible.
以下 index.html 报告应在浏览器中打开,显示 testng.xml、测试数量、组、时间、Reporter 输出、忽略的方法和时间顺序视图。此外,“Results”部分包含带有方法数量、通过和失败的方法名称 moveToRegisterPage、verifyRegisterPageHeading 和 verifyWelcomePageHeading。
The below index.html report should open in the browser showing the testng.xml, number of test, groups, Times, Reporter output, ignored methods, and Chronological view. Also, the Results section contains the test method names moveToRegisterPage, verifyRegisterPageHeading, and verifyWelcomePageHeading with the number of methods, passed, and failed.
单击左侧的“Reporter output”选项卡时,可以看到记录消息以及 test 方法名称。
On clicking the Reporter output tab in the left, the logging messages along with the test method names were visible.
以下链接提供了 TestNG 的详细说明: TestNG
The below link provides a detailed description of TestNG: TestNG
到此,我们对 Selenium - TestNG Report 教程的全面介绍结束了。我们从描述 TestNG 报告开始,介绍了设置 TestNG 报告的先决条件,并逐步指导如何创建不同类型的 TestNG 报告,例如 emailable-report.html、index.html 和带有 Reporter 类的报告,并通过一个示例来说明如何将它们与 Selenium 一起使用。这让你对 TestNG 报告有了深入了解。明智的做法是不断练习你所学到的知识,并探索与 Selenium 相关的内容以加深你的理解和拓展你的视野。
This concludes our comprehensive take on the tutorial on Selenium - TestNG Report. We’ve started with describing a TestNG report, prerequisites to set up a TestNG report, and walked through steps to create a different types of TestNG reports like emailable-report.html, index.html, and reports with Reporter class with an example illustrating how to use them along with Selenium. This equips you with in-depth knowledge of the TestNG report. It is wise to keep practicing what you’ve learned and exploring others relevant to Selenium to deepen your understanding and expand your horizons.