开发者

Problem with Groovy and variable types

I'm trying to get a piece of Groovy code (working with SOAPUI), but am having a problem.

What this code is suppose to do is read in a property file, then set some properties in SOAPUI. I put this in a Groovy script test step in SOAPUI test case, and each time it is run, I want it to read from a different filename, so I have two additional properties in the test case, cur_request_number and max_request_number.

This code is suppose to increment the cur_request_number each time, and check if it reaches the max_request_number, and if so, set the cur_request_number back to 1. It kind of works, but only as long as max_request_number is 9 or below. If it's 10 or above, when it reaches 9 or 10, the cur_request_number++ seems to be incrementing it to a string value, e.g., semi-colon.

In other words, it's suppose to read (on each cycle):

testprop1.txt
testprop2.txt
.
.

The code I'm posting below doesn't actually read the textpropX.txt files yet, but just prints the filenames that it WOULD read, since I'm still debugging.

I'm really new to Groovy, so I'm kind of stuck. I've tried adding toInteger(), but then I get different problems.

def cur_request_number = testRunner.testCase.getPropertyValue("cur_request_number");
def max_request_number = testRunner.testCase.getPropertyValue("max_request_number");
log.info "INITIAL cur_request_number=[" + cur_request_number + "]"
log.info "INITIAL max_request_number=[" + max_request_number + "]"

cur_request_number++;

log.info "BUMPED cur_request_number=[" + cur_request_number + "]"

if (cur_request_number == max_request_number) {
    log.info "In the IF about to reset cur_request_number to 1";
    cur_request_number = "1";
}

// set the cur_request_number property (either the incremented one, or "1")
testRunner.testCase.setPropertyValue("cur_request_number", cur_request_number);

cur_request_filename = "E:/SOAPUI-PROPS/testprops" + cur_request_number.toString() +".txt";
log.info "READING FROM Request file [" + cur_request_filename + "]"

props = new java.util.Properties ()
file = new File("E:/SOAPUI-PROPS/testprops.txt")
if(!file.exists()) {
    log.info "No file found at E:/SOAPUI-PROPS/testprops.txt"
}
else {
    log.info "max_request_number=[" + max_request_number + "]"

    log.info "In the ELSE, cur_request_number=[" + cur_request_number + "]"
    //testRunner.testCase.setPropertyValue("cur_request_number", cur_request_number);
    log.info "Found E:/SOAPUI-PROPS/testprops.txt"
    fis = new FileInputStream (file)
    props.load (fis)
    requestid = props.getProperty ( "requestid" )
    log.info "requestid = [" + requestid + "]"
    dnstring = props.getProperty ( "dnstring" )
    log.info "dnstring= [" + dnstring + "]"
    testRunner.testCase.setPro开发者_运维问答pertyValue("requestid", requestid);
    context.requestid = requestid;
    log.info "Finished setting 'requestid' property"
    testRunner.testCase.setPropertyValue("dnstring", dnstring);
    context.dnstring = dnstring;
    log.info "Finished setting 'dnstring' property"
} 

If anyone could tell me what I'm doing wrong, I'd really appreciate it.

Thanks, Jim


Try changing

def cur_request_number = testRunner.testCase.getPropertyValue("cur_request_number");
def max_request_number = testRunner.testCase.getPropertyValue("max_request_number");

to

int cur_request_number = Integer.parseInt( testRunner.testCase.getPropertyValue("cur_request_number") )
int max_request_number = Integer.parseInt( testRunner.testCase.getPropertyValue("max_request_number") )

I suspect it's reading the number out of a properties file somewhere as a string

As you can see, getPropertyValue returns a String

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