- Selenium 教程
- Selenium - 首頁
- Selenium - 概述
- Selenium - 元件
- Selenium - 自動化測試
- Selenium - 環境搭建
- Selenium - 遠端控制
- Selenium IDE 教程
- Selenium - IDE 簡介
- Selenium - 特性
- Selenium - 限制
- Selenium - 安裝
- Selenium - 建立測試用例
- Selenium - 建立指令碼
- Selenium - 控制流
- Selenium - 儲存變數
- Selenium - 警報和彈出視窗
- Selenium - Selenese 命令
- Selenium - Actions 命令
- Selenium - Accessors 命令
- Selenium - Assertions 命令
- 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 其他概念
- Selenium - IE 驅動程式
- Selenium - 自動化框架
- Selenium - 關鍵字驅動框架
- Selenium - 資料驅動框架
- Selenium - 混合驅動框架
- Selenium - SSL 證書錯誤
- Selenium - 替代方案
- Selenium 有用資源
- Selenium - 問答
- Selenium - 快速指南
- Selenium - 有用資源
- Selenium - 自動化實踐
- Selenium - 討論
Selenium WebDriver - 頁面物件模型
使用 Selenium WebDriver 開發的測試用例可以透過在自動化測試中採用設計模式來改進。頁面物件模型 (也稱為 POM) 是最常用的設計模式之一,可用於使用 Selenium WebDriver 建立自動化測試。
什麼是頁面物件模型?
頁面物件模型是一種在 Selenium 測試用例開發中採用的設計模式,用於將 Web 元素和測試用例分離到不同的類檔案中。頁面物件類似於面向物件的類,類似於被測應用程式的介面。
實際的測試用例利用物件類的方法與 Web 應用程式進行互動。在開發測試用例後,如果將來 Web 元素的任何屬性發生更改,則只需要在頁面物件級別進行必要的更改,而無需在測試用例級別進行更改。
同樣,如果應用程式中 Web 頁面的 Web 元素已新增,則應首先將其新增到一個且只有一個專用位置,在該位置宣告所有頁面級別的物件。
在基於頁面物件模型設計測試用例時,應用程式中的每個頁面都被視為一個單獨的類檔案。在該獨立的類檔案中,將提供特定於該頁面的所有 Web 元素。
測試用例的驗證和斷言儲存在特定的測試類檔案中,而不是頁面物件類檔案中。因此,任何類檔案之間都沒有相互依賴關係。
使用頁面物件模型的好處
使用頁面物件模型的好處如下所示:
- 測試用例程式碼、頁面特定功能、Web 元素定位器等等之間有明確的區別。
- 頁面物件模型提供了類似於物件儲存庫的功能,所有 Web 元素以及要對其執行的操作都儲存在一個特定位置,而不是在測試用例中的多個位置宣告。
- 如果發現 Web 元素的屬性發生更改,則只需要在一個位置修改與該元素相關的修改,而不是在多個位置修改。因此,它易於維護。
- 由於維護單獨且獨立的類檔案,因此為一個頁面開發的測試程式碼可以重複用於另一個測試用例。因此節省大量資源。
- 由於根據應用程式上的 Web 頁面開發單獨且獨立的類檔案,因此只需瀏覽類檔案即可使程式碼非常易於閱讀和理解。
- 每個獨特的 Web 元素僅宣告一次。
示例
單擊歡迎頁面上的“新使用者”按鈕。
單擊新使用者後,我們將導航到註冊頁面,文字為歡迎,註冊。
這裡,我們將有頁面類 - WelcomePage.java 和 RegisterPage.java,它們將包含這兩個頁面上的 Web 元素以及對這些元素的操作。我們還將有測試類 - WelcomePageTest.java,它將是實際的測試用例,以及與測試用例相關的斷言。所有測試類都位於同一個POM包中。
頁面類 WelcomePage.java 的程式碼實現。
package POM;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
public class WelcomePage {
WebDriver driver;
By text = By.xpath("//*[@id='signInForm']/h1");
By btn = By.xpath("//*[@id='signInForm']/div[3]/a");
public WelcomePage(WebDriver driver) {
this.driver=driver;
}
public String verifyPageHeading() {
String getHeadtext = driver.findElement(text).getText();
return getHeadtext;
}
public void clickOnNewUser() {
driver.findElement(btn).click();
}
}
程式碼實現 - RegisterPage.java。
package POM;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
public class RegisterPage {
WebDriver driver;
By text = By.xpath("//*[@id='signupForm']/h1");
public RegisterPage(WebDriver driver) {
this.driver=driver;
}
public String verifyPageHeading() {
String getHeadtext = driver.findElement(text).getText();
return getHeadtext;
}
}
程式碼實現 - WelcomePageTest.java。
package POM;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.*;
import java.util.concurrent.TimeUnit;
import static org.testng.Assert.assertEquals;
public class WelcomePageTest {
WebDriver driver;
WelcomePage objWelcomePage;
RegisterPage objRegisterPage;
@BeforeTest
public void setup() {
// Initiate the Webdriver
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() {
// object of WelcomePage page class
objWelcomePage = new WelcomePage(driver);
String text = objWelcomePage.verifyPageHeading();
System.out.println("Page heading in Welcome Page: " + text);
// assertions to test case
assertEquals("Welcome, Login In", text);
}
@Test(priority = 2)
public void moveToRegisterPage() {
objWelcomePage = new WelcomePage(driver);
objWelcomePage.clickOnNewUser();
}
@Test(priority = 3)
public void verifyRegisterPageHeading() {
// object of RegisterPage page class
objRegisterPage = new RegisterPage(driver);
String text = objRegisterPage.verifyPageHeading();
System.out.println("Page heading in Register Page: " + text);
// assertions to test case
assertEquals("Welcome,Register", text);
}
@AfterTest
public void teardown() {
// quitting browser
driver.quit();
}
}
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>
輸出
Page heading in Welcome Page: Welcome, Login In Page heading in Register Page: Welcome,Register =============================================== Default Suite Total tests run: 3, Passes: 3, Failures: 0, Skips: 0 =============================================== Process finished with exit code 0
首先,我們在歡迎頁面和註冊頁面上識別了元素,並在頁面類 WelcomePage.java 和 RegisterPage.java 中對這些 Web 元素執行了操作。
在測試類 WelcomePageTest.java 中,我們建立了頁面類的物件,以實現具有斷言的測試用例的完整流程。
我們使用了 TestNG 測試框架來實現頁面物件模型,並在控制檯中檢索頁面標題和訊息 - 歡迎頁面標題:歡迎,登入和註冊頁面標題:歡迎,註冊。
控制檯中的輸出顯示總共執行測試:3,因為有三個方法具有@Test 註解 - verifyWelcomePageHeading()、moveToRegisterPage() 和 verifyRegisterPageHeading()。
最後,收到訊息透過:3和程序已完成,退出程式碼為 0,表示程式碼成功執行。
結論
本教程總結了關於 Selenium WebDriver 頁面物件模型的全面介紹。我們從描述頁面物件模型開始,介紹了頁面物件模型的好處,並透過一個示例說明了如何將其與 Selenium 一起使用。這使您能夠深入瞭解頁面物件模型。明智的做法是繼續練習您所學的內容,並探索與 Selenium 相關的其他內容,以加深您的理解並拓寬您的視野。
