Selenium 简明教程
Selenium - Keyword Driven Framework
Selenium Webdriver 可用于开发基于关键词驱动框架的测试脚本。关键词驱动框架主要用于创建功能测试用例,其中测试用例的设计和开发之间有明确的分界。
Selenium Webdriver can be used to develop test scripts which are based on the keyword driven framework. A keyword driven framework is mostly used to create functional test cases where there is a clear demarcation between the design of the test cases and development.
关键词驱动框架包含一系列执行一组操作的关键词和动作。这些关键词可以在同一个测试用例中多次重用。关键词被赋予自解释的名称,并描述它们打算对正在测试的应用程序执行的操作。因此,关键词驱动很容易用于自动化测试用例。
A keyword driven framework comprises a group of keywords and actions that performs a set of operations. These keywords can be reused multiple times in the same test case. The keywords are given names such that they are self-explanatory, and describe what the actions they are intended to perform on the application under test. Thus a keyword driven can be adopted very easily to automate test cases.
Keyword Driven Testing Framework
关键词测试或关键词驱动框架是在自动化测试用例时遵循的一组准则,其中隐藏了实现逻辑和技术信息。关键词驱动框架的使用和维护非常简单。
A keyword testing or a keyword driven framework is a set of guidelines followed while automating test cases, in which the implementation logic and the technical information are hidden. The usage and maintenance of a keyword driven framework is very easy.
一组特定且有目的的关键词必须作为测试用例准备的一部分纳入其中,以对应用程序执行特定任务或任务。所有这些关键词都描述在通用存储库或可重用库中。
A group of keywords in a particular and purposeful order has to be incorporated as a part of test case preparation to perform a particular task or tasks on the application. All these keywords are described in the common repository or a reusable library.
Need of Keyword Driven Testing Framework
在关键词驱动测试框架中,测试用例和测试自动化的编码逻辑之间有明确的划分。它有以下用法:-
In the keyword driven testing framework, there is a clear partition between the test case and the coding logic of test automation. It has the below usages −
-
A keyword driven framework can be used effectively by anyone without requiring technical expertise. Every member of the team can participate in the testing which helps to ensure a quality product and development of test cases at a faster rate.
-
In case an update is required, the changes are made at the implementation logic and there is no need to rewrite or modify the test cases.
-
In the keyword driven framework, meaningful and consistent keywords are used in the entire test case, thus ensuring uniformity in the test cases.
-
A new test case can be created in the keyword driven framework easily by simply updating, incorporating or changing the order of the keywords.
Tools to Create Keyword Driven Testing Framework
可以使用以下工具来创建关键字驱动框架 -
The below tools can be used to create a keyword driven framework −
-
Robot Framework
-
Selenium
-
Playwright
Advantages of Keyword Driven Testing Framework
以下是关键字驱动测试框架的优点 -
The below are the advantages of keyword driven testing framework −
-
No technical knowledge required to create, execute, and maintain the test cases built on a keyword driven testing framework.
-
The test cases can be reused to a large extent in a keyword driven testing framework.
-
There is clear separation among the test cases, data, and the implementation logic and functions, so any change required in any one of the sections does not impact others.
-
The test cases developed on the keyword driven testing framework does not have any implementation logic available, hence it gives readability to the test cases.
Disadvantages of Keyword Driven Testing Framework
以下是关键字驱动测试框架的缺点 -
The below are the disadvantages of keyword driven testing framework −
-
Implementation of the keyword driven testing framework and its logic require high technical skills.
-
The unnecessary keywords created in the keyword driven testing framework may cause confusion.
-
Extensive planning and preparation are required to be done in the initial stages.
-
Periodic maintenance is required to be done both at the test case level and implementation layers in a keyword driven testing framework.
What Comprises a Keyword Driven Testing Framework?
一个关键字驱动框架包含下列项目 -
A keyword driven framework comprises of the below items −
-
Excel Sheet which contains the keywords.
-
Object Repository which contains the element locators.
-
Keyword Library which contains the implementation logic of the keywords.
-
Libraries which contain the common functions in the framework.
-
Test Data files which contain the data required for the test cases.
-
Driver Engine which controls and communicates with the test cases.
-
Any tool, for example Selenium which supports creation of a keyword driven framework.
-
Test Cases which are designed to test the application.
Example
举个例子,在下方页面中,我们要在使用关键字驱动的测试框架点击 Yes 旁边的单选按钮后获取并验证消息 - You have checked Yes 。
Let us take an example of the below page where we would retrieve and verify the message - You have checked Yes obtained after clicking the radio button beside Yes using a keyword driven testing framework.
Step 1 − 首先,我们会准备一个名为 TestCase.xlsx 的 excel 文档,其中包括此示例中的所有关键字(如 launchBrowser、open、click、verifyTest 和 quit),形式为一个测试用例。我们将该文件放置在项目中的 excel 包中。
Step 1 − First of all, we would prepare an excel called TestCase.xlsx as shown in the below image with all the keywords like launchBrowser, open, click, verifyTest, and quit in the form of a test case for the example. We had placed this file under the excel package in our project.
Step 2 − 我们将在 Utils 包中的 StaticDatas.java 类文件中存储 TestCase.xlsx 的路径和应用程序 URL 等其他环境值。
Step 2 − We would store the path of the TestCase.xlsx and other environment values like the application URL within the class file StaticDatas.java under the Utils package.
在 StaticDatas.java 中进行代码实现
Code Implementation in StaticDatas.java
package Utils;
public class StaticDatas {
public static final String exeData = "TestCase.xlsx";
public static final String URLToOpen = "https://www.tutorialspoint.com/selenium/practice/radio-button.php";
}
Step 3 − 我们将创建 TestCase.xlsx 中标识的关键字的方法,以执行我们要执行的操作,为此我们将在 Action 包中创建类文件 ActionsToPerform.java 。
Step 3 − We would create the methods for the keywords identified within the TestCase.xlsx to perform the actions to be performed for that we would create the class file ActionsToPerform.java under the Action package.
在 ActionsToPerform.java 中进行代码实现
Code Implementation in 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();
}
}
Step 4 − 我们需要读取 TestCase.xlsx 文件(使用 Apache POI Library)以获取应用程序要执行的操作所需的关键字,为此我们将在 ExcelUtils 包中创建 GetDataFromExcel.java 类文件。
Step 4 − We would need to read the TestCase.xlsx file(using the Apache POI Library) to get hold of the keywords identified to perform on the application for that we would create the class file GetDataFromExcel.java under the ExcelUtils package.
在 GetDataFromExcel.java 中进行代码实现
Code Implementation in 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) {
}
}
Step 5 − 我们需要从 GetDataFromExcel 类文件中调用方法 getExcel 以读取 TestCase.xlsx 文件和 ActionsToPerform 类文件的方法,为此我们将在 DriveEngine 包中创建 ActualTest.java 类文件。
Step 5 − We would need to call the method getExcel from the GetDataFromExcel class file to read the TestCase.xlsx file and methods of the ActionsToPerform class file for that we would create the class file ActualTest.java under the DriveEngine package.
在 ActualTest.java 中进行代码实现
Code Implementation in 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");
}
}
Step 6 − 添加的依赖关系已添加到 pom.xml 中。
Step 6 − Dependencies added to 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>
Output
Message is: You have checked Yes
Keyword driven testing framework executed successfully
Process finished with exit code 0
在上述示例中,我们已实现一个关键字驱动的测试框架来验证和在控制台中获取消息 - You have checked Yes 。
In the above example, we had implemented a keyword driven testing framework to verify and get the message in the console - You have checked Yes.
最后,收到了消息 Process finished with exit code 0 ,表示代码成功执行。
Finally, the message Process finished with exit code 0 was received, signifying successful execution of the code.
这完成了我们对 Selenium Webdriver - 关键字驱动框架教程的全面讲解。我们从描述什么是关键字驱动框架开始,包括为什么使用关键字驱动框架、关键字驱动框架使用的工具、关键字驱动框架的优点和缺点、关键字驱动框架包含哪些内容,并通过一个示例说明如何实现一个关键字驱动框架,以及 Selenium Webdriver 的结合。
This concludes our comprehensive take on the tutorial on Selenium Webdriver - Keyword Driven Framework. We’ve started with describing what is a keyword driven framework, why is a keyword driven framework used, which tools are used for a keyword driven framework, what are the advantages and disadvantages of a keyword driven framework, what comprises of a keyword driven framework, and walked through an example of how to implement a keyword driven framework along with Selenium Webdriver.
这使你能够深入了解 Selenium Webdriver 中的关键字驱动框架。最佳做法是继续实践你学到的知识,并探索与 Selenium 相关的其他内容,以加深你的理解并拓宽你的视野。
This equips you with in-depth knowledge of the Keyword Driven Framework in Selenium Webdriver. It is wise to keep practicing what you’ve learned and exploring others relevant to Selenium to deepen your understanding and expand your horizons.