A Groovy kind of trick for SoapUI free in eight steps

Categories: Software Product Development | Software Testing |

If you have had the chance of working with SoapUI free to do web services testing, this tutorial could be really helpful.

 

Imagine you are testing a web service, and you want to run the same test with a different data set. In the pro version, you can simply use a DataSource step inside your test case to take data from an external source. Unfortunately, in the free version this kind of step is not available, and the simplest solution would be to create as many test cases as data sets you have… this could work out well if you have ten data sets, but imagine if you have a hundred or more!!

DON’T WORRY! There is a way to work around it, and it is called groovy scripting.

With two simple groovy scripts, you can build your own data driver and take your data from an Excel file.

Here is an example:

Case explanation

Let’s suppose we have to test a web service with a method to login. The method will receive a user and password in a request, and will return a token in response. What we need to do is to test it with a set of users and passwords. With every user – password entered, we need to assert that the token has been returned in the response.

This is just a simple example. With some modifications, we can parameterize any case.

Step 1:

First of all, we need to have an Excel file with all our data in a local location. For instance, c:\logindata.xls

In that file, we will have all the user – password combinations we want to test in each row:

cuadro

Step 2

We need to have a library to help us handle the excel file. The library name is JXL.jar. and it can be downloaded following this link:

http://sourceforge.net/projects/jexcelapi/files/jexcelapi/2.6.12/jexcelapi_2_6_12.zip/download

Unzip the file, and copy JXL.jar to your SOAPUI installation folder\lib. That would typically be C:\Program Files\SmartBear\SoapUI-xxx\lib

Step 3

Create a test case in SoapUI from your Login request.

Step 4

Add 2 steps Groovy Script, and a step Properties inside the Test steps section, then name and order them following the next scheme:

A Groovy kind of trick for SoapUI free in eight steps

 

 

 

 

 

Step 5

On the Property – Looper step, create the properties of the following picture:

A Groovy kind of trick for SoapUI free in eight steps

The variables you need for your test are user and password. If you are performing a different test and you need other variables, you have to create them there.

Other variables used to handle the data driven process and the loop are Total, Count (initialized to 0), Next and StopLoop.

Step 6

Inside the data driver groovy script step, enter the following code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// IMPORT THE LIBRARIES WE NEED
 
import com.eviware.soapui.support.XmlHolder
 
import jxl.*
 
import jxl.write.*
 
// DECLARE THE VARIABLES
 
def myTestCase = context.testCase //myTestCase contains the test case
 
def counter,next,previous,size //Variables used to handle the loop and to move inside the file
 
Workbook workbook1 = Workbook.getWorkbook(new File("c:\\dataFile.xls")) //file containing the data
 
Sheet sheet1 = workbook1.getSheet(0) //save the first sheet in sheet1
 
size= sheet1.getRows().toInteger() //get the number of rows, each row is a data set
 
propTestStep = myTestCase.getTestStepByName("Property - Looper") // get the Property TestStep object
 
propTestStep.setPropertyValue("Total", size.toString())
 
counter = propTestStep.getPropertyValue("Count").toString() //counter variable contains iteration number
 
counter = counter.toInteger() //
 
next = (counter > size-2? 0: counter+1) //set the next value
 
// OBTAINING THE DATA YOU NEED
 
Cell u = sheet1.getCell(0,counter) // getCell(column,row) //obtains user
 
Cell p = sheet1.getCell(1,counter) // obtains password
 
workbook1.close() //close the file
 
////////////////////////////////////
 
usr = u.getContents()
 
pass = p.getContents()
 
propTestStep.setPropertyValue("user", usr) //the value is saved in the property
 
propTestStep.setPropertyValue("pass", pass) //the value is saved in the property
 
propTestStep.setPropertyValue("Count", next.toString()) //increase Count value
 
next++ //increase next value
 
propTestStep.setPropertyValue("Next", next.toString()) //set Next value on the properties step
 
//Decide if the test has to be run again or not
 
if (counter == size-1)
 
{
 
propTestStep.setPropertyValue("StopLoop", "T")
 
log.info "Setting the stoploop property now..."
 
}
 
else if (counter==0)
 
{
 
def runner = new com.eviware.soapui.impl.wsdl.testcase.WsdlTestCaseRunner(testRunner.testCase, null)
 
propTestStep.setPropertyValue("StopLoop", "F")
 
}
 
else
 
{
 
propTestStep.setPropertyValue("StopLoop", "F")
 
}

