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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import groovy.json.* | |
// Change 'count' property for step 'Search Request' | |
testRunner.testCase.testSteps['Search Request'].setPropertyValue('count', '10') | |
//define and run a new test step to execute 'Search Request' with 'count= 10' | |
def testStep_SearchRequest= testRunner.testCase.testSteps['Search Request']; | |
testStep_SearchRequest.run(testRunner,context); | |
// Storing the json response in an object | |
def response = context.expand( '${Search Request#Response}' ) | |
// Over-Write the json response in a file | |
def file = new File( 'C:\\Users\\Documents\\_response.txt' ) | |
file.write( response ) | |
//Parse json response object | |
def jsonSlurper = new JsonSlurper() | |
def object = jsonSlurper.parseText(response) | |
int i = 0 | |
//traverse through nodes to count text response | |
try | |
{ | |
while (object.statuses[i].text) | |
{ | |
i++; | |
} | |
} | |
catch(Exception e) | |
{ | |
log.info ("No more tweets. count reached") | |
} | |
log.info ("Total number of tweets returned " +i) |
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.
No comments:
Post a Comment