Pages

Showing posts with label Groovy Script for SOAPUI. Show all posts
Showing posts with label Groovy Script for SOAPUI. Show all posts

Conditional and Data Driven Execution in SoapUI Sample Twitter Project

Test data can be prepared in external sources for data-driven testing. These data sources can be:
  • Excel Files
  • CSV Files
  • ODBC Sources
  • SQL / ADO Objects
I will share an example of reading data from excel file for Sample Twitter Project available in SoapUI-Tutorials. Data in the Excel file is formatted as below:

To read data in groovy script, you need to import POI or JXL libraries.Since JXL does not support .xlsx format I am using Apache POI to read data from the excel file. Latest POI JAR files can download from http://poi.apache.org/download.html. Make sure to copy all the JAR files in the lib folder of  SmartBear\SoapUI-5.3.0 before moving on!

The groovy script below can be added as first groovy script step in the test case of sample Twitter project. This script
  1. Reads data from excel file to determine if a test step should be executed or not.
  2. Determines and copies the test step name to be executed.
  3. Reads the data for property 'count' from excel file and uses it to set property value at test step level.
  4. Executes the test step with the modified count property value.
  5. Reads the test step property to verify that it was modified in step 3.
The output for above script should like below:




SoapUI Groovy Script for Sample Twitter Project


In the previous blog How to start testing SoapUI 5.3.0 Sample Twitter API Project  ,we successfully imported the Sample Twitter Project in SoapUI. Sample Twitter Project comes with one test case having four test steps.

Today we will add a simple groovy script step in the test case that will
  • Modify property value "count" for the first test step "Search Request"
  • Execute the first test step "Search Request" again from the script
  • Access the json response of test step "Search request" and write it to a file
  • Parse the json response to count number of tweets returned 
Right click on Test Steps and select Groovy Script to add a new step. Now copy paste the below code and click the play button.
All groovy scripts are invoked with following three objects. Click on the links to view a complete list of methods that you can use with these objects to modify your script.
You can check the methods for these objects and play around with the groovy script above.