使用groovy的每个SoapUI请求的唯一属性 [英] Unique property per SoapUI request using groovy

查看:306
本文介绍了使用groovy的每个SoapUI请求的唯一属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用SoapUI的开源版本来进行一些SOAP Web服务负载测试.

I am using the open source version of SoapUI to do some SOAP Web service Load Testing.

我希望每个请求都尽可能与先前的请求有所不同,我不希望负载测试具有相同的属性.

I would like each request to differ from the previous requests as much as possible, I do not want a load test with the same properties.

我有一个具有多个属性的属性文件,每个属性值都将调用一个groovy脚本从文件中读取一个随机值,并将其分配给测试用例属性,即在value字段中:

I have a properties file which has several properties, each property value is to call a groovy script to read a random value from a file and assign it to the test case property i.e. in the value field:

${=(DynamicPropertyScript)}

脚本:

// Load property from file
def file = new File('path')

// Create empty list for file contents
def list = [];

// Populate list with file contents
addURLstoList = {list.add(it)};
file.eachLine(addURLstoList);

// Pick a random item from list
def randomIndex = (int)Math.random()*list.size;
def randomValue = list.get(randomIndex);

// Assign random value to property
def tc = testRunner.testCase;
tc.setPropertyValue('property', randomValue);
log.info(randomValue)

如果我在测试用例开始时调用此脚本,它将很好地工作,我关心的领域是启动时生成的属性对于每个后续请求都是相同的,这就是我要避免的

This script works fine if I call it at the start of a test case, my area of concern is that the property that is generated on start-up will be the same for each subsequent request, this is what I want to avoid.

我尝试了几件事,但最终都失败了(由于缺乏对Groovy和SoapUI的经验).

I have tried several things but ultimately failed (due to my lack of experience with Groovy and SoapUI).

我在请求中尝试过的一些事情

Some things I have tried in the request

<inc:ID>${Properties#property}</inc:ID>

<inc:ID>${=(DynamicPropertyScript)}</inc:ID>

我遇到的错误:

<inc:ID>No such property: DynamicPropertyScript for class: Script4</inc:>

任何帮助都将不胜感激,此外,如果还有另一种方法也能帮上忙(我理解每次发送大量请求和每次从磁盘读取信息都是不理想的).

Any help would be much appreciated, additionally if there is an alternative way that would also help (I understand sending lots of requests & reading from disk every time is not ideal).

谢谢:)

推荐答案

我希望将数据列表一次加载到内存中,以避免重复IO,然后在需要它的测试步骤中从列表中选择随机项使用Groovy表达式.您可以使用上下文变量将数据保存在内存中.

I'd look to load the data list into memory once to avoid repeated IO, and then pick the random item from the list within the test step that requires it using a Groovy expression. You can use a context variable to hold the data in memory.

以下Groovy脚本将读取位于项目根目录中的名为data1.txt的数据文件的内容,并将其加载到上下文变量中. context.data变量将数据项保存为列表,而context.dataCount则保存项数.

The following Groovy script will read the contents of a data file named data1.txt, located within the Project root directory, and load it into a context variable. The context.data variable holds the data items as a list, and the context.dataCount holds the number of items.

您可能希望将其添加为安装脚本(针对TestSuite或TestCase),而不是在Groovy测试步骤中添加,因此仅运行一次.上下文变量仍在相应的Suite/Case运行程序的范围内,因此可以在随后的任何步骤中进行引用.

You probably want to add this as a Setup Script (either against the TestSuite or TestCase), rather than within a Groovy test step, so that it only runs the once. Context variables remain in scope of the corresponding Suite/Case runner, so can be referenced in any of the subsequent steps.

def projectDir = context.expand('${projectDir}') + File.separator
def dataFile = "data1.txt"

try 
{
    File file = new File(projectDir + dataFile)
    context.data = file.readLines()
    context.dataCount = context.data.size
} 
catch (Exception e) 
{
    testRunner.fail("Failed to load " + dataFile + " from project directory.")
    return
}

然后,要从context.data变量中获取随机数据项,请根据需要输入以下表达式作为参数值或嵌入请求的正文中.

Then, to get a random data item from the context.data variable, enter the following expression as a parameter value or embedded within the body of a request, as required.

${=context.data.get((int)(Math.random()*context.dataCount))}

这篇关于使用groovy的每个SoapUI请求的唯一属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