Using Java library for excel
Using SOAP UI, create properties
Create Test Suite in SOAP UI
Test
Source:
https://inderpsingh.blogspot.com/2019/07/SoapUIDataDrivenTesting.html
http://lifeofanoracleprodigy.blogspot.com/2019/09/updating-customer-site-profiles-classes.html
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")
This is how you can also do SoapUI data driven testing with Groovy script. If you want to see this complete Soap UI data driven testing example, it is available in my SoapUI data driven testing tutorial. Thank you.