Step 7

On the Login Step, replace on the request header or body the value you sent for the value token from the Property – Looper step. In the example, that would be:

1
2
${Property - Looper#user}
${Property - Looper#pass}

Step 8

On the Loop step enter the following code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
def myTestCase = context.testCase
 
def runner
 
propTestStep = myTestCase.getTestStepByName("Property - Looper") // get the Property TestStep
 
endLoop = propTestStep.getPropertyValue("StopLoop").toString()
 
if (endLoop.toString() == "T" || endLoop.toString()=="True" || endLoop.toString()=="true")
 
{
 
log.info ("Exit Groovy Data Source Looper")
 
assert true
 
}
 
else
 
{
 
testRunner.gotoStepByName("data driver") //setStartStep
 
}

As a conclusion…

In my experience, the most important difference between the SoapUI Free and Pro version is the data driven ability. Now, with just those simple scripts, we can work around this limitation, adding to our work an extra value covering more scenarios but using the same test case. This is very useful when we don’t have the chance of acquiring a Pro license.

If you have any question or suggestions, do not hesitate on contacting me.

Leave a comment

 

    Hi Lucas,

    I have tried your example(A GROOVY KIND OF TRICK FOR SOAPUI FREE IN EIGHT STEPS) but does not work, I receiving this error message (org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed: Script22.groovy: 29: Expression list of the form (a; b; c) is not supported in this context. at line: 29 column: 8. File: Script22.groovy @ line 29, column 8. next = (counter > size-2? 0: counter+1) //set the next value ^ org.codehaus.groovy.antlr.ASTParserException: Expression list of the form (a; b; c) is not supported in this context. at line: 29 ……..)Can you help me please? I am a novice in this area ,I just want to learn some way how I can use a data source in free version of SoapUI and I am not familiar with a programming.Thank you for your answer forward. Bran

    Hi, did you get any help on that error you had? I’m just getting started also… and I got the exact samen error as you did…

    @Bran: In step 6, there is some glitch in the code.
    Change > to >
    next = (counter > size-2? 0: counter+1) //set the next value
    to
    next = (counter > size-2? 0: counter+1) //set the next value

    I am getting error
    “rg.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed: Script15.groovy: 10: unable to resolve class Workbook @ line 10, column 10. Workbook workbook1 = Workbook.getWorkbook(new File(“C:/Documents and Settings/abarawk/Desktop/SOAP UI Frame Work/Input_Excel.xls”))//file”

    I have included JXL.jar as mentioned in step 2. Can you please help solving this.

    Hi Lucas,
    I tried to execute above codes to data driven testing in soapui using groovy script. I got java.lang.nullPointerException:Can not invoke method setPropertyValue() on null object, error line:23.
    Can you pls resolve the issue. I followed all steps as explained above.

    I am not getting any errors but the looping functionality is not working. I have to execute the data driver each time and then execute request for each test data. Is there any way to automatically all the test data at once. Please let me know as soon as possible.

    @ Vinay i had the same problem but i solved with the following.

    go to the loop groocy script and change the ELSE from

    else
    {
    testRunner.gotoStepByName(“data driver”) //setStartStep
    }

    TO

    else
    {
    testRunner.gotoStepByName(“NAME OF YOUR SOAP ENVELOPE”) //setStartStep
    }

    that solved the problem for me.

    Thanks to the author and all the other users for the help and great comments 🙂

    @BA when you say “NAME OF YOUR SOAP ENVELOPE” what in the request XML are you referring to? namespace name?
    I’m have exact same problem cant figure out to get the loop to run.

    Thanks
    Stefan

    Very nice example Lucas for the beginners like me learning data driven testing using Open source SoapUI version.
    However, one thing I observed. While running the test case repeatedly it was working fine for the total number of data rows during first run but second time onward failing. means Loop was getting executed additionally one more time than number of rows.
    So, after debugging found that, count in property needs to be set to zero again when we exit (i.e.) assert true in Step8.

    Thanks,
    Nilesh D.

    Thanks Lucas, it helped a lot for us like beginners. But I have a doubt (just started soapUI), what does given statement do in else if block : \”def runner = new com.eviware.soapui.impl.wsdl.testcase.WsdlTestCaseRunner(testRunner.testCase, null)\”
    Also, I got no error but loop is not working, every time i need to go to data driver step and run it again manually.

    Please Help.

    Thanks,
    Himanshu