如何使用 Selenium 和 TestNG?


TestNG 是一个强大的测试框架,是 JUnit 的增强版本,JUnit 在 TestNG 出现之前已经使用了很长时间。NG 代表“下一代”。

TestNG 框架提供以下功能:

  • 注解帮助我们轻松地组织测试。

  • 灵活的测试配置。

  • 测试用例可以更容易地分组。

  • 可以使用 TestNG 实现测试的并行化。

  • 支持数据驱动测试。

  • 内置报告。

Selenium 允许与网页交互。它是一个接口,而不是一个测试框架。要仅在 Selenium 中运行任何测试或代码,我们必须使用 Java 的 main 方法。TestNG 为我们提供了一个框架,可以在不使用 Java main 方法的情况下运行 Selenium 代码。除此之外,更好的代码可维护性、报告和灵活的测试配置是将 TestNG 与 Selenium 结合使用的额外优势。

Selenium 中的 TestNG 注解

注解在 JDK 5 中正式添加到 Java 语言中,TestNG 选择使用注解来注释测试类。以下是使用注解的一些好处。更多关于 TestNG 的信息可以在这里找到

  • TestNG 通过查找注解来识别它感兴趣的方法。因此,方法名称不受任何模式或格式的限制。

  • 我们可以将其他参数传递给注解。

  • 注解是强类型的,因此编译器会立即标记任何错误。

  • 测试类不再需要扩展任何内容(例如 JUnit 3 的 TestCase)。

用户可以在 Selenium 中使用所有可用的 TestNG 注解。其中一些如下所示

序号

                 注解及描述

1

@BeforeSuite

带注释的方法将仅在该套件中的所有测试运行之前运行一次。

2

@AfterSuite

带注释的方法将仅在该套件中的所有测试运行之后运行一次。

3

@BeforeClass

带注释的方法将仅在调用当前类中的第一个测试方法之前运行一次。

4

@AfterClass

带注释的方法将仅在当前类中的所有测试方法运行之后运行一次。

5

@BeforeTest

带注释的方法将在运行<test>标签内的类中的任何测试方法之前运行。

6

@AfterTest

带注释的方法将在运行<test>标签内的类中的所有测试方法之后运行。

7

@BeforeGroups

此配置方法将在其之前运行的组列表。保证此方法将在调用属于这些组中的任何一个的第一个测试方法之前不久运行。

8

@AfterGroups

此配置方法将在其之后运行的组列表。保证此方法将在调用属于这些组中的任何一个的最后一个测试方法之后不久运行。

9

@BeforeMethod

带注释的方法将在每个测试方法之前运行。

10

@AfterMethod

带注释的方法将在每个测试方法之后运行。

11

@DataProvider

将方法标记为为测试方法提供数据。带注释的方法必须返回一个 Object[ ][ ],其中每个 Object[ ] 可以分配给测试方法的参数列表。想要从该 DataProvider 接收数据的 @Test 方法需要使用一个数据提供程序名称,该名称等于此注解的名称。

12

@Factory

将方法标记为一个工厂,该工厂返回 TestNG 将用作测试类的对象。该方法必须返回 Object[ ]。

13

@Listeners

在测试类上定义监听器。

14

@Parameters

描述如何将参数传递给 @Test 方法。

15

@Test

将类或方法标记为测试的一部分。

让我们分析如何将 Selenium 与 TestNG 结合使用。

解决此问题的步骤/算法

  • 步骤 1:确保 Selenium、TestNG 和 Firefox 驱动程序的初始设置已正确设置在系统中。

  • 步骤 2:创建一个 TestNG 类,并编写程序代码中提到的 Selenium 代码。

  • 步骤 3:运行 TestNGClass 文件。

  • 步骤 4:在没有 TestNG 的情况下编写相同的代码,并比较差异。

示例

以下代码用于创建带有 Selenium 代码的 TestNG 类

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class TestNGClass {
   WebDriver driver = new FirefoxDriver();
   
   @BeforeTest
   public void launchApp() {
      // Puts an Implicit wait, Will wait for 10 seconds before throwing exception
      driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
      
      // Launch website
      driver.navigate().to("http://www.calculator.net");
      driver.manage().window().maximize();
   }
   
   @Test
   public void calculatePercent() {
      // Click on Math Calculators
      driver.findElement(By.xpath(".//*[@id='menu']/div[3]/a")).click();
      
      // Click on Percent Calculators
      driver.findElement(By.xpath(".//*[@id='menu']/div[4]/div[3]/a")).click();
      
      // Enter value 10 in the first number of the percent Calculator
      driver.findElement(By.id("cpar1")).sendKeys("10");
      
      // Enter value 50 in the second number of the percent Calculator
      driver.findElement(By.id("cpar2")).sendKeys("50");
      
      // Click Calculate Button
      driver.findElement(By.xpath(".//*[@id='content']/table/tbody/tr/td[2]/input")).click();
      
      // Get the Result Text based on its xpath
      String result =
         driver.findElement(By.xpath(".//*[@id='content']/p[2]/span/font/b")).getText();
      
      // Print a Log In message to the screen
      System.out.println(" The Result is " + result);
      
      if(result.equals("5")) {
         System.out.println(" The Result is Pass");
      } else {
         System.out.println(" The Result is Fail");
      }
   }
   
   @AfterTest
   public void terminatetest() {
      driver.close();
   }
}

输出

[TestNG] Running:
  C://Users/**************
  The Result is 5
  The Result is Pass
PASSED: calulatePercent
===============================================
Suite1
Total tests run: 1, Passes: 1, Failures: 0, Skips: 0
=============================================== 

没有 TestNG 的 Selenium 代码

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;

public class SeleniumWithoutTestNG {
public static void main(String[] args) throws InterruptedException {
        WebDriver driver = new FirefoxDriver();
   
   
        // Puts an Implicit wait, Will wait for 10 seconds before throwing exception
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);      
      // Launch website
       driver.navigate().to("http://www.calculator.net");
       driver.manage().window().maximize();
      // Click on Math Calculators             driver.findElement(By.xpath(".//*[@id='menu']/div[3]/a")).click();      
      // Click on Percent Calculators      driver.findElement(By.xpath(".//*[@id='menu']/div[4]/div[3]/a")).click();      
      // Enter value 10 in the first number of the percent //Calculator
      driver.findElement(By.id("cpar1")).sendKeys("10");      
      // Enter value 50 in the second number of the percent //Calculator
      driver.findElement(By.id("cpar2")).sendKeys("50");
      
      // Click Calculate Button
      driver.findElement(By.xpath(".//*[@id='content']/table/tbody/tr/td[2]/input")).click();
      
      // Get the Result Text based on its xpath
      String result =         driver.findElement(By.xpath(".//*[@id='content']/p[2]/span/font/b")).getText();      
      // Print a Log In message to the screen
      System.out.println(" The Result is " + result);
      
      if(result.equals("5")) {
         System.out.println(" The Result is Pass");
      } else {
         System.out.println(" The Result is Fail");
      }
      driver.close();
   }
}

更新于: 2023-08-18

337 次浏览

启动你的 职业生涯

通过完成课程获得认证

开始
广告
© . All rights reserved.