• Selenium Video Tutorials

Selenium - TestNG



當我們使用 Selenium 或任何其他工具執行任何自動化測試時,都需要檢視和分析執行結果,以得出已執行、透過、失敗的測試數量、失敗資料等資訊,並以報告的形式呈現。

有時,測試失敗時捕獲的螢幕截圖也會包含在報告中。測試報告也需要定期與專案利益相關者共享。為此,我們可以藉助 TestNG 報告。

TestNG 報告是在使用 TestNG 構建並執行測試用例後自動生成的 HTML 報告。它是一個單元測試框架,可以與 Selenium 測試整合並用於報告目的。

此外,TestNG 還有一個名為 Reporter 的預設報告類,用於幫助記錄日誌。這有助於檢測故障的根本原因,以除錯失敗的測試。

建立 TestNG 報告的先決條件

  • 在系統中安裝 Java(版本高於 8),並使用命令檢查它是否存在:java -version。如果安裝已成功完成,則將顯示已安裝的 Java 版本。

  • 在系統中安裝 Maven,並使用命令檢查它是否存在:mvn -version。如果安裝已成功完成,則將顯示已安裝的 Maven 版本。

  • 安裝任何 IDE,如 Eclipse、IntelliJ 等。

  • 從以下連結新增 TestNG 依賴項:https://mvnrepository.com/artifact/

  • 從以下連結新增 Selenium Java 依賴項:selenium-java

  • 儲存包含所有依賴項的 pom.xml 並更新 Maven 專案

生成 TestNG 報告的不同方法有哪些?

下面列出了生成 TestNG 報告的不同方法:

  • emailable-report.html

  • index.html 報告

  • Reporter 類報告

emailable-report.html

建立 emailable-report.html 的步驟如下:

步驟 1 - 建立一個 TestNG 測試類,其中包含以下示例的實現,我們首先將點選歡迎頁面上的新使用者按鈕

請注意,我們將透過 testng.xml 檔案執行測試。

Selenium TestNG Report 1

點選新使用者按鈕後,我們將導航到註冊頁面,其中包含歡迎,註冊文字,如下圖所示。

Selenium TestNG Report 2

示例

package Report;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.*;
import java.util.concurrent.TimeUnit;
import static org.testng.Assert.assertEquals;

public class TestNGTest {
   WebDriver driver;
   @BeforeTest
   public void setup() throws Exception{

      // Initiate browser driver
      driver = new ChromeDriver();
      
      // adding implicit wait of 12 secs
      driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
      
      // Opening the webpage
      driver.get("https://tutorialspoint.tw/selenium/practice/login.php");
   }
   @Test(priority = 1)
   public void verifyWelcomePageHeading() {
   
      // identify header then get text
      WebElement header = driver.findElement
         (By.xpath("//*[@id='signInForm']/h1"));
      String text = header.getText();
      
      // assertion to verify login page header
      assertEquals("Welcome, Login In", text);
   }
   @Test(priority = 2)
   public void moveToRegisterPage() {
   
      // identify button then click
      WebElement btn = driver.findElement
         (By.xpath("//*[@id='signInForm']/div[3]/a"));
      btn.click();
   }
   @Test(priority = 3)
   public void verifyRegisterPageHeading() {
   
      // identify header then get text
      WebElement heder = driver.findElement
         (By.xpath("//*[@id='signupForm']/h1"));
      String text = heder.getText();
      
      // assertion to verify register page header
      assertEquals("Welcome,Register", text);
   }

   @AfterTest
   public void teardown() {
      // quitting browser
      driver.quit();
   }
}

testng.xml 檔案中的配置。

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="All Test Suite">
   <test verbose="2" preserve-order="true" name="TestNGTest.java">
      <classes>
         <class name="Report.TestNGTest">
            <methods>
               <include name="verifyWelcomePageHeading"/>
               <include name="moveToRegisterPage"/>
               <include name="verifyRegisterPageHeading"/>
            </methods>
         </class>
      </classes>
   </test>
</suite>

pom.xml 檔案中的依賴項。

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
   http://maven.apache.org/xsd/maven-4.0.0.xsd">
   
   <modelVersion>4.0.0</modelVersion>
   <groupId>org.example</groupId>
   <artifactId>SeleniumJava</artifactId>
   <version>1.0-SNAPSHOT</version>
   
   <properties>
      <maven.compiler.source>16</maven.compiler.source>
      <maven.compiler.target>16</maven.compiler.target>
      <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
   </properties>
   
   <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
   <dependencies>
      <dependency>
         <groupId>org.seleniumhq.selenium</groupId>
         <artifactId>selenium-java</artifactId>
         <version>4.11.0</version>
      </dependency>
      
      <!-- https://mvnrepository.com/artifact/org.testng/testng -->
      <dependency>
         <groupId>org.testng</groupId>
         <artifactId>testng</artifactId>
         <version>7.9.0</version>
         <scope>test</scope>
      </dependency>
   </dependencies>
</project>

上述實現的專案結構如下圖所示:

Selenium TestNG Report 3

我們將透過 testng.xml 執行測試。

輸出

===============================================
All Test Suite
Total tests run: 3, Passes: 3, Failures: 0, Skips: 0
===============================================

Process finished with exit code 0

我們藉助 TestNG 測試框架建立了測試,並檢索了頁面標題並使用斷言對其進行了驗證。

控制檯中的結果顯示總測試執行次數:3,因為有三個帶有 @Test 註釋的方法 - verifyWelcomePageHeading()、moveToRegisterPage() 和 verifyRegisterPageHeading()。

最後,收到訊息透過:3程序已完成,退出程式碼為 0,表示程式碼已成功執行。

步驟 2 - 重新整理專案,專案結構中應該會生成一個名為test-output的新資料夾。

