SOAPUI 实现 一键化 SOAP 登录
测试soap接口需要先登录,但登录加密过程复杂,难道必须手动 发送多次请求并拷贝最终的cookie到待测请求的header?只要你敢想,就没什么做不到的。笔者就因为太懒,Google一下,果然 Groovy 脚本可以帮我们实现 自动化流程,无需亲自动手
1. 在Header中 传递 Cookie
用例步骤截图:
关键setSKCookie 的 Groovy脚本内容如下:
def responseCookie = testRunner.testCase.getTestStepByName("setSK").httpRequest.response.responseHeaders["Set-Cookie"]
def JSESSIONID = (responseCookie =~ "JSESSIONID=\\w{32}")[0] //正则表达式截取需要的部分
def newHeaders = testRunner.testCase.testSteps["login"].getHttpRequest().getRequestHeaders()
def list = []
list.add(JSESSIONID)
newHeaders["Cookie"] = list;
testRunner.testCase.testSteps["login"].getHttpRequest().setRequestHeaders(newHeaders)
//testRunner.testCase.testSteps["login"].httpRequest.requestHeaders = newHeaders
此脚本实现了获取setSK的请求结果中的cookie,并把该cookie放至 login请求的header 中。
2. 在 body中传递 xml 节点值
用例截图:
上代码:
import com.tool.secrectKey.soap.Base64Utils;
import com.tool.secrectKey.soap.RSA2048Utils;
import com.tool.secrectKey.soap.AES128Utils;
def resXML = testRunner.testCase.getTestStepByName("getPk").httpRequest.getResponseContentAsXml()
def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context )
def holder = groovyUtils.getXmlHolder(resXML)
def publicKey= holder.getNodeValue("//publicKey")
def iv="1234567890123456".getBytes()
def secretKey= Base64Utils.encode(RSA2048Utils.encode(AES128Utils.byte2Hex(iv).getBytes("UTF-8"), publicKey));
log.info "pk: ${publicKey}"
def requestBody='''<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:esdk="esdk_platform">
<soapenv:Header/>
<soapenv:Body>
<esdk:setSecretKey>
<secretType>AES128</secretType>
<secretKey>'''+"${secretKey}"+'''</secretKey>
<iv>'''+"${secretKey}"+'''</iv>
</esdk:setSecretKey>
</soapenv:Body>
</soapenv:Envelope>'''
testRunner.testCase.testSteps["setSK"].getHttpRequest().setRequestContent(requestBody)
以上脚本实现了从第一个请求中获取 response body,并通过引用的 jar包 对body进行加密输出,最终通过 把加密字串放至登录请求的body中。
参考:
1. SOAPUI 接口自动化测试:
https://blog.csdn.net/powertoolsteam/article/details/78643464
https://www.cnblogs.com/wade-xu/p/4236295.html
https://blog.csdn.net/qq_15158911/article/details/53445340
2. SOAPUI 传递 header
https://testerhome.com/topics/3575
3. soapUI 调用 Jar 包