為什麼將 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