
- 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 - 斷言/驗證方法
- Selenium - 定位策略
- Selenium - 指令碼除錯
- Selenium - 驗證點
- Selenium - 模式匹配
- Selenium - JSON 資料檔案
- Selenium - 瀏覽器執行
- Selenium - 使用者擴充套件
- Selenium - 程式碼匯出
- Selenium - 程式碼發射
- Selenium - JavaScript 函式
- Selenium - 外掛
- Selenium WebDriver 教程
- Selenium - 簡介
- Selenium WebDriver 與 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 Webdriver 可用於開發基於混合驅動框架的測試指令碼。混合驅動框架是關鍵字驅動框架和資料驅動框架的組合。因此,在混合框架中,我們結合了關鍵字驅動框架和資料驅動框架的優點。
為什麼使用混合框架?
採用混合框架是為了利用關鍵字驅動框架和資料驅動框架的基本功能。它有助於使測試用例更具靈活性,並且各個功能可以獨立存在而不影響其他功能。使用混合框架建立的測試用例更易於維護,並且向此類框架新增新的測試用例所需的時間更少。
混合框架的優點
混合框架的優點如下所示:
- 它是免費和開源的。
- 可用於多個平臺、瀏覽器和裝置。
- 易於維護和擴充套件。
混合框架的缺點
混合框架的缺點如下所示:
- 建立混合框架需要技術專業知識。
- 難以將使用混合框架建立的測試用例移植到另一個應用程式。
- 測試指令碼的可讀性可能會變得具有挑戰性。
混合框架的基本元件
混合框架的基本元件在下面的圖表中進行了描述:

如上圖所示,混合框架分為兩個階段 - 實現和開發階段,以及報告生成階段。報告生成階段可以進一步擴充套件以包含持續整合階段。對於持續整合階段,我們可以藉助 Jenkins 和其他工具以及 Git(用於程式碼的版本控制)。
示例
讓我們以以下頁面為例,在此頁面中,我們將名字輸入為Ram,姓氏輸入為Ganesh,然後使用混合框架驗證註冊頁面中輸入的值。

根據之前顯示的圖表,我們將建立一個名為Hybrid的專案,該專案將開發用於建立混合框架的資料夾和包。

先決條件
- 在系統中安裝 Java(版本高於 8),並使用命令檢查它是否存在:java -version。如果安裝已成功完成,則將顯示已安裝的 Java 版本。
- 在系統中安裝 Maven,並使用命令檢查它是否存在:mvn -version。如果安裝已成功完成,則將顯示已安裝的 Maven 版本。
- 安裝任何 IDE,例如 Eclipse、IntelliJ 等。
- 從連結 TestNG 新增 TestNG 依賴項。
- 從連結 Selenium Java 新增 Selenium Java 依賴項。
- 從連結 Maven Artifacts 新增 Log4j 依賴項。
- 從連結 Apache POI Common 新增 Apache POI Common 依賴項。
- 從連結 Apache POI API Based 新增基於 OPC 和 OOXML 架構的 Apache POI API 依賴項。
- 儲存包含所有依賴項的 pom.xml 並更新 Maven 專案
步驟 1 - 首先,準備一個名為Data.xlsx的 Excel 表格,如下面的圖片所示,其中包含名字和姓氏欄位的Ram和Ganesh資料。將此檔案放在專案中的TestData資料夾下。

