excel文件的groovy脚本在SoapUI中进行数据驱动测试

SoapUI Pro具有从外部文件读取数据的功能,例如:excel,csv等。但SoapUI不提供从excel文件读取数据的功能。因此,为了从SoapUI中的excel文件中读取数据,我们需要在groovy脚本中编写一些代码。
我这篇文章我将告诉你,如何从excel文件中读取数据。我正在使用poi jar文件从groovy中的excel文件中读取数据,下载以下jar文件并放入SoapUI lib文件夹。

 

  • POI-3.8-beta5-20111217.jar
  • POI-例子-3.8-beta5-20111217.jar
  • POI-excelant-3.8-beta5-20111217.jar
  • POI-OOXML-3.8-beta5-20111217.jar
  • POI-OOXML-架构 - 3.8 beta5-20111217.jar
  • POI暂存器-3.8-beta5-20111217.jar 
  • dom4j的-1.6.1.jar

在这篇文章中,我为“ConversionRate”API创建了一个SoapUI项目,并创建了一个名为“ConversionRate”的测试用例和测试步骤。所以这里我需要为数据集运行此测试,其中数据在外部excel文件中。

我创建了一个“ReadXLSFile”groovy步骤,并在下面编写代码,从“Book1.xlsx”文件中读取“ConversionRate”API方法的数据。以下是excel文件数据:

 

I have created a “ReadXLSFile” groovy step and write below code to read data from “Book1.xlsx” file for the “ConversionRate” API method. Below is excel file data:

To
From
USD
ALL
AFA
DZD
AWG
BSD
BSD
BDT
GroovyScript Code:

 



import org.apache.poi.ss.usermodel.*;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.xssf.usermodel.*;
import org.apache.poi.ss.util.*;
import org.apache.poi.ss.usermodel.*;
import java.io.*;

class ExcelReader {

def readData() {
def path = "E:\\Automation-WorkArea\\APITest\\Book1.xlsx";
InputStream inputStream = new FileInputStream(path);
Workbook workbook = WorkbookFactory.create(inputStream);
Sheet sheet = workbook.getSheetAt(0);

Iterator rowIterator = sheet.rowIterator();
rowIterator.next()
Row row;
def rowsData = []
while(rowIterator.hasNext()) {
row = rowIterator.next()
def rowIndex = row.getRowNum()
def colIndex;
def rowData = []
for (Cell cell : row) {
colIndex = cell.getColumnIndex()
rowData[colIndex] = cell.getRichStringCellValue().getString();
}
rowsData << rowData
}
rowsData
}
}

def groovyUtils = new com.eviware.soapui.support.GroovyUtils(context)
def myTestCase = context.testCase

ExcelReader excelReader = new ExcelReader();
List rows = excelReader.readData();
def d = []
Iterator i = rows.iterator();
while( i.hasNext()){
d = i.next();
myTestCase.setPropertyValue("From", d[0])
myTestCase.setPropertyValue("To", d[1])
testRunner.runTestStepByName( "ConversionRate")
}


 

描述:

  • 包含函数“readData”的ExcelReader类,用于从“Book1.xlsx”文件中读取数据。
  • myTestCase.setPropertyValue(“From”,d [0])和myTestCase.setPropertyValue(“To”,d [1])用于设置测试用例“from”和“To”属性值。
  • testRunner.runTestStepByName(“ConversionRate”)此步骤用于运行测试步骤“ConversionRate”
所以这样当我运行测试“ReadXLSFile”从xls文件中读取数据时,将为每个输出数据执行测试“ConversionRate”

转载于:https://www.cnblogs.com/a00ium/p/10548623.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SoapUI使用Groovy脚本可以帮助你扩展和自定义测试逻辑。下面是一些在SoapUI使用Groovy脚本的常见示例: 1. 在请求使用Groovy脚本设置参数值: ```groovy // 获取请求对象 def request = context.testCase.testSteps["Request"].testRequest // 设置参数值 request.setRequestContent("Hello, SoapUI!") ``` 2. 在测试步骤使用Groovy脚本进行断言: ```groovy // 获取响应对象 def response = context.testCase.testSteps["Request"].testRequest.response // 断言响应码为200 assert response.getStatusCode() == 200 // 断言响应内容包含指定字符串 assert response.getResponseText().contains("Success") ``` 3. 在Groovy脚本使用循环和条件语句: ```groovy def count = 0 // 循环执行请求 while (count < 5) { def response = context.testCase.testSteps["Request"].testRequest.response log.info("Response: " + response.getResponseText()) count++ } // 根据条件执行不同的操作 if (response.getStatusCode() == 200) { log.info("Request succeeded") } else { log.error("Request failed") } ``` 4. 在Groovy脚本调用其他自定义方法或函数: ```groovy // 定义自定义函数 def customMethod(String message) { log.info("Custom Method: " + message) } // 调用自定义方法 customMethod("Hello, SoapUI!") ``` 以上示例只是演示了一些Groovy脚本的基本用法,你可以根据具体需求进行扩展和修改。在SoapUI,你可以将Groovy脚本添加到测试步骤、测试套件和全局范围,并通过`context`对象访问相关上下文信息。希望对你有帮助!

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值