JMeter接口測試-JSR223內置變量使用-3

2、vars

2.1 vars簡介

<code>vars是最常用的JMeter變量之一。
在JMeter內部,映射org.apache.jmeter.threads的JMeterVariables類,
vars提供了對當前線程變量的讀寫能力。所有的JMeter變量都是java字符串,
如果你需要把數據存放到一個JMeter變量中,需要先將它轉換成字符串。
參見:https://jmeter.apache.org/api/org/apache/jmeter/threads/JMeterVariables.html/<code>

2.2 vars常用方法

2.2.1 getThreadName

  • 方法

public String getThreadName()

  • 功能

獲取當前運行線程名

<code>tname = vars.getThreadName()
log.info('Current thread name is ' + tname)
//output
Current thread name is Thread Group 1-1/<code>

2.2.2 getIteration

  • 方法

public int getIteration()

  • 功能

獲取線程當前的迭代號

<code>//前置條件,將線程組下的“Loop Count”設置為2
iternum = vars.getIteration()
log.info('Current Iteration number is ' + iternum)
//output
Current Iteration number is 1
Current Iteration number is 2/<code>

2.2.3 incIteration

  • 方法

public void incIteration()

  • 功能

將線程當前的迭代號遞增1

<code>//前置條件,將線程組下的“Loop Count”設置為2
vars.incIteration()
iternum = vars.getIteration()
log.info('Current Iteration number is ' + iternum)
//output
Current Iteration number is 2
Current Iteration number is 4/<code>

2.2.4 remove

  • 方法

public Object remove(String key)

  • 功能

刪除一個變量,並返回變量的值,若變量不存在則返回null

<code>//1.變量不存在
v1 = vars.remove('aaaaa')
log.info('v1 is ' + v1)
//output
v1 is null
//2.變量存在
v2 = vars.remove('name')
log.info('v2 is ' + v2)
//output
v2 is zhangsan/<code>

2.2.5 put

  • 方法

public void put(String key, String value)

  • 功能

創建或更新字符串變量。key變量名,value變量值

<code>//1.創建變量
vars.put('name','zhangsan')
vars.put('age','20') //不能寫成20
vname = vars.get('name')
log.info('vname is ' + vname)

//output
vname is zhangsan
//2.更新變量
vars.put('age','24') //age已經定義
vage = vars.get('age')
log.info('vage is ' + vage)
//output
vage is 24/<code>

2.2.6 putObject

  • 方法

public void putObject(String key,Object value)

  • 功能

創建或更新一個非字符串變量。key變量名,value變量值

<code>//設置非字符串變量的值,Numbers,Lists,Arrays,Maps,Closure
vars.putObject("number1",3.14)
vars.putObject("list1",[11,22,33,44])
vars.putObject("array1",[1,2,3,4,5] as int[])
vars.putObject("map1",["name":"zhangsan","sex":1,"age":20,"city":"shenzhen"])
vars.putObject("map2",["address":"longhua","phone":"110"])
vars.putObject("closure1",{x->x+=1})
//獲取非字符串變量的值Numbers,Lists,Arrays,Maps,Closure
log.info("number1 is "+vars.getObject("number1").toString())
log.info("list1 count is "+ vars.getObject("list1").size())
log.info("list1 first element is "+vars.getObject("list1").get(0))
log.info("array1 count is "+ vars.getObject("array1").length)
log.info("map1's name key value is "+ vars.getObject("map1").get("name"))
log.info("x value is "+vars.getObject("closure1").call(1).toString())
//output
number1 is 3.14
list1 count is 4
list1 first element is 11
array1 count is 5
map1's name key value is zhangsan

x value is 2/<code>

2.2.7 get

  • 方法

public String get(String key)

  • 功能

獲取變量的值並將其轉換為字符串。若變量存在則將值轉換為字符串,否則返回null

<code>//1.變量存在
vars.put("color","red")
vcolor = vars.get("color")
log.info('vcolor is ' + vcolor)
vars.putObject("aman",["name":"zhangsan","sex":1,"age":20,"city":"shenzhen"])
vaman = vars.get("aman")
log.info('vaman is ' + vaman)
//output
vcolor is red
vaman is {name=zhangsan, sex=1, age=20, city=shenzhen}
//2.變量不存在
vfont = vars.get("font")
log.info('vfont is ' + vfont)
//output
vfont is null/<code>

2.2.8 getObject

  • 方法

public Object getObject(String key)

  • 功能

獲取變量的值(不轉換為字符串)。若變量不存在則返回null

<code>//1.變量存在
vars.putObject("age",24)
vage = vars.getObject("age")
log.info('vage is ' + vage)
vars.putObject("aman",["name":"zhangsan","sex":1,"age":20,"city":"shenzhen"])
vaman = vars.getObject("aman")
log.info('vaman is ' + vaman)
//output
vage is 24
vaman is [name:zhangsan, sex:1, age:20, city:shenzhen]
//2.變量不存在
vcontent = vars.getObject("content")
log.info('vcontent is ' + vcontent)
//output
vcontent is null/<code>


分享到:


相關文章: