When doing Restful API automation with SoapUI, case calling another case is very usually, here i did a little research, just a start.
Simple Demo
This demo is: login system, get the authorization token, then get the calendar list.
- Create a test suite and a test case, and add the login request.
To get the correct format authorization token.
1
2
3
4
5
6
7
8
9
10import groovy.json.JsonSlurper
import groovy.json.*
def response = context.expand( '${Login#Response}' )
def JsonSlurper jsonSlurper = new JsonSlurper()
def Object result = jsonSlurper.parseText(response)
def token = result.token
def authorization = "Bearer "+token
return authorizationTranser property to getCalendar request.
Run TestCase
From demo we can see, the login request adding in get calendar API, in this way, we will adding login request in each test case. if the login request change someday, we need to update all test cases, not easy to maintain, so here we can separate login request to another test suite.
- Separate login test suite.
Set customized token to a property use Groovy.
1
2
3
4
5
6
7
8
9
10import groovy.json.JsonSlurper
import groovy.json.*
def response = context.expand( '${Login#Response}' )
def JsonSlurper jsonSlurper = new JsonSlurper()
def Object result = jsonSlurper.parseText(response)
def token = result.token
def authorization = "Bearer "+token
testRunner.testCase.setPropertyValue("token", authorization)Setup get calendar test suite.
- Add ‘Run TestCase’ in test case.
- Add token(return from ‘Run TestCase’ step) to get calendar request.
Groovy Script
Use ‘Run TestCase’ method, it’s not easy to determine which user to login, need to maintain in the login suite, the best is to pass user/pwd from get calendar request, and here we go.
In get calendar case, we use groovy script to run login request instead of Run TestCase.
In CaseB > getCalendar > Login1
2
3
4
5
6
7
8
9
10
11
12
13
14
15import com.eviware.soapui.support.types.StringToObjectMap
// get your test case
def testCase1 = testRunner.testCase.testSuite.project.getTestSuiteByName("CaseA").getTestCaseByName("pass")
// set the user and password properties in the context
context.setProperty("Login_userName","yongfei.hu")
context.setProperty("Login_password","Beyond@123")
// run the testCase
def contextMap = new StringToObjectMap( context )
testCase1.run(contextMap,false)
// return the token value
return testCase1.getPropertyValue("token")Then need to update the pass parameter to login request.
- Add token(return from ‘Login’ step) to get calendar request.