Monthly Archives: February 2014

SoapUI – Parameterizing Requests in the free version

Hello,

This post will show how to send values from an external file to specific WSDL tags as arguments. In this way if you have to perform combinatorial testing with let’s say, 50 different requests, instead of creating individual SOAP requests, you’ll only need one and a (rather big) testdata file.

For this example we’ll be reading the testdata from an Excel file so you should first make sure to place the Apache POI jar file in the “SoapUI\bin\ext” folder. This is the content of the Excel file:

City Name Zip
New York 10001
not found 33333
Minneapolis 55555
BeverlyHills 90210

 

Now open SoapUI and create a new project using a WSDL file such as the famous weather one:  http://wsf.cdyne.com/WeatherWS/Weather.asmx?WSDL

We’re now going to Generate a TestSuite by right-clicking the WeatherSoap interface and selecting the GetCityForecastByZip operation. In the TestCase and open TestSteps and add a new step of type “Properties”, name it something like “property-loop” open it and insert a new property named “Zip”. Now open the Soap Request (GetCityForecastByZip) and right click on the question mark “?” and select GetData > property-loop> Zip. It should look something like this:

SoapUI-Property

We have the request mapped to the property Zip value but now we need to also pass the data from the excel to the property. We can do this by using a Groovy Script so let’s get groovy 😀

Add a new test step select Groovy Script and insert the following code for reading an Excel file:


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
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import java.io.*

File excel = new File("D:/data.xls"); // path to the Excel file

FileInputStream fis = new FileInputStream(excel);
HSSFWorkbook wb = new HSSFWorkbook(fis);
HSSFSheet ws = wb.getSheetAt(0); // get the first Sheet of the Excel file

int rowNum = ws.getLastRowNum() + 1;
int colNum = ws.getRow(0).getLastCellNum();
propTestStep = context.testCase.getTestStepByName("property-loop") // get the Property step (ours is named "property-loop")

    for (int i = 1; i< rowNum; i++) {
        HSSFRow row = ws.getRow(i);
        for (int j=1; j < colNum; j++) {
            HSSFCell cell = row.getCell(j);
            String value = cellToString(cell);

            log.info ("The value is " + value);
            propTestStep.setPropertyValue("Zip", cellToString(row.getCell(1))) //set the value of "Zip" property equal to Excel's column B ( getCell(1) )

        }
        testRunner.runTestStepByName("GetCityForecastByZIP"); //we're going to run the Soap Request after each iteration of the Excel's rows.
    }

    //a helper method to convert the Cell's value to String
    public static String cellToString(HSSFCell cell) {
        int type;
        Object result;
        type= cell.getCellType();

        switch (type) {
            case 0 : //numeric value in Excel
                result = String.valueOf((int)cell.getNumericCellValue());
                break;
            case 1 : //String value in Excel
                result = cell.getStringCellValue();
                break;
            default:
                result = cell.getCellType();
                throw new RuntimeException("There is no reader for this type of Excel data");
        }
        return result.toString();
    }

So what the above script does is for each row in the Excel file, we’re passing the value from the Column B to the Property field named “Zip” and then run our SoapRequest.

We can also disable the property and the soap request so that our TestCase only executes the Groovy Script which calls the request using testRunner.runTestStepByName(“GetCityForecastByZIP”).

Hope you’ll find this helpful,
-M.