我是soapUI的新手,仍在探索时髦的剧本,我需要你的帮助。 这是我的json回复:
public class HttpLoggingInterceptor implements ClientHttpRequestInterceptor {
private final static Logger logger = LoggerFactory.getLogger(HttpLoggingInterceptor.class);
@Override
public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {
logger.info("request method: {}, request URI: {}, request headers: {}, request body: {}",
request.getMethod(), request.getURI(), request.getHeaders(), new String(body, Charset.forName("UTF-8")));
ClientHttpResponse response = execution.execute(request, body);
logger.info("response status code: {}, response headers: {}, response body: {}",
response.getStatusCode(), response.getHeaders(), new String(ByteStreams.toByteArray(response.getBody()), Charset.forName("UTF-8")));
return response;
}
} ]
如何获取userId = 36的密码值并将其传递给我的其他测试用例?我搜索了很多,但找不到关于现有问题的正确指南。我应该提一下,我的反应会不时变化,所以我不能把它当成字符串。 感谢您的所有回复。
答案 0 :(得分:3)
我建议您使用JsonSlurper
。
对于像这样的Json文件: Example.json
{
"userAccessDto": {
"userAccessId": 37,
"userId": 37,
"password": "a05qHK+KrXXmHTFFGQN9JRQWkHnjJX+SCmqBK1PAa2f95I8e20JNt5GaVxL5nGbnTReobSZ/vej3qCAsZK9Q7DEyMDE=",
"deleted": "false"
}
}
Groovy代码:
import groovy.json.JsonSlurper;
def root =new JsonSlurper().parse(new File ('/tmp/example.json'))
println root.userAccessDto.password
答案 1 :(得分:1)
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");
//运行测试用例
runner = testCase.run(new
com.eviware.soapui.support.types.StringToObjectMap(), false);
Thread.sleep(3000)
//从testStep获取JSONresponse并解析它
def responseContent = testCase.getTestStepByName("ApiV1UsersGet -Get
all").getPropertyValue("response")
slurperResponse = new JsonSlurper().parseText(responseContent)
//从TestSuite属性中获取userId以与
进行比较newUserId =
testRunner.testCase.testSuite.project.getTestSuiteByName("Users").
getPropertyValue("idOfUser")
//循环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;
}
}
//将hashPassword和empId分配给testSuite属性以进行下一个测试步骤
testRunner.testCase.testSuite.project.getTestSuiteByName("Users")
.setPropertyValue("Hash_Pass",hashPass.toString())
testRunner.testCase.testSuite.project.getTestSuiteByName("Users").
setPropertyValue("EmployeeId2",empId.toString())
答案 2 :(得分:0)