Selenium 简明教程
Selenium - JUnit Report
Junit 可用于创建详细的自动化测试报告。它是一个开源框架,可以与 Selenium 测试集成并用于报告目的。
Junit 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 JUnit Report
-
Install Java (version above 8) in the system from the link Java Downloads. To get more details setting up of Java, please refer to the link Java Environment Setup.
-
Install maven in the system from the link Apache Maven. To get more details about setting up of Maven, refer to the link Maven Environment Setup
-
Install IntelliJ from the link IntelliJ IDEA Ultimate. To get more details about setting up of IntelliJ, refer to the link Selenium IntelliJ.
Steps to Create JUnit Report
Step 1 − 创建一个 Maven 项目并将适当的依赖项添加到 pom.xml 文件中,用于以下项 −
Step 1 − Create a maven project and add the proper dependencies to the pom.xml file for the below items −
-
Add the Selenium Java dependencies from the link Selenium Java.
-
Add the JUnit dependencies from the link JUnit.
-
Add the JUnit Jupiter dependencies from the link JUnit Jupiter API.
-
Add the Maven site dependencies from the link Apache Maven Site Plugin.
-
Add the Maven Surefire Report dependencies from the link Maven Surefire Report Plugin.
-
Save the pom.xml with all the dependencies and update the maven project.
Step 2 −使用以下示例的实现创建 JUnit 测试类,其中我们将首先单击 New User button 验证 Welcome Page 上的文本 Welcome, Login In 。
Step 2 − Create a JUnit test class with the implementation of the below example where we will first click on the New User button verify the text Welcome, Login In on the Welcome Page.
点击 New User 按钮后,我们将导航到注册页面,其中 Back to Login 按钮如以下图像中突出所示。
On clicking the New User button, we will be navigating to the Registration page, having the Back to Login button as highlighted in the below image.
Code Implementation
package Report;
import org.junit.*;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.junit.jupiter.api.MethodOrderer;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.TestMethodOrder;
import java.util.concurrent.TimeUnit;
import static org.junit.Assert.assertEquals;
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class JunitTest {
WebDriver driver;
@Before
public void setup() throws Exception{
// Initiate browser driver
driver = new ChromeDriver();
// adding implicit wait of 20 secs
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
// Opening the webpage
driver.get("https://www.tutorialspoint.com/selenium/practice/login.php");
}
@Test
@Order(1)
public void verifyLoginAndRegisterPage() {
// identify header then get text
WebElement header = driver.findElement
(By.xpath("//*[@id='signInForm']/h1"));
String text = header.getText();
// assertions to test case to check login page
assertEquals("Welcome, Login In", text);
// navigate to register page
WebElement btn = driver.findElement
(By.xpath("//*[@id='signInForm']/div[3]/a"));
btn.click();
// assertions added to test case to check register page
WebElement btnchk = driver.findElement
(By.xpath("//*[@id='signupForm']/div[5]/a"));
boolean displayed = btnchk.isDisplayed();
// assertions to test case
assertEquals(true, displayed);
}
@After
public void teardown() {
// quitting browser
driver.quit();
}
}
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.junit.jupiter/junit-jupiter-api -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.10.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.maven.plugins/maven-site-plugin -->
<dependency>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-site-plugin</artifactId>
<version>4.0.0-M13</version>
</dependency>
</dependencies>
<!-- https://mvnrepository.com/artifact/org.apache.maven.plugins/maven-surefire-report-plugin -->
<reporting>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-report-plugin</artifactId>
<version>3.2.5</version>
</plugin>
</plugins>
</reporting>
</project>
Step 3 −使用以下命令从命令行运行测试: mvn clean test suite 。
Step 3 − Run the test from the command line with the command: mvn clean test suite.
Step 4 −刷新项目,名为 site 的新文件夹应在 target 文件夹内生成。
Step 4 − Refresh the project and a new folder called the site should get generated within the target folder.
Step 5 −右键单击 surefire-report.html 并选择在浏览器中打开的选项。
Step 5 − Right-click on the surefire-report.html and select the option to open in a browser.
-
JUnit* 报告将在浏览器中打开,显示包含总共 1 个测试方法的摘要,通过成功率为 100%。它还显示了包列表(包的名称、测试的数量、通过数、失败数、通过成功率、测试持续时间等)的详细信息。此外,测试方法名称 verifyLoginAndRegisterPage 也包含在报告中。
The JUnit report will be opened in the browser showing the Summary with total number of test methods as 1, with a pass success percentage of 100. It also showed details of the Package List(name of the package, number of tests, passed counts, failed counts, pass success percentage, duration of tests and so on. Also, the test method name verifyLoginAndRegisterPage is also included in the report.
Conclusion
这就总结了我们对 Selenium JUnit 报告教程的综合讲解。我们从描述 JUnit 报告、设置 JUnit 报告的先决条件开始,并逐步通过一个示例介绍如何使用 JUnit 报告以及与 Selenium 结合使用来说明如何创建 JUnit 报告。这将使您对 JUnit 具备深入了解。明智的做法是继续练习您所学的知识,并探索与 Selenium 相关的其他知识,以加深您的理解并拓宽您的视野。
This concludes our comprehensive take on the tutorial on Selenium JUnit Report. We’ve started with describing a JUnit report, prerequisites to set up an JUnit report, and walked through steps to create a JUnit report with an example illustrating how to use it along with Selenium. This equips you with in-depth knowledge of the JUnit. It is wise to keep practicing what you’ve learned and exploring others relevant to Selenium to deepen your understanding and expand your horizons.