Selenium 简明教程
Selenium - Allure
Allure 可用于创建详细的自动化测试报告。它是一个开源框架,可以与 Selenium 测试集成并用于报告目的。
Allure can be used to create a detailed automation test report. It is an open source framework which can be integrated with Selenium tests and used for reporting purposes.
Prerequisites to Create Allure Report
在系统中安装 Java(版本高于 8),并使用命令:java -version 检查是否存在 Java。如果安装成功,将显示已安装的 Java 版本。Java 可以使用链接 Java Downloads 下载并安装。
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. Java can be downloaded and installed using the link Java Downloads.
要获取有关如何设置 Java 的更多知识,请参阅以下链接 -
To get more knowledge on setting up of Java, refer to the below link −
在系统中安装 maven,并使用命令:mvn -version 检查是否存在 maven。如果安装成功,将显示已安装的 maven 版本。Maven 可以使用链接 Apache Maven 下载并安装。
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. Maven can be downloaded and installed using the link Apache Maven.
要获取有关如何设置 Maven 的更多知识,请参阅链接 Maven Environment Setup 。
To get more knowledge on setting up of Maven, refer to the link Maven Environment Setup.
安装 IntelliJ。要获取有关如何设置 IntelliJ 的更多知识,请参阅链接 Selenium IntelliJ 。
Install IntelliJ. To get more knowledge on setting up of IntelliJ, refer to the link Selenium IntelliJ.
Steps to Create Allure Report
Step 1 − 创建一个 maven 项目,并为 pom.xml 文件添加以下项目的依赖 −
Step 1 − Create a maven project and add the dependencies to the pom.xml file for the below items −
-
Add the TestNG dependencies from the link TestNG.
-
Add the Selenium Java dependencies from the link Selenium Java
-
Add the Allure TestNG dependencies from the link Allure TestNG
-
Save the pom.xml with all the dependencies and update the maven project.
Step 2 − 在系统中下载 Allure。对于 Mac 和 Linux,运行命令: brew install allure 。在系统中安装 Homebrew 后,这应该能起作用。对于 Windows,运行命令: scoop install allure 。在系统中安装 Scoop 后,这应该能起作用。使用命令检查 Allure 是否存在:allure –version。如果安装成功,将显示已安装的 Allure 版本。
Step 2 − Download the Allure in the system. For Mac, and Linux, run the command: brew install allure. This should work provided Homebrew is installed in the system. For Windows, run the command: scoop install allure. This should work provided Scoop is installed in the system. Check if Allure is present with the command: allure –version. The Allure version installed will be visible if installation has been completed successfully.
Step 3 − 创建一个 TestNG 测试类,负责以下示例。在 Welcome Page 上单击 New User button 。
Step 3 − Create a TestNG test class taking care of the below example. Click on New User button on the Welcome Page.

单击 New User 后,我们将看到“注册”页面,页面中带有 Welcome, Register 文本,如下图所示。
After clicking New User, we will have the Registration page, with Welcome, Register text as shown in the below image.

Code Implementation
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 AllureTest {
WebDriver driver;
@BeforeTest
public void setup(@Optional String browser) 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="AllureTest.java">
<classes>
<class name="Report.AllureTest">
<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>
<!-- https://mvnrepository.com/artifact/io.qameta.allure/allure-testng -->
<dependency>
<groupId>io.qameta.allure</groupId>
<artifactId>allure-testng</artifactId>
<version>2.25.0</version>
</dependency>
</dependencies>
</project>
上述实现的项目结构如下图所示 −
Project structure of the above implementation is shown in the below image −

Step 4 − 使用 testng.xml 运行测试。
Step 4 − Run the test using the testng.xml.
它将显示以下内容 output :
It will show the following output −
===============================================
All Test Suite
Total tests run: 3, Passes: 3, Failures: 0, Skips: 0
===============================================
Process finished with exit code 0
我们使用 TestNG 框架来设计测试并获取页面标题,最后对它们进行验证。
We had used the TestNG framework, to design the test and obtained the page headers, finally verified them.
控制台中的结果显示 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 5 − 刷新项目文件夹,名为 allure-results 的新文件夹应该会生成。
Step 5 − Refresh the project folder and a new folder called the allure-results should get generated.

Step 6 − 从项目文件夹位置运行命令: allure serve 。此处,项目文件夹名称为 Selenium Java。运行命令后,服务器会启动,同时 Allure 报告将在浏览器中打开,其中测试方法(或用例)总数为 3,通过率为 100。
Step 6 − Run the command: allure serve from the project folder location. Here, the project folder name is Selenium Java. After running the command, a server gets started, along with that the Allure report will be opened in the browser with the total number of test methods(or cases) as 3, having the pass percentage as 100.

单击左侧的 Suites 选项卡,我们将获得有关测试方法 moveToRegisterPage()、verifyPageHeading() 和 verifyWelcomePageHeading() 的执行持续时间和结果的信息。
On clicking the Suites tab on the left, we will get the information about the duration of execution for the test methods - moveToRegisterPage(), verifyPageHeading(), and verifyWelcomePageHeading() and their results.

移至左侧的 Graphs 选项卡,我们将获得有关状态、严重性、持续时间等的所有信息。
On moving to the Graphs tab on the left, we will get all information about STATUS, SEVERITY, DURATION, and so on.

Conclusion
这里是对 Selenium Allure 教程的全面介绍的结尾。我们首先描述了 Allure 报告、设置 Allure 报告的先决条件,并逐步指导使用示例说明了如何与 Selenium 结合使用来创建 Allure 报告。这让你深入了解 Allure。明智的做法是坚持实践你所学的内容,并探索其他与 Selenium 相关的知识,以加深理解并拓展视野。
This concludes our comprehensive take on the tutorial on Selenium Allure. We’ve started with describing an Allure report, prerequisites to set up an Allure report, and walked through steps to create an Allure report with an example illustrating how to use it along with Selenium. This equips you with in-depth knowledge of the Allure. It is wise to keep practicing what you’ve learned and exploring others relevant to Selenium to deepen your understanding and expand your horizons.