SoapUI - property transfer between test cases

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.

  1. Create a test suite and a test case, and add the login request.
  2. To get the correct format authorization token.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    import 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 authorization
  3. Transer 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.

  1. Separate login test suite.
  2. Set customized token to a property use Groovy.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    import 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)
  3. Setup get calendar test suite.

  4. Add ‘Run TestCase’ in test case.
  5. 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.

  1. In get calendar case, we use groovy script to run login request instead of Run TestCase.
    In CaseB > getCalendar > Login

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    import 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")
  2. Then need to update the pass parameter to login request.

  3. Add token(return from ‘Login’ step) to get calendar request.
唐胡璐 wechat
欢迎您扫一扫上面的微信公众号,订阅我的博客!
分享创造价值,您的支持将鼓励我继续前行!