这个 Selenium框架 是一种代码结构,它使代码维护变得简单而高效。如果没有框架,用户可以将“c”框架产生了一些有益的结果,比如增加了代码可重用性、更高的可移植性、降低了脚本维护成本、更好的代码可读性等。
Selenium WebDriver主要创建了三种类型的框架来自动化手动测试用例
Selenium中的数据驱动框架 是一种将数据集与测试用例分开的方法。一旦数据集从测试用例中分离出来,就可以很容易它用于从外部文件获取测试用例和套件,如Excel、.csv、.xml或某些数据库表。
为了读取或写入Excel,Apache提供了一个非常著名的库POI。此库足够读写两个库 XLS 和 xlsx Excel的文件格式。
阅读 XLS 文件,一个 HSSF 实现由POI库提供。
阅读 xlsx, XSSF 实现 POI 库 将是我们的选择。让我们详细研究一下这些实现。
我们已经在我们的上一个教程
Selenium中的关键字驱动框架 是一种用于通过分隔常用函数和指令集的关键字来加速自动化测试的方法。用户可以轻松地控制和指定他们想要测试的功能。
下面是完整框架的外观
如所见,它是一个5步框架。让我们逐步详细地研究一下
步骤1)
步骤2)
步骤3) ReadGuru99ExcelFile.java会将此数据传递给驱动程序脚本Execute.java
步骤4)
注: 可能会想,我们为什么需要创建对象存储库。对于对象存储库,只需在存储库中进行一次更改。
步骤5)
整个项目将看起来像-
让我们来看一个例子:
object.properties
url= [http://www.itxiaonv.com/V4/](http://www.itxiaonv.com/V4/)
username=uid
password=password
title=barone
loginButton=btnLogin
resetButton=btnReset
package excelExportAndFileIO;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class ReadGuru99ExcelFile {
public Sheet readExcel(String filePath,String fileName,String sheetName) throws IOException{
//Create a object of File class to open xlsx file
File file = new File(filePath+"\\"+fileName);
//Create an object of FileInputStream class to read excel file
FileInputStream inputStream = new FileInputStream(file);
Workbook guru99Workbook = null;
//Find the file extension by spliting file name in substing and getting only extension name
String fileExtensionName = fileName.substring(fileName.indexOf("."));
//Check condition if the file is xlsx file
if(fileExtensionName.equals(".xlsx")){
//If it is xlsx file then create object of XSSFWorkbook class
guru99Workbook = new XSSFWorkbook(inputStream);
}
//Check condition if the file is xls file
else if(fileExtensionName.equals(".xls")){
//If it is xls file then create object of XSSFWorkbook class
guru99Workbook = new HSSFWorkbook(inputStream);
}
//Read sheet inside the workbook by its name
Sheet guru99Sheet = guru99Workbook.getSheet(sheetName);
return guru99Sheet;
}
}
package operation;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class ReadObject {
Properties p = new Properties();
public Properties getObjectRepository() throws IOException{
//Read object repository file
InputStream stream = new FileInputStream(new File(System.getProperty("user.dir")+"\\src\\objects\\object.properties"));
//load all objects
p.load(stream);
return p;
}
}
package operation;
import java.util.Properties;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
public class UIOperation {
WebDriver driver;
public UIOperation(WebDriver driver){
this.driver = driver;
}
public void perform(Properties p,String operation,String objectName,String objectType,String value) throws Exception{
System.out.println("");
switch (operation.toUpperCase()) {
case "CLICK":
//Perform click
driver.findElement(this.getObject(p,objectName,objectType)).click();
break;
case "SETTEXT":
//Set text on control
driver.findElement(this.getObject(p,objectName,objectType)).sendKeys(value);
break;
case "GOTOURL":
//Get url of application
driver.get(p.getProperty(value));
break;
case "GETTEXT":
//Get text of an element
driver.findElement(this.getObject(p,objectName,objectType)).getText();
break;
default:
break;
}
}
/
* Find element BY using object type and value
* @param p
* @param objectName
* @param objectType
* @return
* @throws Exception
*/
private By getObject(Properties p,String objectName,String objectType) throws Exception{
//Find by xpath
if(objectType.equalsIgnoreCase("XPATH")){
return By.xpath(p.getProperty(objectName));
}
//find by class
else if(objectType.equalsIgnoreCase("CLASSNAME")){
return By.className(p.getProperty(objectName));
}
//find by name
else if(objectType.equalsIgnoreCase("NAME")){
return By.name(p.getProperty(objectName));
}
//Find by css
else if(objectType.equalsIgnoreCase("CSS")){
return By.cssSelector(p.getProperty(objectName));
}
//find by link
else if(objectType.equalsIgnoreCase("LINK")){
return By.linkText(p.getProperty(objectName));
}
//find by partial link
else if(objectType.equalsIgnoreCase("PARTIALLINK")){
return By.partialLinkText(p.getProperty(objectName));
}else
{
throw new Exception("Wrong object type");
}
}
}
ExecuteTest.java
package testCases;
import java.util.Properties;
import operation.ReadObject;
import operation.UIOperation;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;
import excelExportAndFileIO.ReadGuru99ExcelFile;
public class ExecuteTest {
@Test
public void testLogin() throws Exception {
// TODO Auto-generated method stub
WebDriver webdriver = new FirefoxDriver();
ReadGuru99ExcelFile file = new ReadGuru99ExcelFile();
ReadObject object = new ReadObject();
Properties allObjects = object.getObjectRepository();
UIOperation operation = new UIOperation(webdriver);
//Read keyword sheet
Sheet guru99Sheet = file.readExcel(System.getProperty("user.dir")+"\\","TestCase.xlsx" , "KeywordFramework");
//Find number of rows in excel file
int rowCount = guru99Sheet.getLastRowNum()-guru99Sheet.getFirstRowNum();
//Create a loop over all the rows of excel file to read it
for (int i = 1; i < rowCount+1; i++) {
//Loop over all the rows
Row row = guru99Sheet.getRow(i);
//Check if the first cell contain a value, if yes, That means it is the new testcase name
if(row.getCell(0).toString().length()==0){
//Print testcase detail on console
System.out.println(row.getCell(1).toString()+"----"+ row.getCell(2).toString()+"----"+
row.getCell(3).toString()+"----"+ row.getCell(4).toString());
//Call perform function to perform operation on UI
operation.perform(allObjects, row.getCell(1).toString(), row.getCell(2).toString(),
row.getCell(3).toString(), row.getCell(4).toString());
}
else{
//Print the new testcase name when it started
System.out.println("New Testcase->"+row.getCell(0).toString() +" Started");
}
}
}
}
执行后,输出将如下所示-
下载本教程中演示的Selenium项目文件
混合框架
混合框架 In Selenium是一个概念,在这个概念中,我们既利用了关键字驱动框架的优势,也利用了数据驱动框架的优势。它是一个易于使用的框架,允许手动测试人员只需查看关键字、测试数据和对象存储库就可以创建测试用例,而无需在框架中编码。
在这里,对于关键字,我们将使用Excel文件来维护测试用例,而对于测试数据,我们可以使用Data、ProviderTestNG框架。
在我们的混合框架中,我们不需要更改关键字驱动框架中的任何内容,这里我们只需要将ExecuteTest.java文件替换为Hybridge ExecuteTest.java文件。
此Hybridge ExecuteTest文件包含由数据提供程序概念驱动的关键字的所有代码。
混合框架的完整图示如下所示
HybridExecuteTest.java
package testCases;
import java.io.IOException;
import java.util.Properties;
import operation.ReadObject;
import operation.UIOperation;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import excelExportAndFileIO.ReadGuru99ExcelFile;
public class HybridExecuteTest {
WebDriver webdriver = null;
@Test(dataProvider="hybridData")
public void testLogin(String testcaseName,String keyword,String objectName,String objectType,String value) throws Exception {
// TODO Auto-generated method stub
if(testcaseName!=null&&testcaseName.length()!=0){
webdriver=new FirefoxDriver();
}
ReadObject object = new ReadObject();
Properties allObjects = object.getObjectRepository();
UIOperation operation = new UIOperation(webdriver);
//Call perform function to perform operation on UI
operation.perform(allObjects, keyword, objectName,
objectType, value);
}
@DataProvider(name="hybridData")
public Object[][] getDataFromDataprovider() throws IOException{
Object[][] object = null;
ReadGuru99ExcelFile file = new ReadGuru99ExcelFile();
//Read keyword sheet
Sheet guru99Sheet = file.readExcel(System.getProperty("user.dir")+"\\","TestCase.xlsx" , "KeywordFramework");
//Find number of rows in excel file
int rowCount = guru99Sheet.getLastRowNum()-guru99Sheet.getFirstRowNum();
object = new Object[rowCount][5];
for (int i = 0; i < rowCount; i++) {
//Loop over all the rows
Row row = guru99Sheet.getRow(i+1);
//Create a loop to print cell values in a row
for (int j = 0; j < row.getLastCellNum(); j++) {
//Print excel data in console
object[i][j] = row.getCell(j).toString();
}
}
System.out.println("");
return object;
}
}
总结:
最后感谢每一个认真阅读我文章的人,礼尚往来总是要有的,虽然不是什么很值钱的东西,如果你用得到的话可以直接拿走:
这些资料,对于【软件测试】的朋友来说应该是最全面最完整的备战仓库,这个仓库也陪伴上万个测试工程师们走过最艰难的路程,希望也能帮助到你!有需要的小伙伴可以点击下方小卡片领取