为什么将 Selenium 与 TestNG 结合使用?
TestNG 是一个强大的测试框架,是 JUnit 的增强版本,JUnit 在 TestNG 出现之前很长时间内一直在使用。NG 代表“下一代”。
TestNG 框架提供以下功能:
注解帮助我们轻松组织测试。
灵活的测试配置。
多个测试用例可以更容易地分组。
可以使用 TestNG 实现测试的并行化。
支持数据驱动测试。
内置报告以及支持 Extent Reporting 和其他报告工具。Selenium 默认不生成任何报告。
易于配置跨浏览器测试。
TestNG 框架可以轻松地与 Maven、Jenkins、Gradle、Ant 或任何其他构建工具以及 CI/CD 工具集成。
TestNG 也处理一些未捕获的异常。每当这些异常发生时,TestNG 都会使测试步骤失败,并且可以在报告中验证这一点。
所有 TestNG 注解都可以在 Selenium 中使用。注解易于阅读和理解。
Selenium 允许与网页交互。它是一个接口,而不是一个测试框架。要仅在 Selenium 中运行任何测试或代码,我们必须使用 Java main 方法。TestNG 为我们提供了一个框架,该框架可以在不使用 Java main 方法的情况下运行 Selenium 代码。除此之外,更好的代码可维护性、报告、灵活的测试配置是将 TestNG 与 Selenium 结合使用的额外优势。
解决此问题的方法/算法
步骤 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();
}
}
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP