首页
学习
活动
专区
圈层
工具
发布

Jmeter接口测试之用户自定义变量(九)

在使用Jmeter做接口自动化测试中,经常会使用到公共的数据,那么就需要对这些公共的数据分离出来,不管是基于测试框架的思想,还是使用工具来进行做自动化测试,公共数据的分离首先是需要思考的。这里就以获取电话号码归属地来作为测试的案例,看接口的文档如下:

代码语言:javascript
复制
POST /WebServices/MobileCodeWS.asmx HTTP/1.1
Host: ws.webxml.com.cn
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://WebXml.com.cn/getMobileCodeInfo"

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <getMobileCodeInfo xmlns="http://WebXml.com.cn/">
      <mobileCode>string</mobileCode>
      <userID>string</userID>
    </getMobileCodeInfo>
  </soap:Body>
</soap:Envelope>
HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: length

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <getMobileCodeInfoResponse xmlns="http://WebXml.com.cn/">
      <getMobileCodeInfoResult>string</getMobileCodeInfoResult>
    </getMobileCodeInfoResponse>
  </soap:Body>
</soap:Envelope>

在如上信息中,可以得出如下的信息:

请求地址:

http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx

请求头:

代码语言:javascript
复制
Content-Type: text/xml; charset=utf-8

请求参数:

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <getMobileCodeInfo xmlns="http://WebXml.com.cn/">
      <mobileCode>string</mobileCode>
      <userID>string</userID>
    </getMobileCodeInfo>
  </soap:Body>

其中请求参数中userID可以为空,而请求参数mobileCode不能为空

打开Jmeter测试工具,创建线程组并且新增该接口的测试用例,如图所示:

那么在这个案例中,参数mobileCode它就是一个变量,我们完全可以把它分离出来,因为依据测试用例的不同查询的电话号码是不同的,在计划中,添加用户自定义的步骤为:添加--->配置元件--->用户自定义的变量,添加用户自定义变量后,定义变量mobile,如下图所示:

那么下来就是调用该变量,在Jmeter中调用的方式为&{自定义的变量},在接口变量中调用,我们在请求参数中直接调用mobile的变量,如下图所示:

再次执行接口用例,执行的结果如图所示:

见发送请求后,服务端返回的响应数据:

也许看到这里,有人心里会想,那么每次请求可以除了这种方式可以自定义不同的电话吗?答案是可以的,这就是下一节需要讲的知识体系。

下一篇
举报
领券