SOAPUI 利用Groovy脚本串连登录请求——将response的header和body传递给下一个请求

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 包

https://blog.csdn.net/meyoung01/article/details/41940693

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值