如何使用 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(); } }