Let us continue with SoapUI testing tutorials. This SoapUI tutorial for beginners is on SoapUI data driven testing with Groovy script. What is data driven testing? It means that you store the test data in some format e.g. in an XML file, an Excel sheet or a database and then use that test data in your tests. The advantage of data driven testing using SoapUI is that you can run your tests with multiple test data values. First view this SoapUI Data Driven Testing Groovy tutorial. Then continue reading.
SoapUI free version does not give the user interface to create data driven tests. However, SoapUI Pro provides a DataSource test step to get test data from sources like XML files, Excel sheets, files, directories and databases. This test data can be put into SoapUI properties and used in test steps. Also, a DataSource Loop test step is available to loop the previous test steps for each row of test data in the data source.
Note: You can see how to do data driven testing in SoapUI free version in my tutorial on SoapUI Data Driven Testing Groovy.
We can write a Groovy script in Soap UI tool to get the test data and run our test steps. This is how I implemented data-driven testing using Groovy.
We can write a Groovy script in Soap UI tool to get the test data and run our test steps. This is how I implemented data-driven testing using Groovy.
- There is a library to handle Excel files using Java code, called JExcelApi. I downloaded it from SourceForge. Then I unzipped it. After unzip, the jxl.jar should be copied to the SoapUI lib folder (alternately, it can be copied to the SoapUI bin/ext folder). Then, I re-started Soap UI.
- Next, I put my test data in an Excel file. In my case, there were two columns, one for Numbers and the other for the same number in words. I used each Number as a parameter of my test request. SoapUI load test data from file in Excel.
- Then, I added a Properties test step. I clicked on the + icon to add properties. There were 5 properties. Number stored the parameter value. Word tested the assertion. Counter, Total and End properties were used in the Groovy script logic. Counter has to have an initial value of 0. End has to have an initial value of False.
- Next, I added a Groovy script test step because we have to do data driven testing in SoapUI using groovy script.
import jxl.* // import Java Excel API library
def TestCase = context.testCase
def FilePath = "E:\\Training\\SoapUI\\Files\\NumbersWords.xls"
def count
Workbook WorkBook1 = Workbook.getWorkbook(new File(FilePath))
Sheet Sheet1 = WorkBook1.getSheet(0)
PropertiesTestStep = TestCase.getTestStepByName("Properties")
count = PropertiesTestStep.getPropertyValue("Counter").toInteger()
//If Total records is unknown (at start), get the rowcount from Excel
if (PropertiesTestStep.getPropertyValue("Total").toString() == "")
PropertiesTestStep.setPropertyValue("Total", Sheet1.getRows().toString())
count++
//Read the Excel test data
Cell Field1 = Sheet1.getCell(0, count)
Cell Field2 = Sheet1.getCell(1, count)
log.info ("Count is " + count.toString() + " Number : " + Field1.getContents() + " Word : " + Field2.getContents())
WorkBook1.close()
//Copy the Excel test data to properties in Properties test step
PropertiesTestStep.setPropertyValue("Number", Field1.getContents())
PropertiesTestStep.setPropertyValue("Word", Field2.getContents())
PropertiesTestStep.setPropertyValue("Counter", count.toString())
if (count == PropertiesTestStep.getPropertyValue("Total").toInteger() - 1)
PropertiesTestStep.setPropertyValue("End", "True")
- Also, I added a Groovy script test step to implement the data loop.
def TestCase = context.testCase
PropertiesTestStep = TestCase.getTestStepByName("Properties")
Stop = PropertiesTestStep.getPropertyValue("End").toString()
if (Stop=="True")
log.info("Exit Groovy Script - DataLoop")
else
testRunner.gotoStepByName("Groovy Script") - One thing to keep in mind is that the test steps in the test case should have the Groovy script as the first step and Groovy script with data loop as the last step.
- In the request, I put a property expansion as the parameter value. This means that the request read the Number parameter value from the property, Number. In order to test the response, I put an assertion. This assertion also used a property expansion The assertion wa tested against the property, Word.
- Ensured that the properties are initialized correctly. Then, I ran the test case.
- After the test case is run, in the Properties test step, Number and Word should have the last row data. Also, in the script log, each Number and Word should have been used.
- I also had a cleanup step (disabled in the Step 6 image above) to reset the property values after each run of the test case.
def TestCase = context.testCase
PropertiesTestStep = TestCase.getTestStepByName("Properties")
PropertiesTestStep.setPropertyValue("Number","")
PropertiesTestStep.setPropertyValue("Word", "")
PropertiesTestStep.setPropertyValue("Counter", "0")
PropertiesTestStep.setPropertyValue("Total", "")
PropertiesTestStep.setPropertyValue("End", "False")