- Selenium 教程
- Selenium - 首頁
- Selenium - 概述
- Selenium - 元件
- Selenium - 自動化測試
- Selenium - 環境搭建
- Selenium - 遠端控制
- Selenium IDE 教程
- Selenium - IDE 簡介
- Selenium - 特性
- Selenium - 限制
- Selenium - 安裝
- Selenium - 建立測試
- Selenium - 建立指令碼
- Selenium - 控制流
- Selenium - 儲存變數
- Selenium - 警報和彈出視窗
- Selenium - Selenese 命令
- Selenium - 操作命令
- Selenium - 獲取器命令
- Selenium - 斷言命令
- Selenium - Assert/Verify 方法
- Selenium - 定位策略
- Selenium - 指令碼除錯
- Selenium - 驗證點
- Selenium - 模式匹配
- Selenium - JSON 資料檔案
- Selenium - 瀏覽器執行
- Selenium - 使用者擴充套件
- Selenium - 程式碼匯出
- Selenium - 程式碼輸出
- Selenium - JavaScript 函式
- Selenium - 外掛
- Selenium WebDriver 教程
- Selenium - 簡介
- Selenium WebDriver vs RC
- Selenium - 安裝
- Selenium - 第一個測試指令碼
- Selenium - 驅動程式會話
- Selenium - 瀏覽器選項
- Selenium - Chrome 選項
- Selenium - Edge 選項
- Selenium - Firefox 選項
- Selenium - Safari 選項
- Selenium - 雙擊
- Selenium - 右鍵點選
- Python 中的 HTML 報告
- 處理編輯框
- Selenium - 單個元素
- Selenium - 多個元素
- Selenium Web 元素
- Selenium - 檔案上傳
- Selenium - 定位器策略
- Selenium - 相對定位器
- Selenium - 查詢器
- Selenium - 查詢所有連結
- Selenium - 使用者互動
- Selenium - WebElement 命令
- Selenium - 瀏覽器互動
- Selenium - 瀏覽器命令
- Selenium - 瀏覽器導航
- Selenium - 警報和彈出視窗
- Selenium - 處理表單
- Selenium - 視窗和選項卡
- Selenium - 處理連結
- Selenium - 輸入框
- Selenium - 單選按鈕
- Selenium - 複選框
- Selenium - 下拉框
- Selenium - 處理 IFrame
- Selenium - 處理 Cookie
- Selenium - 日期時間選擇器
- Selenium - 動態 Web 表格
- Selenium - Actions 類
- Selenium - Action 類
- Selenium - 鍵盤事件
- Selenium - 向上/向下鍵
- Selenium - 複製和貼上
- Selenium - 處理特殊鍵
- Selenium - 滑鼠事件
- Selenium - 拖放
- Selenium - 筆事件
- Selenium - 滾動操作
- Selenium - 等待策略
- Selenium - 顯式/隱式等待
- Selenium - 支援特性
- Selenium - 多選
- Selenium - 等待支援
- Selenium - 選擇支援
- Selenium - 顏色支援
- Selenium - ThreadGuard
- Selenium - 錯誤和日誌記錄
- Selenium - 異常處理
- Selenium - 其他
- Selenium - 處理 Ajax 呼叫
- Selenium - JSON 資料檔案
- Selenium - CSV 資料檔案
- Selenium - Excel 資料檔案
- Selenium - 跨瀏覽器測試
- Selenium - 多瀏覽器測試
- Selenium - 多視窗測試
- Selenium - JavaScript 執行器
- Selenium - 無頭執行
- Selenium - 捕獲螢幕截圖
- Selenium - 捕獲影片
- Selenium - 頁面物件模型
- Selenium - 頁面工廠
- Selenium - 記錄和回放
- Selenium - 框架
- Selenium - 瀏覽上下文
- Selenium - DevTools
- Selenium Grid 教程
- Selenium - 概述
- Selenium - 架構
- Selenium - 元件
- Selenium - 配置
- Selenium - 建立測試指令碼
- Selenium - 測試執行
- Selenium - 端點
- Selenium - 自定義節點
- Selenium 報告工具
- Selenium - 報告工具
- Selenium - TestNG
- Selenium - JUnit
- Selenium - Allure
- Selenium & 其他技術
- Selenium - Java 教程
- Selenium - Python 教程
- Selenium - C# 教程
- Selenium - Javascript 教程
- Selenium - Kotlin 教程
- Selenium - Ruby 教程
- Selenium - Maven & Jenkins
- Selenium - 資料庫測試
- Selenium - LogExpert 日誌記錄
- Selenium - Log4j 日誌記錄
- Selenium - Robot Framework
- Selenium - AutoIT
- Selenium - Flash 測試
- Selenium - Apache Ant
- Selenium - Github 教程
- Selenium - SoapUI
- Selenium - Cucumber
- Selenium - IntelliJ
- Selenium - XPath
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 檔案執行測試。
點選新使用者按鈕後,我們將導航到註冊頁面,其中包含歡迎,註冊文字,如下圖所示。
示例
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>
上述實現的專案結構如下圖所示:
我們將透過 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的新資料夾。
步驟 3 - 右鍵點選emailable-report.html,然後選擇在瀏覽器中開啟的選項。
該報告將在瀏覽器中開啟,顯示測試類名稱 - TestNGTest.java,以及透過、跳過、失敗的總數、測試持續時間等。此外,報告中還包含測試方法名稱 moveToRegisterPage、verifyRegisterPageHeading 和 verifyWelcomePageHeading。
index.html 報告
建立 index.html 報告的步驟如下:
步驟 1 - 按照建立 emailable-report.html 所述步驟的步驟 1 進行操作。
步驟 2 - 重新整理專案,專案結構中應該會生成一個名為test-output的新資料夾。
步驟 3 - 右鍵點選index.html,然後選擇在瀏覽器中開啟的選項。
該報告將在瀏覽器中開啟,顯示 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 檔案執行測試。此外,測試執行完成後,報告中將包含以下日誌訊息:
正在轉到註冊頁面
已驗證登入頁面標題
已驗證註冊頁面標題
點選新使用者按鈕後,我們將導航到註冊頁面,其中包含歡迎,註冊文字,如下圖所示。
請注意,我們將透過 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.html 和 index.html,選擇在瀏覽器中開啟的選項。
以下 emailable-report.html 報告應該在瀏覽器中開啟,顯示測試類名稱 - TestNGTest.java,以及透過、跳過、失敗的總數、測試持續時間等資訊。此外,報告中還包含測試方法名稱 moveToRegisterPage、verifyRegisterPageHeading 和 verifyWelcomePageHeading。
在報告的下方部分,可以檢視日誌訊息以及測試方法名稱。
以下 index.html 報告應該在瀏覽器中開啟,顯示 testng.xml、測試數量、組、時間、報告輸出、忽略的方法和按時間順序檢視。此外,結果部分包含測試方法名稱 moveToRegisterPage、verifyRegisterPageHeading 和 verifyWelcomePageHeading,以及方法數量、透過和失敗的數量。
點選左側的“報告輸出”選項卡,可以檢視日誌訊息以及測試方法名稱。
以下連結提供了 TestNG 的詳細描述:TestNG
本教程到此結束,我們全面介紹了 Selenium - TestNG 報告。我們從描述 TestNG 報告、設定 TestNG 報告的先決條件開始,並逐步演示瞭如何建立不同型別的 TestNG 報告,例如 emailable-report.html、index.html 以及使用 Reporter 類的報告,並透過示例說明了如何將它們與 Selenium 結合使用。這使您深入瞭解了 TestNG 報告。建議您不斷練習所學知識,並探索與 Selenium 相關的其他內容,以加深理解並擴充套件視野。
