1
votes

I am new in soapUI and still exploring the groovy script,I need your help. this is my json response:

 {
  "userId": 36,
  "userTypeId": 2,
  "name": "Name_7",
  "surname": "R",
  "friendlyName": "",
  "employeeId": "1000245",
  "role": "engineer",
  "dateOfBirth": "1985-07-09T01:46:23.213",
  "fPicture": "",
  "deleted": false,
  "visitingUserId": null,
  "mineId": 1,
  "crewId": null,
  "versionAutoId": 10002814,
  "externalId": null,
  "email": null,
  "externalLoginId": null,
  "sourceJson": null,
  "description": null,
  "propertyJson": null,
  "userGroups":       [
     {"groupCode": "ALL_USERS"},
     {"groupCode": "MTCC_ADMIN"}
  ],
  "userAccessDto":       {
     "userAccessId": 36,
     "userId": 36,
     "password": "XKjgXD6o/pjHaHd6swvkB8TiQ6L1kEC8307sV94F2GeiFnb4QXUZJhk8rdQJgvdJujSPK/NoM94CMtp8X51ExTEwMDAyNDU=",
     "deleted": false
  }
   },

{
  "userId": 37,
  "userTypeId": 5,
  "name": "Name_9",
  "surname": "R",
  "friendlyName": "",
  "employeeId": "1201",
  "role": "engineer",
  "dateOfBirth": "1985-07-09T01:46:23.213",
  "fPicture": "",
  "deleted": false,
  "visitingUserId": null,
  "mineId": 6,
  "crewId": null,
  "versionAutoId": 10031438,
  "externalId": null,
  "email": null,
  "externalLoginId": null,
  "sourceJson": null,
  "description": null,
  "propertyJson": null,
  "userGroups":       [
     {"groupCode": "ALL_USERS"},
     {"groupCode": "MTCC_ADMIN"}
  ],
  "userAccessDto":       {
     "userAccessId": 37,
     "userId": 37,
     "password": "a05qHK+KrXXmHTFFGQN9JRQWkHnjJX+SCmqBK1PAa2f95I8e20JNt5GaVxL5nGbnTReobSZ/vej3qCAsZK9Q7DEyMDE=",
     "deleted": false
  }

} ]

How can I get the Password value for userId = 36 and pass it to my other test cases? I searched a lot but couldn't find a proper guide on existing asked questions. I should mention that my response changes from time to time, so I can't assume it as string. Appreciate all your responses.

3
Update your question with valid json file and add what you have tried..! - Bhanuchander Udhayakumar

3 Answers

3
votes

I suggest you to use JsonSlurper.

For a Json file like this: Example.json

{
  "userAccessDto": {
    "userAccessId": 37,
    "userId": 37,
    "password": "a05qHK+KrXXmHTFFGQN9JRQWkHnjJX+SCmqBK1PAa2f95I8e20JNt5GaVxL5nGbnTReobSZ/vej3qCAsZK9Q7DEyMDE=",
    "deleted": "false"
  }
}

Groovy Code:

import groovy.json.JsonSlurper;

def root =new JsonSlurper().parse(new File ('/tmp/example.json'))

println root.userAccessDto.password
1
votes
import net.sf.json.groovy.*

def i, newUserId, hashPass, empId;
//get test case from other project or from the same one
project = 
testRunner.getTestCase().getTestSuite().getProject().
getWorkspace().getProjectByName("API_Services_v3.0.321")
testSuite = project.getTestSuiteByName("Users");
testCase = testSuite.getTestCaseByName("Users-Retrieve a list of users");

// run test case

runner = testCase.run(new 
com.eviware.soapui.support.types.StringToObjectMap(), false);
Thread.sleep(3000)

//get JSONresponse from the testStep and parse it

def responseContent = testCase.getTestStepByName("ApiV1UsersGet -Get 
all").getPropertyValue("response")
slurperResponse = new JsonSlurper().parseText(responseContent)

//get userId from the TestSuite property to compare against

newUserId = 
testRunner.testCase.testSuite.project.getTestSuiteByName("Users").
getPropertyValue("idOfUser")

//loop throught the JSONresponse

for(i=0;i<slurperResponse.resource.size();i++)
{
            if(slurperResponse[i].userId == newUserId.toInteger() )
            {
                            log.info("Hash Pass is" +slurperResponse[i].userAccessDto.password);
                            log.info("Employee Id is" +slurperResponse[i].employeeId);
                            hashPass = slurperResponse[i].userAccessDto.password
                            empId = slurperResponse[i].employeeId

                            break;
            }
}

//assign hashPassword and empId to the testSuite properties for next test step

testRunner.testCase.testSuite.project.getTestSuiteByName("Users")
.setPropertyValue("Hash_Pass",hashPass.toString())
 testRunner.testCase.testSuite.project.getTestSuiteByName("Users").
setPropertyValue("EmployeeId2",empId.toString())
0
votes

I see that you found a way, though I'd call it the hard way. You could've simply use JSONPath option in Property Transfer. A query like this works:

$.[?(@.userId==36)].password

Further, you could turn the userId to a parameter for dynamic checks.

Property Transfer with JSONPath