Selenium TestNG Report 4

步驟 3 - 右鍵點選emailable-report.html,然後選擇在瀏覽器中開啟的選項。

Selenium TestNG Report 5

該報告將在瀏覽器中開啟,顯示測試類名稱 - TestNGTest.java,以及透過、跳過、失敗的總數、測試持續時間等。此外,報告中還包含測試方法名稱 moveToRegisterPage、verifyRegisterPageHeading 和 verifyWelcomePageHeading。

index.html 報告

建立 index.html 報告的步驟如下:

步驟 1 - 按照建立 emailable-report.html 所述步驟的步驟 1 進行操作。

步驟 2 - 重新整理專案,專案結構中應該會生成一個名為test-output的新資料夾。

Selenium TestNG Report 6

步驟 3 - 右鍵點選index.html,然後選擇在瀏覽器中開啟的選項。

Selenium TestNG Report 7

該報告將在瀏覽器中開啟,顯示 testng.xml、測試數量、組、時間、報告程式輸出、忽略的方法和按時間順序檢視。此外,“結果”部分包含測試方法名稱 moveToRegisterPage、verifyRegisterPageHeading 和 verifyWelcomePageHeading,以及方法數量、透過和失敗的數量。

從 Reporter 類生成的報告

TestNG 提供了 Reporter 類用於記錄日誌。它有四種不同的方法來處理要記錄的資訊:

  • Reporter.log( String str);

  • Reporter.log( String str, Boolean logToOut);

  • Reporter.log( String str, int l);

  • Reporter.log( String str, int l, Boolean logToOut);

在報告中使用 Reporter 類生成日誌訊息的步驟如下:

步驟 1 - 建立一個 TestNG 測試類,其中包含以下示例的實現,我們首先將點選歡迎頁面上的新使用者按鈕

請注意,我們將透過 testng.xml 檔案執行測試。此外,測試執行完成後,報告中將包含以下日誌訊息:

  • 正在轉到註冊頁面

  • 已驗證登入頁面標題

  • 已驗證註冊頁面標題

Selenium TestNG Report 8

點選新使用者按鈕後,我們將導航到註冊頁面,其中包含歡迎,註冊文字,如下圖所示。

Selenium TestNG Report 9

請注意,我們將透過 testng.xml 檔案執行測試。此外,測試執行完成後,報告中將包含以下日誌訊息:

  • 正在轉到註冊頁面

  • 已驗證登入頁面標題

  • 已驗證註冊頁面標題

示例

package Report;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Reporter;
import org.testng.annotations.*;
import java.util.concurrent.TimeUnit;
import static org.testng.Assert.assertEquals;

public class TestNGTest {
   WebDriver driver;
   @BeforeTest
   public void setup() throws Exception{

      // Initiate browser driver
      driver = new ChromeDriver();
      
      // adding implicit wait of 12 secs
      driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
      
      // Opening the webpage
      driver.get("https://tutorialspoint.tw/selenium/practice/login.php");
   }
   @Test(priority = 1)
   public void verifyWelcomePageHeading() {
   
      // identify header then get text
      WebElement header = driver.findElement(By.xpath("//*[@id='signInForm']/h1"));
      String text = header.getText();
      
      // assertion to verify login page header
      assertEquals("Welcome, Login In", text);
      Reporter.log("Verified Login Page header");
   }
   @Test(priority = 2)
   public void moveToRegisterPage() {
   
      // identify button then click
      WebElement btn = driver.findElement(By.xpath("//*[@id='signInForm']/div[3]/a"));
      btn.click();
      Reporter.log("Moving to Registration Page");
   }
   @Test(priority = 3)
   public void verifyRegisterPageHeading() {
   
      // identify header then get text
      WebElement heder = driver.findElement(By.xpath("//*[@id='signupForm']/h1"));
      String text = heder.getText();
      
      // assertion to verify register page header
      assertEquals("Welcome,Register", text);
      Reporter.log("Verified Register Page header");
   }

   @AfterTest
   public void teardown() {
   // quitting browser
   driver.quit();
   }
}

步驟 2 - 重新整理專案,專案結構中應該會生成一個名為test-output的新資料夾。

步驟 3 − 右鍵點選emailable-report.htmlindex.html,選擇在瀏覽器中開啟的選項。

以下 emailable-report.html 報告應該在瀏覽器中開啟,顯示測試類名稱 - TestNGTest.java,以及透過、跳過、失敗的總數、測試持續時間等資訊。此外,報告中還包含測試方法名稱 moveToRegisterPage、verifyRegisterPageHeading 和 verifyWelcomePageHeading。

在報告的下方部分,可以檢視日誌訊息以及測試方法名稱。

10

以下 index.html 報告應該在瀏覽器中開啟,顯示 testng.xml、測試數量、組、時間、報告輸出、忽略的方法和按時間順序檢視。此外,結果部分包含測試方法名稱 moveToRegisterPage、verifyRegisterPageHeading 和 verifyWelcomePageHeading,以及方法數量、透過和失敗的數量。

點選左側的“報告輸出”選項卡,可以檢視日誌訊息以及測試方法名稱。

11

以下連結提供了 TestNG 的詳細描述:TestNG

本教程到此結束,我們全面介紹了 Selenium - TestNG 報告。我們從描述 TestNG 報告、設定 TestNG 報告的先決條件開始,並逐步演示瞭如何建立不同型別的 TestNG 報告,例如 emailable-report.html、index.html 以及使用 Reporter 類的報告,並透過示例說明了如何將它們與 Selenium 結合使用。這使您深入瞭解了 TestNG 報告。建議您不斷練習所學知識,並探索與 Selenium 相關的其他內容,以加深理解並擴充套件視野。

廣告

© . All rights reserved.