- 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 與 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 - 基於關鍵字驅動的框架
Selenium Webdriver 可用於開發基於關鍵字驅動框架的測試指令碼。關鍵字驅動框架主要用於建立功能測試用例,在這些用例中,測試用例的設計和開發之間有明確的界限。
關鍵字驅動框架包含一組關鍵字和操作,這些關鍵字和操作執行一組操作。這些關鍵字可以在同一個測試用例中重複使用多次。關鍵字的命名使其具有自解釋性,並描述了它們旨在對被測應用程式執行的操作。因此,關鍵字驅動可以非常容易地應用於自動化測試用例。
關鍵字驅動測試框架
關鍵字測試或關鍵字驅動框架是一組在自動化測試用例時遵循的準則,其中實現邏輯和技術資訊被隱藏。關鍵字驅動框架的使用和維護非常容易。
必須將一組特定且有目的的順序的關鍵字作為測試用例準備的一部分,以便在應用程式上執行特定任務或任務。所有這些關鍵字都描述在公共儲存庫或可重用庫中。
關鍵字驅動測試框架的必要性
在關鍵字驅動測試框架中,測試用例和測試自動化的編碼邏輯之間存在清晰的劃分。它具有以下用途 -
任何人都可以使用關鍵字驅動框架有效地執行操作,而無需技術專業知識。團隊中的每個成員都可以參與測試,這有助於確保產品質量並更快地開發測試用例。
如果需要更新,則可以在實現邏輯中進行更改,無需重寫或修改測試用例。
在關鍵字驅動框架中,整個測試用例中使用有意義且一致的關鍵字,從而確保測試用例的統一性。
透過簡單地更新、合併或更改關鍵字的順序,可以在關鍵字驅動框架中輕鬆建立新的測試用例。
建立關鍵字驅動測試框架的工具
以下工具可用於建立關鍵字驅動框架 -
Robot Framework
Selenium
Playwright
關鍵字驅動測試框架的優勢
以下是關鍵字驅動測試框架的優勢 -
無需技術知識即可建立、執行和維護基於關鍵字驅動測試框架構建的測試用例。
在關鍵字驅動測試框架中,測試用例可以在很大程度上重複使用。
測試用例、資料和實現邏輯與函式之間有明確的區分,因此任何一個部分所需的更改都不會影響其他部分。
在關鍵字驅動測試框架上開發的測試用例沒有任何實現邏輯可用,因此它使測試用例具有可讀性。
關鍵字驅動測試框架的缺點
以下是關鍵字驅動測試框架的缺點 -
關鍵字驅動測試框架及其邏輯的實施需要較高的技術技能。
在關鍵字驅動測試框架中建立的不必要的關鍵字可能會造成混淆。
在初始階段需要進行廣泛的計劃和準備。
在關鍵字驅動測試框架中,需要定期維護測試用例級別和實現層。
關鍵字驅動測試框架包含什麼?
關鍵字驅動框架包含以下專案 -
包含關鍵字的 Excel 表格。
包含元素定位器的物件儲存庫。
包含關鍵字實現邏輯的關鍵字型檔。
包含框架中常用函式的庫。
包含測試用例所需資料的資料檔案。
控制並與測試用例通訊的驅動程式引擎。
任何支援建立關鍵字驅動框架的工具,例如 Selenium。
用於測試應用程式的測試用例。
示例
讓我們以以下頁面為例,在該頁面中,我們將使用關鍵字驅動測試框架檢索並驗證單擊“是”旁邊的單選按鈕後獲得的訊息 - **您已選中是**。
**步驟 1** - 首先,我們將準備一個名為 **TestCase.xlsx** 的 Excel 檔案,如下面的圖片所示,其中包含所有關鍵字,如 launchBrowser、open、click、verifyTest 和 quit,以示例的形式組成一個測試用例。我們將此檔案放在專案中的 **excel** 包中。
**步驟 2** - 我們將在 **Utils** 包下的類檔案 **StaticDatas.java** 中儲存 **TestCase.xlsx** 的路徑和其他環境值,例如應用程式 URL。
**StaticDatas.java** 中的程式碼實現
package Utils;
public class StaticDatas {
public static final String exeData = "TestCase.xlsx";
public static final String URLToOpen = "https://tutorialspoint.tw/selenium/practice/radio-button.php";
}
**步驟 3** - 我們將在 **Action** 包下的類檔案 **ActionsToPerform.java** 中為 **TestCase.xlsx** 中識別的關鍵字建立方法,以執行要執行的操作。
**ActionsToPerform.java** 中的程式碼實現
package Action;
import Utils.StaticDatas;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.concurrent.TimeUnit;
import static org.testng.Assert.assertEquals;
public class ActionsToPerform {
public static WebDriver driver;
public void launchBrowser(){
// Initiate the Webdriver
driver = new ChromeDriver();
// adding implicit wait of 15 secs
driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
}
public void open(){
//launch URL
driver.get(StaticDatas.URLToOpen);
}
public void click(){
// identify element then click
WebElement radioBtn = driver.findElement(By.xpath("/html/body/main/div/div/div[2]/form/div[1]/input"));
radioBtn.click();
}
public void verifyText(){
// identify element
WebElement chkMessage = driver.findElement(By.xpath("//*[@id='check']"));
// get element text
String text = chkMessage.getText();
System.out.println("Message is: " + text);
// verify message
assertEquals("You have checked Yes", text);
}
public void quit(){
// quitting browser
driver.quit();
}
}
**步驟 4** - 我們需要讀取 **TestCase.xlsx** 檔案(使用 Apache POI 庫)以獲取要對應用程式執行的已識別關鍵字,為此,我們將建立 **ExcelUtils** 包下的類檔案 **GetDataFromExcel.java**。
**GetDataFromExcel.java** 中的程式碼實現
package ExcelUtils;
import Utils.StaticDatas;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
public class GetDataFromExcel {
public ArrayList getExcel(int c) throws IOException {
// get excel file path
String f = StaticDatas.exeData;
// load the excel file
File fl = new File(f);
FileInputStream fileInputStream = new FileInputStream(fl);
// instance of XSSFWorkbook
XSSFWorkbook w = new XSSFWorkbook(fileInputStream);
// create sheet in XSSFWorkbook with name TestCase1
XSSFSheet s = w.getSheet("TestCase1");
// iterating through rows in sheet
Iterator r = s.rowIterator();
// moving to next row
r.next();
ArrayList<String> arr = new ArrayList();
// iterate till next row data exists
while(r.hasNext()){
Row rw = (Row) r.next();
// move to next cell in same row
Cell cell = rw.getCell(c);
// get cell data
String cellValue = cell.getStringCellValue();
// store cell data in arraylist
arr.add(cellValue);
// store all cell data in subsequent rows in arraylist
arr.add(((Row) r.next()).getCell(c).getStringCellValue());
}
System.out.println("Get all cell data in the list : " + arr);
return arr;
}
public void getFile(int j) {
}
}
**步驟 5** - 我們需要從 **GetDataFromExcel** 類檔案呼叫方法 **getExcel** 來讀取 **TestCase.xlsx** 檔案,以及 **ActionsToPerform** 類檔案的方法,為此,我們將建立 **DriveEngine** 包下的類檔案 **ActualTest.java**。
**ActualTest.java** 中的程式碼實現
package DriverEngine;
import Action.ActionsToPerform;
import ExcelUtils.GetDataFromExcel;
public class ActualTest {
public static void main(String[] args) {
GetDataFromExcel getDataFromExcel = new GetDataFromExcel();
// get column number of containing keywords in the excel
getDataFromExcel.getFile(4);
// get the keyword actions
ActionsToPerform actionsToPerform = new ActionsToPerform();
// execute the test steps starting with browser launch
actionsToPerform.launchBrowser();
// open URL
actionsToPerform.open();
// click radio button
actionsToPerform.click();
// verify message
actionsToPerform.verifyText();
// quit browser
actionsToPerform.quit();
System.out.println("Keyword driven testing framework executed successfully");
}
}
步驟 6 − 將依賴項新增到 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>
</dependencies>
</project>
輸出
Message is: You have checked Yes Keyword driven testing framework executed successfully Process finished with exit code 0
在上面的示例中,我們實現了一個關鍵字驅動的測試框架,以驗證並在控制檯中獲取訊息 - 您已選中是。
最終,收到了訊息程序已結束,退出程式碼為 0,表示程式碼已成功執行。
至此,我們對 Selenium Webdriver 關鍵字驅動框架教程的全面概述就結束了。我們從描述什麼是關鍵字驅動框架、為什麼要使用關鍵字驅動框架、哪些工具用於關鍵字驅動框架、關鍵字驅動框架的優缺點、關鍵字驅動框架包含哪些內容開始,並逐步演示瞭如何使用 Selenium Webdriver 實現關鍵字驅動框架的示例。
這使您深入瞭解了 Selenium Webdriver 中的關鍵字驅動框架。建議您持續練習所學內容,並探索與 Selenium 相關的其他內容,以加深理解並拓寬視野。
