Selenium自动化框架:数据驱动、关键字驱动和混合

什么是Selenium框架?

这个 Selenium框架 是一种代码结构,它使代码维护变得简单而高效。如果没有框架,用户可以将“c”框架产生了一些有益的结果,比如增加了代码可重用性、更高的可移植性、降低了脚本维护成本、更好的代码可读性等。

Selenium WebDriver主要创建了三种类型的框架来自动化手动测试用例

  • 数据驱动测试框架
  • 关键字驱动测试框架
  • 混合测试框架

56.png

Selenium中的数据驱动框架

Selenium中的数据驱动框架 是一种将数据集与测试用例分开的方法。一旦数据集从测试用例中分离出来,就可以很容易它用于从外部文件获取测试用例和套件,如Excel、.csv、.xml或某些数据库表。

57.png

为了读取或写入Excel,Apache提供了一个非常著名的库POI。此库足够读写两个库 XLS 和 xlsx Excel的文件格式。

阅读 XLS 文件,一个 HSSF 实现由POI库提供。

阅读 xlsx, XSSF 实现 POI 库 将是我们的选择。让我们详细研究一下这些实现。

我们已经在我们的上一个教程

Selenium中的关键字驱动框架

Selenium中的关键字驱动框架 是一种用于通过分隔常用函数和指令集的关键字来加速自动化测试的方法。用户可以轻松地控制和指定他们想要测试的功能。

下面是完整框架的外观

58.png

如所见,它是一个5步框架。让我们逐步详细地研究一下

步骤1)

  • 驱动程序脚本Execute.java将调用ReadGuru99ExcelFile.java
  • ReadGuru99ExcelFile.java具有从Excel读取数据的POI脚本

步骤2)

  • ReadGuru99ExcelFile.java将从TestCase.xlsx读取数据
  • 这是床单的样子-
     

59.png

  • 框架将根据Excel文件中写入的关键字对UI进行操作。
  •  例如,我们需要单击“登录”按钮。相应地,我们的Excel将有一个关键字“点击”。现在AUT可以在一个页面上有数百个按钮,为了标识一个登录按钮,在Excel中,我们将输入对象名称为loginButton,输入对象类型作为名称(参见上图中突出显示的行)。对象类型可以是XPath、名称CSS或任何其他值
     

步骤3) ReadGuru99ExcelFile.java会将此数据传递给驱动程序脚本Execute.java

步骤4)

  • 对于所有的UI web元素,我们需要创建一个对象存储库,我们将在其中放置它们的元素定位符(如XPath、名称、CSS路径、类名等)。

60.png

  • java(我们的驱动程序脚本)将读取整个对象存储库并将其存储在一个变量中
  • 要读取此对象存储库,我们需要一个具有getObjectRepository方法的ReadObject类来读取它。

61.png

注: 可能会想,我们为什么需要创建对象存储库。对于对象存储库,只需在存储库中进行一次更改。
步骤5)

  • 驱动程序会将数据从Excel&Object Repository传递到UIOperation类
  • UIOperation类具有执行与关键字相对应的动作的功能,如CLICK、SETTEXT等…在EXCEL中提到
  • UIOperation类是一个Java语言类,该类具有对web元素执行操作的代码的实际实现。

62.png

整个项目将看起来像-

63.png

让我们来看一个例子:

测试场景

  • 我们正在执行2个测试用例

  • 测试用例1:

  • 转到http://www.itxiaonv.com/V4/

  • 输入用户ID

  • 输入密码

  • 单击重置

  • 测试用例2:

  • 转到http://www.itxiaonv.com/V4/

  • 输入用户ID

  • 输入密码

  • 单击登录