步驟 2 - 將環境值(如應用程式 URL)儲存在config.properties檔案中,該檔案包含Configuration資料夾下鍵值對中的資料。
baseUrl=https://tutorialspoint.tw/selenium/practice/register.php
步驟 3 - 建立配置檔案 - log4j2.properties 檔案。在這裡,我們將提供設定。在resources資料夾下建立了一個名為log4j2.properties的檔案。
log4j2.properties 檔案中的配置。
name=PropertiesConfig property.filename = logs appenders = console, file appender.console.type = Console appender.console.name = STDOUT appender.console.layout.type = PatternLayout appender.console.layout.pattern = [%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n appender.file.type = File appender.file.name = LOGFILE appender.file.fileName=${filename}/LogsGenerated.log appender.file.layout.type=PatternLayout appender.file.layout.pattern=[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n loggers=file logger.file.name=Logs logger.file.level = debug logger.file.appenderRefs = file logger.file.appenderRef.file.ref = LOGFILE rootLogger.level = debug rootLogger.appenderRefs = stdout rootLogger.appenderRef.stdout.ref = STDOUT
步驟 4 - 建立實用程式檔案以訪問測試用例中config.properties檔案和Data.xlsx Excel 檔案中的資料。我們將有ReadingConfigValues.java類檔案訪問config.properties檔案中的配置值,以及ReadExcel.java類檔案訪問Data.xlsx Excel 檔案中的測試資料。這兩個類檔案都放置在utilities包下。
程式碼實現
package utilities; import java.io.File; import java.io.FileInputStream; import java.util.Properties; public class ReadingConfigValues { Properties p; public ReadingConfigValues(){ // loading properties file with file location File s = new File("./Configuration/config.properties"); try { // getting the key value pair FileInputStream fileInputStream = new FileInputStream(s); p = new Properties(); p.load(fileInputStream); } catch (Exception e){ System.out.println("Exception encountered: " + e); } } public String getURL() { // get URL from .properties file String url = p.getProperty("baseUrl"); return url; } }
程式碼實現
package utilities; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.util.ArrayList; public class ReadExcel { public ArrayList<String> readingExcel() throws IOException { int k; int l; // identify location of .xlsx file File f = new File("./TestData/Data.xlsx"); FileInputStream i = new FileInputStream(f); // instance of XSSFWorkbook XSSFWorkbook w = new XSSFWorkbook(i); // create sheet in XSSFWorkbook with name Details1 XSSFSheet s = w.getSheet("Details1"); // get row and column numbers int r = s.getLastRowNum() + 1; int c = s.getRow(0).getLastCellNum(); ArrayList<String> names = new ArrayList<>(); // iterating through rows for (k = 1; k < r; k++) { Row rw = s.getRow(k); String name = ""; // iterating through columns for (l = 0; l < c; l++) { Cell cell = rw.getCell(l); if("" != name){ name += "," + cell.getStringCellValue(); } else { name += cell.getStringCellValue(); } } // storing excel values read to arraylist names.add(name); } // closing excel file w.close(); return names; } }
步驟 5 - 在pageObject包下建立一個頁面類RegisterPage.java,其中我們將包含 Web 元素 - 輸入框以輸入資料以及要對這些元素執行的操作。
package pageObjects; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.support.FindBy; import org.openqa.selenium.support.PageFactory; public class RegisterPage { WebDriver driver; @FindBy(xpath = "//*[@id='firstname']") WebElement txtFname; @FindBy(xpath = "//*[@id='lastname']") WebElement txtLname; @FindBy(xpath = "//*[@id='signupForm']/div[5]/input") WebElement btnRegister; public RegisterPage(WebDriver driver) { this.driver=driver; // initializing all page objects PageFactory.initElements(driver, this); } public void inputFirstname(String fname) { txtFname.sendKeys(fname); } public void inputLastname(String lname) { txtLname.sendKeys(lname); } public void clickRegister() { btnRegister.click(); } public String getFirstName(){ return txtFname.getAttribute("value"); } public String getLastName(){ return txtLname.getAttribute("value"); } }
步驟 6 - 在TestNG.xml中新增以下配置,並具有在 Chrome 和 Edge 瀏覽器中執行測試用例的功能。
<?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="RegisterUser.java"> <parameter name="browser" value="Chrome"></parameter> <classes> <class name="testCases.RegisterUserTest"></class> </classes> </test> </suite>
步驟 7 - 在testCases包下建立測試類 - BaseClass.java和RegisterUserTest.java,我們將在這裡編寫實際的測試用例。
程式碼實現
package testCases; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.edge.EdgeDriver; import org.testng.annotations.AfterClass; import org.testng.annotations.BeforeClass; import org.testng.annotations.Parameters; import utilities.ReadingConfigValues; import utilities.ReadExcel; public class BaseClass { // reading values from external files ReadingConfigValues readingConfigValues = new ReadingConfigValues(); ReadExcel readExcel = new ReadExcel(); public String baseUrl = readingConfigValues.getURL(); public static WebDriver driver; public static Logger logger; @BeforeClass @Parameters("browser") public void setup(String browser){ logger = LogManager.getLogger(BaseClass.class); logger.info("Application Launched"); // Initiate browser driver as per browser value if (browser.equalsIgnoreCase("Chrome")) { driver = new ChromeDriver(); System.out.println("Browser opened in Chrome"); } else if (browser.equalsIgnoreCase("Edge")) { driver = new EdgeDriver(); System.out.println("Browser opened in Edge"); } // launch application driver.get(baseUrl); } @AfterClass public void tearDown(){ // quitting browser driver.quit(); } }
程式碼實現
package testCases; import org.testng.annotations.Test; import pageObjects.RegisterPage; import java.io.IOException; import java.util.ArrayList; import java.util.concurrent.TimeUnit; import static org.testng.Assert.assertTrue; public class RegisterUserTest extends BaseClass { @Test public void registerUser() throws IOException { logger.info("Reading values from excel from registration"); // reading values of excel in arraylist ArrayList<String> result = readExcel.readingExcel(); // adding implicit wait of 12 secs driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); // object of RegisterPage page class RegisterPage registerPage = new RegisterPage(driver); logger.info("Starting registration"); // input first and last name for (int i = 0; i <result.size(); i++) { String[] names = result.get(i).split(","); registerPage.inputFirstname(names[0]); registerPage.inputLastname(names[1]); } // click register button registerPage.clickRegister(); logger.info("Verification of entered values"); // verify value input in the first name if (registerPage.getFirstName().equalsIgnoreCase("Ram")){ // assertions to test case assertTrue(true); } else { assertTrue(false); } // verify value input in the last name if (registerPage.getLastName().equalsIgnoreCase("Ganesh")){ // assertions to test case assertTrue(true); } else { assertTrue(false); } } }
步驟 8 - 將以下依賴項新增到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.apache.poi/poi --> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>5.2.5</version> </dependency> <!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml --> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>5.2.5</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> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-api</artifactId> <version>2.23.1</version> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-core</artifactId> <version>2.23.1</version> </dependency> <!-- https://mvnrepository.com/artifact/com.aventstack/extentreports --> <dependency> <groupId>com.aventstack</groupId> <artifactId>extentreports</artifactId> <version>5.1.1</version> </dependency> </dependencies> </project>
步驟 9 - 透過右鍵單擊TestNG.xml檔案並選擇“執行 TestNG.xml”選項來執行測試。
輸出
Browser opened in Chrome =============================================== All Test Suite Total tests run: 1, Passes: 1, Failures: 0, Skips: 0 =============================================== Process finished with exit code 0
在上述示例中,我們藉助 TestNG 測試框架和頁面物件模型設計模式建立了一個測試,並根據從TestNG.xml 檔案傳遞的Chrome 引數在 Chrome 瀏覽器中啟動了應用程式。然後從Data.xlsx 檔案獲取測試資料,並將其輸入註冊頁面中的“名”和“姓”欄位。
控制檯中的結果顯示總測試執行次數:1,因為只有一個帶有 @Test 註解的方法 registerUser()。
最後,收到訊息透過:1和程序已完成,退出程式碼為 0,表示程式碼已成功執行。
步驟 10 - 重新整理專案,專案結構中應該會生成一個名為test-output的新資料夾。
步驟 11 - 右鍵單擊emailable-report.html,然後選擇在瀏覽器中開啟的選項。

報告在瀏覽器中開啟,顯示測試類名稱 - RegisterUser.java,以及透過、跳過、失敗的總數、測試持續時間等。此外,測試方法名稱 registerUser 也包含在報告中。
結論
本教程到此結束,我們全面介紹了 Selenium Webdriver 混合驅動框架。我們首先描述了為什麼使用混合驅動框架,以及混合驅動框架的優點、缺點和基本元件,並透過一個示例演示瞭如何結合 Selenium Webdriver 實現混合驅動框架。
這使您深入瞭解 Selenium Webdriver 中的混合驅動框架。明智的做法是不斷練習您所學到的知識,並探索與 Selenium 相關的其他內容,以加深您的理解並擴充套件您的視野。