• Selenium Video Tutorials

Selenium - 基於關鍵字驅動的框架



Selenium Webdriver 可用於開發基於關鍵字驅動框架的測試指令碼。關鍵字驅動框架主要用於建立功能測試用例,在這些用例中,測試用例的設計和開發之間有明確的界限。

關鍵字驅動框架包含一組關鍵字和操作,這些關鍵字和操作執行一組操作。這些關鍵字可以在同一個測試用例中重複使用多次。關鍵字的命名使其具有自解釋性,並描述了它們旨在對被測應用程式執行的操作。因此,關鍵字驅動可以非常容易地應用於自動化測試用例。

關鍵字驅動測試框架

關鍵字測試或關鍵字驅動框架是一組在自動化測試用例時遵循的準則,其中實現邏輯和技術資訊被隱藏。關鍵字驅動框架的使用和維護非常容易。

必須將一組特定且有目的的順序的關鍵字作為測試用例準備的一部分,以便在應用程式上執行特定任務或任務。所有這些關鍵字都描述在公共儲存庫或可重用庫中。

關鍵字驅動測試框架的必要性

在關鍵字驅動測試框架中,測試用例和測試自動化的編碼邏輯之間存在清晰的劃分。它具有以下用途 -

  • 任何人都可以使用關鍵字驅動框架有效地執行操作,而無需技術專業知識。團隊中的每個成員都可以參與測試,這有助於確保產品質量並更快地開發測試用例。

  • 如果需要更新,則可以在實現邏輯中進行更改,無需重寫或修改測試用例。

  • 在關鍵字驅動框架中,整個測試用例中使用有意義且一致的關鍵字,從而確保測試用例的統一性。

  • 透過簡單地更新、合併或更改關鍵字的順序,可以在關鍵字驅動框架中輕鬆建立新的測試用例。

建立關鍵字驅動測試框架的工具

以下工具可用於建立關鍵字驅動框架 -

  • Robot Framework

  • Selenium

  • Playwright

關鍵字驅動測試框架的優勢

以下是關鍵字驅動測試框架的優勢 -

  • 無需技術知識即可建立、執行和維護基於關鍵字驅動測試框架構建的測試用例。

  • 在關鍵字驅動測試框架中,測試用例可以在很大程度上重複使用。

  • 測試用例、資料和實現邏輯與函式之間有明確的區分,因此任何一個部分所需的更改都不會影響其他部分。

  • 在關鍵字驅動測試框架上開發的測試用例沒有任何實現邏輯可用,因此它使測試用例具有可讀性。

關鍵字驅動測試框架的缺點

以下是關鍵字驅動測試框架的缺點 -

  • 關鍵字驅動測試框架及其邏輯的實施需要較高的技術技能。

  • 在關鍵字驅動測試框架中建立的不必要的關鍵字可能會造成混淆。

  • 在初始階段需要進行廣泛的計劃和準備。

  • 在關鍵字驅動測試框架中,需要定期維護測試用例級別和實現層。

關鍵字驅動測試框架包含什麼?

關鍵字驅動框架包含以下專案 -

  • 包含關鍵字的 Excel 表格。

  • 包含元素定位器的物件儲存庫。

  • 包含關鍵字實現邏輯的關鍵字型檔。

  • 包含框架中常用函式的庫。

  • 包含測試用例所需資料的資料檔案。

  • 控制並與測試用例通訊的驅動程式引擎。

  • 任何支援建立關鍵字驅動框架的工具,例如 Selenium。

  • 用於測試應用程式的測試用例。

示例

讓我們以以下頁面為例,在該頁面中,我們將使用關鍵字驅動測試框架檢索並驗證單擊“是”旁邊的單選按鈕後獲得的訊息 - **您已選中是**。

Selenium Keyword Driven Framework 1

**步驟 1** - 首先,我們將準備一個名為 **TestCase.xlsx** 的 Excel 檔案,如下面的圖片所示,其中包含所有關鍵字,如 launchBrowser、open、click、verifyTest 和 quit,以示例的形式組成一個測試用例。我們將此檔案放在專案中的 **excel** 包中。

Selenium Keyword Driven Framework 2

**步驟 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 Keyword Driven Framework 3

至此,我們對 Selenium Webdriver 關鍵字驅動框架教程的全面概述就結束了。我們從描述什麼是關鍵字驅動框架、為什麼要使用關鍵字驅動框架、哪些工具用於關鍵字驅動框架、關鍵字驅動框架的優缺點、關鍵字驅動框架包含哪些內容開始,並逐步演示瞭如何使用 Selenium Webdriver 實現關鍵字驅動框架的示例。

這使您深入瞭解了 Selenium Webdriver 中的關鍵字驅動框架。建議您持續練習所學內容,並探索與 Selenium 相關的其他內容,以加深理解並拓寬視野。

廣告