object.properties

 
  1. url= [http://www.itxiaonv.com/V4/](http://www.itxiaonv.com/V4/)

  2. username=uid

  3. password=password

  4. title=barone

  5. loginButton=btnLogin

  6. resetButton=btnReset

 ReadGuru99ExcelFile.java

 
  1. package excelExportAndFileIO;

  2. import java.io.File;

  3. import java.io.FileInputStream;

  4. import java.io.IOException;

  5. import org.apache.poi.hssf.usermodel.HSSFWorkbook;

  6. import org.apache.poi.ss.usermodel.Sheet;

  7. import org.apache.poi.ss.usermodel.Workbook;

  8. import org.apache.poi.xssf.usermodel.XSSFWorkbook;

  9. public class ReadGuru99ExcelFile {

  10. public Sheet readExcel(String filePath,String fileName,String sheetName) throws IOException{

  11. //Create a object of File class to open xlsx file

  12. File file = new File(filePath+"\\"+fileName);

  13. //Create an object of FileInputStream class to read excel file

  14. FileInputStream inputStream = new FileInputStream(file);

  15. Workbook guru99Workbook = null;

  16. //Find the file extension by spliting file name in substing and getting only extension name

  17. String fileExtensionName = fileName.substring(fileName.indexOf("."));

  18. //Check condition if the file is xlsx file

  19. if(fileExtensionName.equals(".xlsx")){

  20. //If it is xlsx file then create object of XSSFWorkbook class

  21. guru99Workbook = new XSSFWorkbook(inputStream);

  22. }

  23. //Check condition if the file is xls file

  24. else if(fileExtensionName.equals(".xls")){

  25. //If it is xls file then create object of XSSFWorkbook class

  26. guru99Workbook = new HSSFWorkbook(inputStream);

  27. }

  28. //Read sheet inside the workbook by its name

  29. Sheet guru99Sheet = guru99Workbook.getSheet(sheetName);

  30. return guru99Sheet;

  31. }

  32. }

ReadObject.java

 
  1. package operation;

  2. import java.io.File;

  3. import java.io.FileInputStream;

  4. import java.io.IOException;

  5. import java.io.InputStream;

  6. import java.util.Properties;

  7. public class ReadObject {

  8. Properties p = new Properties();

  9. public Properties getObjectRepository() throws IOException{

  10. //Read object repository file

  11. InputStream stream = new FileInputStream(new File(System.getProperty("user.dir")+"\\src\\objects\\object.properties"));

  12. //load all objects

  13. p.load(stream);

  14. return p;

  15. }

  16. }

UIOperation.java

 
  1. package operation;

  2. import java.util.Properties;

  3. import org.openqa.selenium.By;

  4. import org.openqa.selenium.WebDriver;

  5. public class UIOperation {

  6. WebDriver driver;

  7. public UIOperation(WebDriver driver){

  8. this.driver = driver;

  9. }

  10. public void perform(Properties p,String operation,String objectName,String objectType,String value) throws Exception{

  11. System.out.println("");

  12. switch (operation.toUpperCase()) {

  13. case "CLICK":

  14. //Perform click

  15. driver.findElement(this.getObject(p,objectName,objectType)).click();

  16. break;

  17. case "SETTEXT":

  18. //Set text on control

  19. driver.findElement(this.getObject(p,objectName,objectType)).sendKeys(value);

  20. break;

  21. case "GOTOURL":

  22. //Get url of application

  23. driver.get(p.getProperty(value));

  24. break;

  25. case "GETTEXT":

  26. //Get text of an element

  27. driver.findElement(this.getObject(p,objectName,objectType)).getText();

  28. break;

  29. default:

  30. break;

  31. }

  32. }

  33. /

  34. * Find element BY using object type and value

  35. * @param p

  36. * @param objectName

  37. * @param objectType

  38. * @return

  39. * @throws Exception

  40. */

  41. private By getObject(Properties p,String objectName,String objectType) throws Exception{

  42. //Find by xpath

  43. if(objectType.equalsIgnoreCase("XPATH")){

  44. return By.xpath(p.getProperty(objectName));

  45. }

  46. //find by class

  47. else if(objectType.equalsIgnoreCase("CLASSNAME")){

  48. return By.className(p.getProperty(objectName));

  49. }

  50. //find by name

  51. else if(objectType.equalsIgnoreCase("NAME")){

  52. return By.name(p.getProperty(objectName));

  53. }

  54. //Find by css

  55. else if(objectType.equalsIgnoreCase("CSS")){

  56. return By.cssSelector(p.getProperty(objectName));

  57. }

  58. //find by link

  59. else if(objectType.equalsIgnoreCase("LINK")){

  60. return By.linkText(p.getProperty(objectName));

  61. }

  62. //find by partial link

  63. else if(objectType.equalsIgnoreCase("PARTIALLINK")){

  64. return By.partialLinkText(p.getProperty(objectName));

  65. }else

  66. {

  67. throw new Exception("Wrong object type");

  68. }

  69. }

  70. }

ExecuteTest.java 

 
  1. package testCases;

  2. import java.util.Properties;

  3. import operation.ReadObject;

  4. import operation.UIOperation;

  5. import org.apache.poi.ss.usermodel.Row;

  6. import org.apache.poi.ss.usermodel.Sheet;

  7. import org.openqa.selenium.WebDriver;

  8. import org.openqa.selenium.firefox.FirefoxDriver;

  9. import org.testng.annotations.Test;

  10. import excelExportAndFileIO.ReadGuru99ExcelFile;

  11. public class ExecuteTest {

  12. @Test

  13. public void testLogin() throws Exception {

  14. // TODO Auto-generated method stub

  15. WebDriver webdriver = new FirefoxDriver();

  16. ReadGuru99ExcelFile file = new ReadGuru99ExcelFile();

  17. ReadObject object = new ReadObject();

  18. Properties allObjects = object.getObjectRepository();

  19. UIOperation operation = new UIOperation(webdriver);

  20. //Read keyword sheet

  21. Sheet guru99Sheet = file.readExcel(System.getProperty("user.dir")+"\\","TestCase.xlsx" , "KeywordFramework");

  22. //Find number of rows in excel file

  23. int rowCount = guru99Sheet.getLastRowNum()-guru99Sheet.getFirstRowNum();

  24. //Create a loop over all the rows of excel file to read it

  25. for (int i = 1; i < rowCount+1; i++) {

  26. //Loop over all the rows

  27. Row row = guru99Sheet.getRow(i);

  28. //Check if the first cell contain a value, if yes, That means it is the new testcase name

  29. if(row.getCell(0).toString().length()==0){

  30. //Print testcase detail on console

  31. System.out.println(row.getCell(1).toString()+"----"+ row.getCell(2).toString()+"----"+

  32. row.getCell(3).toString()+"----"+ row.getCell(4).toString());

  33. //Call perform function to perform operation on UI

  34. operation.perform(allObjects, row.getCell(1).toString(), row.getCell(2).toString(),

  35. row.getCell(3).toString(), row.getCell(4).toString());

  36. }

  37. else{

  38. //Print the new testcase name when it started

  39. System.out.println("New Testcase->"+row.getCell(0).toString() +" Started");

  40. }

  41. }

  42. }

  43. }

执行后,输出将如下所示-

64.png

下载本教程中演示的Selenium项目文件

混合框架

混合框架 In Selenium是一个概念,在这个概念中,我们既利用了关键字驱动框架的优势,也利用了数据驱动框架的优势。它是一个易于使用的框架,允许手动测试人员只需查看关键字、测试数据和对象存储库就可以创建测试用例,而无需在框架中编码。

在这里,对于关键字,我们将使用Excel文件来维护测试用例,而对于测试数据,我们可以使用Data、ProviderTestNG框架。

65.png

在我们的混合框架中,我们不需要更改关键字驱动框架中的任何内容,这里我们只需要将ExecuteTest.java文件替换为Hybridge ExecuteTest.java文件。

66.png

此Hybridge ExecuteTest文件包含由数据提供程序概念驱动的关键字的所有代码。

混合框架的完整图示如下所示

67.png

HybridExecuteTest.java

 
  1. package testCases;

  2. import java.io.IOException;

  3. import java.util.Properties;

  4. import operation.ReadObject;

  5. import operation.UIOperation;

  6. import org.apache.poi.ss.usermodel.Row;

  7. import org.apache.poi.ss.usermodel.Sheet;

  8. import org.openqa.selenium.WebDriver;

  9. import org.openqa.selenium.firefox.FirefoxDriver;

  10. import org.testng.annotations.DataProvider;

  11. import org.testng.annotations.Test;

  12. import excelExportAndFileIO.ReadGuru99ExcelFile;

  13. public class HybridExecuteTest {

  14. WebDriver webdriver = null;

  15. @Test(dataProvider="hybridData")

  16. public void testLogin(String testcaseName,String keyword,String objectName,String objectType,String value) throws Exception {

  17. // TODO Auto-generated method stub

  18. if(testcaseName!=null&&testcaseName.length()!=0){

  19. webdriver=new FirefoxDriver();

  20. }

  21. ReadObject object = new ReadObject();

  22. Properties allObjects = object.getObjectRepository();

  23. UIOperation operation = new UIOperation(webdriver);

  24. //Call perform function to perform operation on UI

  25. operation.perform(allObjects, keyword, objectName,

  26. objectType, value);

  27. }

  28. @DataProvider(name="hybridData")

  29. public Object[][] getDataFromDataprovider() throws IOException{

  30. Object[][] object = null;

  31. ReadGuru99ExcelFile file = new ReadGuru99ExcelFile();

  32. //Read keyword sheet

  33. Sheet guru99Sheet = file.readExcel(System.getProperty("user.dir")+"\\","TestCase.xlsx" , "KeywordFramework");

  34. //Find number of rows in excel file

  35. int rowCount = guru99Sheet.getLastRowNum()-guru99Sheet.getFirstRowNum();

  36. object = new Object[rowCount][5];

  37. for (int i = 0; i < rowCount; i++) {

  38. //Loop over all the rows

  39. Row row = guru99Sheet.getRow(i+1);

  40. //Create a loop to print cell values in a row

  41. for (int j = 0; j < row.getLastCellNum(); j++) {

  42. //Print excel data in console

  43. object[i][j] = row.getCell(j).toString();

  44. }

  45. }

  46. System.out.println("");

  47. return object;

  48. }

  49. }

总结:

  • 我们可以使用Selenium WebDriver创建三种类型的测试框架。
  • 它们是数据驱动、关键字驱动和混合测试框架。
  • 我们可以使用TestNG的数据提供程序来实现数据驱动框架。
  • 在关键字驱动框架中,关键字被写入一些外部文件,如EXCEL文件,Java代码将调用该文件并执行测试用例。
  • 该混合框架是关键字驱动和数据驱动的混合框架。

软件测试学习资料获取关注公众号:程序员雷叔    

  • 28
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值