How To Do Data Driven Testing in SoapUi With Groovy
How To Do Data Driven Testing in SoapUi With Groovy
Download and install the open source version of SoapUI from their website.
Download jxl.jar and place it in lib folder of SoapUI.
For example: ‘C:\Program Files\SmartBear\SOAP UI\lib’
‘jxl.jar’ is the jar file which supports all Excel operations, this jar supports
only ‘.xls’ format.
Right click on ‘Project’ and click on ‘New Test Suite’ to create a test suite. Now
right click on ‘Test Suite’ and then ‘New Test Case’ to create a test case under
test suite. Test case consists of test steps where the actual automation takes
place.
Note: We can create any number of test cases under a test suite.
Right click on ‘Test Case’ and select Add Step > Properties, these properties are
there to hold the values of defined variables.
Right click on ‘Test Case’ and select Add Step > SOAP Request (which consists of
actual request to parameterize). The one marked in yellow is getting
parameterized.
Right click on ‘Test Case’ and select Add Step > Groovy Script. Now, place the
below script and click on ‘Save’ option.
1 import jxl.*
import jxl.write.*
2
import java.io.File;
3 import com.eviware.soapui.support.*;
4 import java.util.*;
5 import java.lang.*;
6 import jxl.read.biff.BiffException;
Workbook wk;
7 WritableWorkbook workbookCopy;
8 def fr;
9 try
10 {
11 fr = new File("C:\\SOAP Projects CSC\\My Own Projects\\Check Domain
Groovy\\CheckDomainTestData1.xls")
12 wk = Workbook.getWorkbook(fr);
13 def s1 = wk.getSheet(0);
14 workbookCopy = Workbook.createWorkbook(fr, wk);
15 WritableSheet sheetToEdit = workbookCopy.getSheet(0);
def r = s1.getRows();
16
// -------------------------------------------------------------
17 // Defining object “fr” for Excel workbook
18 // Assigning Workbook Object to “wk”
19 // Getting Sheet1 into variable “s1”
20 // Creating and Assigning workbook to “WorkbookCopy”
// Defining writable sheet and getting Sheet1 to “sheetToEdit”
21 // Getting row count to “r”
22 // -------------------------------------------------------------
23 for(def i=1;i<r;i++)
24 {
25 def c1 = s1.getCell(0, i)
def c2 = s1.getCell(1, i)
26 // -------------------------------------------------------------
27 // Iterating through Excel rows
28 // Assigning cell “A2” to “c1”
29 // Assigning cell “A3” to “c2”
// -------------------------------------------------------------
30 testRunner.testCase.testSteps["GroovyProperties"].setPropertyValue("Do
31 getContents())
32 testRunner.testCase.testSteps["GroovyProperties"].setPropertyValue("TL
33 ents())
34 // -------------------------------------------------------------
// Using testRunner object we are assigning content in cell “A2” to property “DomainN
35 to Property “TLD”
36 // -------------------------------------------------------------
37 testRunner.runTestStepByName("CheckDomainCommand")
38 def EndPointURL =
39 testRunner.testCase.testSteps["GroovyProperties"].getPropertyValue("En
// -------------------------------------------------------------
40 // Using “testRunner.run” we are executing the actual command
41 // “EndPointURL” defines in which environment the test need to execute
42 // -------------------------------------------------------------
43 def groovyUtils = new com.eviware.soapui.support.GroovyUtils(context)
// -------------------------------------------------------------
44 // Defining “groovyUtils” to access the utility methods of GroovyUtils class
45 // -------------------------------------------------------------
46 def holder = groovyUtils.getXmlHolder("CheckDomainCommand#Response")
47 // -------------------------------------------------------------
48 // Defining “holder” to capture the XML response
// -------------------------------------------------------------
49 soapReturn = holder.getNodeValue( "//soapExecuteReturn" )
50 soapResponse = groovyUtils.getXmlHolder(soapReturn)
51 responseStatus = soapResponse.getNodeValue("//ResponseStatus")
52 // -------------------------------------------------------------
// Capturing SOAP response to “soapResponse” and “Status” to “responseStatus”
53
// -------------------------------------------------------------
54 if ("FAILURE".equalsIgnoreCase(responseStatus))
55 {
56 log.info("Check Dmoain Status ////////////////: " + responseStatus)
57 ErrorResponse = soapResponse.getNodeValue("//ErrorMsg")
log.info("Check Dmoain Status ////////////////: " + ErrorResponse)
58 Label label1 = new Label(2, i, responseStatus.toString());
59 cell1 = (WritableCell) label1;
60 sheetToEdit.addCell(cell1);
61 Label label3 = new Label(4, i, EndPointURL.toString());
62 cell3 = (WritableCell) label3;
sheetToEdit.addCell(cell3);
63 Label label4 = new Label(5, i, ErrorResponse.toString());
64 cell4 = (WritableCell) label4;
65 sheetToEdit.addCell(cell4);
}
66
else
67 {
68 isAvailable = soapResponse.getNodeValue("//IsAvail")
69 log.info("Check Dmoain IsAvail ////////////////: " + isAvailable)
70 Label label1 = new Label(2, i, responseStatus.toString());
cell1 = (WritableCell) label1;
71 sheetToEdit.addCell(cell1);
72 Label label2 = new Label(3, i, isAvailable.toString());
73 cell2 = (WritableCell) label2;
74 sheetToEdit.addCell(cell2);
75 Label label3 = new Label(4, i, EndPointURL.toString());
cell3 = (WritableCell) label3;
76 sheetToEdit.addCell(cell3);
77 }
78 // -------------------------------------------------------------
79 // Based on the response status “FAILURE” or “SUCCESS” writing error message to log f
“log.info”
80 // Defining label and capturing the required response field from complete response
81 // Assigning the captured label value to a cell
82 // Finally writing it on sheet which is editable
83 // -------------------------------------------------------------
84 }
workbookCopy.write();
85 }
86 catch (Exception e)
87 // -------------------------------------------------------------
88 // Catching exception and writing it to error log
89 // -------------------------------------------------------------
{
90 log.error(e)
91 }
92 finally
93 {
if(workbookCopy != null)
94 {
95 workbookCopy.close();
96 }
97 if(wk != null)
98 {
wk.close();
99 }
10 }
0 log.info("Testing Completed")
10 // -------------------------------------------------------------
Catching final exception using “finally” block
1
Closing workbook if it is not null
10 Closing Worksheet if it is not null
2 Writing a note “Test Completed” to log file using “log.info”
10 // -------------------------------------------------------------
3
10
4
10
5
10
6
10
7
10
8
10
9
11
0
11
1
11
2
11
3
11
4
11
5
11
6
11
7
11
8
11
9
12
0
12
1
12
2
12
3
Conclusion
We can perform data driven testing related to database, and any SQL operation with
a customized query using Groovy script and Excel as a data source. We can also
capture test results in the same spreadsheet to generate user-friendly reports.
Additionally, test results can be presented based on our requirements using the
various features available in Microsoft Excel.