如何使用TestNG在不同浏览器上进行Selenium测试?
TestNG是一个强大的测试框架,它是JUnit的增强版,JUnit在TestNG出现之前已经使用了很长时间。NG代表“下一代”。
应该在多个浏览器(如IE、Chrome、Firefox、Safari)上测试网站,以验证网站的兼容性和功能。由于HTML、CSS和JavaScript在所有浏览器中都是独一无二的,因此始终建议进行跨浏览器测试以确保网站的兼容性。Selenium支持跨浏览器测试,TestNG也支持。
在本文中,我们将分析如何使用Selenium和TestNG进行跨浏览器测试。
解决此问题的方法/算法
步骤1:确保Selenium、TestNG以及不同浏览器的初始设置(如Firefox、IE、Chrome驱动程序)已正确安装在系统中。
步骤2:创建一个TestNG类,并编写如程序代码中所示的Selenium代码。
Parameter('Browser')将从testng.xml读取值并将其传递给代码。
launchapp()将设置启动网站http://www.calculator.net所需的所有初始步骤。
CalculatePercent将执行实际操作并进行验证。
步骤3:创建testng.xml文件,参数名称为browser,如testng.xml部分所示。
步骤4:运行testng.xml文件。
步骤5:它将同时在所有三个浏览器中运行测试用例。
示例
以下代码创建了一个包含Selenium代码的TestNG类。
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.*;
import org.testng.annotations.*;
public class TestNGClass {
private WebDriver driver;
private String URL = "http://www.calculator.net";
@Parameters("browser")
@BeforeTest
public void launchapp(String browser) {
if (browser.equalsIgnoreCase("firefox")) {
System.out.println(" Executing on FireFox");
driver = new FirefoxDriver();
driver.get(URL);
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.manage().window().maximize();
} else if (browser.equalsIgnoreCase("chrome")) {
System.out.println(" Executing on CHROME");
System.out.println("Executing on IE");
System.setProperty("webdriver.chrome.driver", "D:\chromedriver.exe");
driver = new ChromeDriver();
driver.get(URL);
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.manage().window().maximize();
} else if (browser.equalsIgnoreCase("ie")) {
System.out.println("Executing on IE");
System.setProperty("webdriver.ie.driver", "D:\IEDriverServer.exe");
driver = new InternetExplorerDriver();
driver.get(URL);
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.manage().window().maximize();
} else {
throw new IllegalArgumentException("The Browser Type is Undefined");
}
}
@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 closeBrowser() {
driver.close();
}
}
testng.xml
这是一个配置文件,用于组织和运行TestNG测试用例。
当只需要执行有限的测试而不是完整的套件时,它非常方便。
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE suite SYSTEM "<a href="http://testng.org/testng-1.0.dtd">http://testng.org/testng-1.0.dtd</a>"> <suite name="TestSuite" thread-count="3" parallel="tests" > <test name="ChromeTest"> <parameter name="browser" value="chrome"/> <classes> <class name="TestNGClass "> </class> </classes> </test> <test name="FirefoxTest"> <parameter name="browser" value="firefox" /> <classes> <class name="TestNGClass"> </class> </classes> </test> <test name="ie"> <parameter name="browser" value="ie" /> <classes> <class name="TestNGClass"> </class> </classes> </test> </suite>
输出
[TestNG] Running: C://Users/************** Executing on CHROME Executing on IE Executing on FIREFOX The Result is 5 The Result is Pass The Result is 5 The Result is Pass The Result is 5 The Result is Pass =============================================== Suite1 Total tests run: 3, Passes: 3, Failures: 0, Skips: 0 ===============================================
广告
数据结构
网络
关系数据库管理系统 (RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP