Unique property per SoapUI request using groovy

对着背影说爱祢 提交于 2019-12-25 16:46:21

问题


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.

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)}

Script:

// 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.

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>

The error I was getting:

<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).

Thanks :)


回答1:


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.

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.

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
}

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))}


来源:https://stackoverflow.com/questions/27424797/unique-property-per-soapui-request-using-groovy

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!