Selenium - TestNG
Selenium - TestNG 集成
Section titled “Selenium - TestNG 集成”TestNG 是一个流行的 Java 开源测试框架,它受到 JUnit 的启发,但引入了更强大的特性,适用于复杂的测试自动化项目。将 Selenium WebDriver 与 TestNG 集成,为组织、执行和报告测试提供了一个健壮的结构。
使用 TestNG 与 Selenium 的主要特性和优势:
- 注解 (Annotations): 提供注解(
@Test、@BeforeMethod、@AfterMethod、@DataProvider等)来控制测试生命周期和结构。 - 测试配置 (Test Configuration): 支持在不同级别(Suite、Test、Class、Method)灵活设置初始化和清理方法。
- 分组 (Grouping): 允许对测试方法进行分组(例如,‘smoke’、‘regression’、‘sanity’)。
- 并行执行 (Parallel Execution): 内置支持测试的并行运行(方法、类、测试),以加快执行速度。
- 参数化 (Parameterization): 轻松向测试传递参数,实现数据驱动测试(使用
@Parameters或@DataProvider)。 - 依赖管理 (Dependency Management): 定义测试方法之间的依赖关系。
- 报告 (Reporting): 生成基本的 HTML 报告,并能很好地集成 ExtentReports 或 Allure 等报告库。
为 Eclipse/IntelliJ 安装 TestNG
Section titled “为 Eclipse/IntelliJ 安装 TestNG”向项目添加 TestNG 的推荐方法是通过 Maven 或 Gradle 等构建工具。如果使用 IDE,也可以安装 TestNG 插件:
- Eclipse: 进入
Help->Eclipse Marketplace...,搜索 ‘TestNG’,然后安装 ‘TestNG for Eclipse’。 - IntelliJ IDEA: TestNG 插件通常与 IntelliJ IDEA 捆绑在一起。如果没有,进入
File->Settings->Plugins,在 Marketplace 标签页中搜索 ‘TestNG’ 并安装。
请记住,您还需要像“Selenium - WebDriver”章节示例中所示,将 TestNG 依赖项添加到您的 pom.xml (Maven) 或 build.gradle (Gradle) 文件中。
常用 TestNG 注解 (Annotations)
Section titled “常用 TestNG 注解 (Annotations)”注解控制 TestNG 如何执行您的代码:
| 注解 (Annotation) | 描述 (Description) |
|---|---|
@Test | 标记一个方法作为测试用例。 |
@BeforeSuite / @AfterSuite | 在 XML suite 中的所有测试运行之前/之后执行一次。 |
@BeforeTest / @AfterTest | 在 XML 文件中当前 <test> 标签内的所有测试方法执行之前/之后执行。 |
@BeforeClass / @AfterClass | 在当前类中的第一个/最后一个测试方法被调用之前/之后执行一次。 |
@BeforeMethod / @AfterMethod | 在每个标记有 @Test 的方法执行之前/之后执行。 |
@DataProvider | 标记一个为测试方法提供数据的方法。测试方法必须声明 dataProvider="name"。 |
@Parameters | 用于将 testng.xml 文件中定义的参数传递给测试方法。 |
@Listeners | 为测试类定义测试监听器(实现 ITestListener、ISuiteListener 等接口的类)。 |
@Factory | 标记一个方法作为创建测试类实例的工厂方法(高级特性)。 |
Selenium 与 TestNG 的示例
Section titled “Selenium 与 TestNG 的示例”让我们修改 WebDriver 章节中的 Google 搜索示例,以展示常用的 TestNG 注解(@BeforeMethod、@AfterMethod、@Test)。
package com.example.tests;
import io.github.bonigarcia.wdm.WebDriverManager;import org.openqa.selenium.By;import org.openqa.selenium.Keys;import org.openqa.selenium.WebDriver;import org.openqa.selenium.WebElement;import org.openqa.selenium.chrome.ChromeDriver;import org.openqa.selenium.support.ui.ExpectedConditions;import org.openqa.selenium.support.ui.WebDriverWait;import org.testng.Assert;import org.testng.annotations.*; // Import TestNG annotations
import java.time.Duration;
public class GoogleSearchTestNG {
WebDriver driver; WebDriverWait wait;
// Runs before each @Test method in this class @BeforeMethod public void setupTest() { System.out.println("Setting up WebDriver for test..."); WebDriverManager.chromedriver().setup(); driver = new ChromeDriver(); wait = new WebDriverWait(driver, Duration.ofSeconds(10)); driver.manage().window().maximize(); }
// This is the actual test case @Test(description = "Verify Google search functionality") public void testGoogleSearch() { System.out.println("Executing testGoogleSearch..."); driver.get("https://www.google.com"); WebElement searchBox = wait.until(ExpectedConditions.presenceOfElementLocated(By.name("q"))); searchBox.sendKeys("TestNG Selenium"); searchBox.sendKeys(Keys.ENTER); wait.until(ExpectedConditions.titleContains("TestNG Selenium")); Assert.assertTrue(driver.getTitle().contains("TestNG Selenium"), "Title mismatch"); System.out.println("Search successful!"); }
// Another test case in the same class @Test(description = "Verify Google page title") public void testGoogleTitle() { System.out.println("Executing testGoogleTitle..."); driver.get("https://www.google.com"); Assert.assertEquals(driver.getTitle(), "Google", "Homepage title mismatch"); System.out.println("Title verified!"); }
// Runs after each @Test method in this class @AfterMethod public void tearDownTest() { System.out.println("Tearing down WebDriver..."); if (driver != null) { driver.quit(); } }
// Example using @BeforeClass / @AfterClass (runs once per class) @BeforeClass public void setupClass() { System.out.println("--- Setting up resources for GoogleSearchTestNG class ---"); }
@AfterClass public void tearDownClass() { System.out.println("--- Cleaning up resources for GoogleSearchTestNG class ---"); }}通过 testng.xml 执行
Section titled “通过 testng.xml 执行”虽然您可以从 IDE 运行单个测试类,但 testng.xml 提供了对执行更精细的控制,包括定义 suites、tests、parameters、groups 和 parallelism。
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd"><suite name="Selenium Test Suite"> <test name="Google Tests"> <classes> <class name="com.example.tests.GoogleSearchTestNG"/> <!-- Add other test classes here --> </classes> </test> <!-- Test --></suite> <!-- Suite -->要使用 XML 文件运行:在您的 IDE 中右键单击 testng.xml,然后选择 ‘Run As’ -> ‘TestNG Suite’。
执行后,TestNG 会提供反馈:
- 控制台输出 (Console Output): 显示测试执行进度和通过/失败状态。
- TestNG 结果窗口 (IDE): 大多数 IDE 插件提供一个专门的视图,以树状格式显示测试结果。
- HTML 报告 (HTML Reports): TestNG 会在
test-output文件夹(通常位于项目根目录)中生成基本的 HTML 报告,详细说明执行结果。
对于更复杂的报告,可以考虑使用监听器 (listeners) 将 ExtentReports 或 Allure Test Report 等库与 TestNG 集成。