Jmeter script parameters and regular match

  We do interface testing process, often encounter the following situations

  • With each request, the parameter values ​​need to change parameter is not used, such as mobile phone number registration, dynamic time
  • On a request body parameters of the interface request body parameters for an interface
  • A body parameter in response interface for requesting a body interface parameters, such as: sessionId, token
  • Interface on a response body url parameters for the interface address of the next interface
  • A header parameter for the interface in response to the request body parameters of an interface, such as: token
  • ......

  For All these circumstances, we can be resolved by way of regular and parametric extraction

A parametric

  For example demo, provides an interface registration and login, request body needs the mobile phone number and password to register log in, and registered account to re-register will be prompted registered as either unregistered or registered mobile phone number to register interface will be called success, so I will add an assertion on the registration interface

Registration interface:

 Interface login:

 Affirm:

 

When we register again, the assertion will fail

1. The user-defined variables

  See the example above, each time to call interface must be manually modify the phone number, and registration and login modify the interface to be synchronized so that the operation is quite cumbersome, address this issue we use instrumental variables user-defined optimization

  On Thread Group: Right -> Add -> configuration elements -> user-defined variables

   We registered and logged phone number and password are extracted into here

   Once defined, we need to call this parameter is called by the corresponding sampler, the sampler body replace the value in the request to use $ {key} format, so that we can each only be modified once all the a common interface

  While on this basis has been a corresponding optimization, we can not do it every time you run not spent modify the value of user-defined variables inside of it? The answer is yes, we want to use this time to Assistant function, structure corresponding random function

   For various uses of functions, are not described here, you can go to see the usage of each function with the help of assistant function

  We value to construct a method to generate a random phone number by function assistant to replace a user-defined variables

2.CSV data file settings

  该方法也是参数化的一种场景,我们可以提前使用csv文件或者其它文本文件(txt、log、xml、json...)构造大量数据,然后再读取文件里面的内容引用到对应的接口当中去,这里要注意的是在国内csv我们使用excel编辑器保存后遇到中文会出现乱码的情况,因此需要进行转码后才能正常使用!

  在线程组上:右键—>添加—>配置元件—>CSV 数据文件设置

   我们先构造测试数据

   然后配置CSV数据文件设置:

   此时我们可以直接替换接口参数的值为该变量名称,但是不可以替换用户定义的变量的值,会引用不到

  执行结果如下:

  我们如果设置并发量为5,则会依次读取每一行数据

 

3.用户参数

  在用户定义的变量中使用随机生成手机号的方式执行,它的执行原理是在整个线程组执行会话之前创建一次后,执行过程中的所有线程都会去使用这个值,并不是每个线程组使用时都会随机生成不一样的值,所以在并发测试中,遇到这种情况就不行了,因此我们可以用户参数的工具来使得每个线程组调用的时候都能随机生成一个随机数

  在线程组上:右键—>添加—>前置处理器—>用户参数

   我们可以将上面用户定义的变量中由函数助手生成的函数拿到用即可

   我们将接口参数化替换

  设置并发为10,查看结果:

 

二、正则提取

  上面我们处理的是随机数,这里我们则要通过一些方法将下个接口要使用的参数用上个接口的请求体、响应头或者响应体中提取出来,以此来处理接口依赖的问题,因为暂时没有好的实例,这里只讲使用方法

1.JSON提取器

  在指定的接口取样器上:右键—添加—后置处理器—JSON提取器

  

  我们在查看结果树里面将JSON Path Tester调出来,可以进行编写表达式进行测试验证表达式是否正确,注意该提取器只适用于接口的json响应体

   JSON提取器的表达式语法格式为:$.key的格式

  如果响应体遇到嵌套列表的形式,如:

{"domain": 
    [
     {"id": "sdfhhsdfafvgg"}, 
     {"name": "Tom"}
    ]
}

  如果要提取name的值,表达式为:$.domain[1].name

{
    "tenant": "admin",
    "domain": [
         {"id": "sdfhhsdfafvgg", "name": "Tom"},
         {"id": "234gdgdh45h", "name": "Jerry"}
         ]
    }    

如果要提取所有name的值,表达式为:$..name  返回结果为一个列表

我们将正确的表达式填入到JSON提取器的配置当中,后面的接口就可以通过${变量名称}的方式调用该参数

2.正则表达式提取器

  在指定的接口取样器上:右键—添加—后置处理器—正则表达式提取器

  正则表达式的格式:左边界(.*?)右边界  以code为例:

  我们执行一次查看提取结果:

  更多关于正则表达式的语句请跳转到此博客地址:https://www.cnblogs.com/xiaogongjin/p/11986493.html

Guess you like

Origin www.cnblogs.com/xiaogongjin/p/11986688.html