如何在 Selenium 中使用 TestNG 進行跨瀏覽器測試?


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 程式碼。

    • 引數(“Browser”)將從 testng.xml 中讀取值並將其傳遞給程式碼。

    • launchapp() 將為瀏覽器啟動網站 http://www.calculator.net 設定所有初始步驟。

    • CalculatePercent 將執行實際執行和驗證。

  • 步驟 3:建立 testng.xml 檔案,引數名稱為瀏覽器,如 testng.xml 部分所示。

  • 步驟 4:執行 testng.xml 檔案。

  • 步驟 5:它將同時在所有 3 個瀏覽器中執行測試用例。

示例

以下程式碼用於建立包含 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
=============================================== 

更新於: 2023-08-21

119 次檢視

開啟你的 職業生涯

透過完成課程獲得認證

開始學習
廣告