如何使用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