无法从JSR223 Sampler获取值

时间:2018-04-09 07:04:36

标签: jmeter jmeter-3.2 jsr223

我在Jmeter中有一个JSR223采样器,代码如下:

import com.jayway.jsonpath.JsonPath
import org.apache.commons.lang3.RandomUtils
import org.apache.jmeter.samplers.SampleResult

def options = JsonPath.read(prev.getResponseDataAsString(), '$.options')

if(options.size() == "1" || options.size() == "2") {

    def randomOption = options.get(0)
    def code = randomOption.get("code")
    vars.put('code1', code)

    def values = randomOption.get('values')
    def randomValue = values.get(RandomUtils.nextInt(0, values.size()))
    def value = randomValue.get('value')
    vars.put('valueF', value)

    def options2 = JsonPath.read(prev.getResponseDataAsString(), '$.options')
    def randomOption2 = options2.get(1)
    def code2 = randomOption2.get("code")
    vars.put('code2', code2)

    def values2 = randomOption2.get('values')
    def randomValue2 = values2.get(RandomUtils.nextInt(0, values.size()))
    def value2 = randomValue2.get('value')
    vars.put('valueF2', value2)

}

else {
    vars.put('no loop','Not enterd into loop')
}

vars.put('counts',new 
groovy.json.JsonSlurper().parse(prev.getResponseData()).options.size() as 
String)

def size = com.jayway.jsonpath.JsonPath.read(prev.getResponseDataAsString(), 
'$.options_available')

if (size == []) {
    vars.put('size', 'NonConfigurable')
} 

else {
    vars.put('size', 'Configurable')
}

我无法在Sampler之外获取code1和valueF,code2和valueF2的值。任何可能的帮助表示赞赏!

2 个答案:

答案 0 :(得分:0)

尝试修改此行:

    <ul>
        <li>
            <div class="container">Container A</div>
            <ul>
                <li>...</li>
                <li>
                    <div class="item">Item A</div>
                    <ul>
                        <li>...</li>
                    </ul>
                </li>
                <li>
                    <div>Container A A</div>
                    <ul>
                        <li>...</li>
                        <li>
                            <div>Item B</div>
                            <ul>
                                <li>...</li>
                            </ul>
                        </li>
                        <li>
                            <div>Item A B</div>
                            <ul>
                                <li>...</li>
                            </ul>
                        </li>
                    </ul>
                </li>
            </ul>
        </li>
        <li>
            <div>Container B</div>
            <ul>
                <li>...</li>
                <li>
                    <div>Item C</div>
                    <ul>
                        <li>...</li>
                    </ul>
                </li>
            </ul>
        </li>
        <li>
            <div>Item X</div>
            <ul>
                <li>...</li>
            </ul>
        </li>
    </ul>

NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];
NSURLSessionDataTask *dataTask = [manager dataTaskWithRequest:request uploadProgress:^(NSProgress * _Nonnull uploadProgress) {
} downloadProgress:^(NSProgress * _Nonnull downloadProgress) {

} completionHandler:^(NSURLResponse * _Nonnull response, id  _Nullable responseObject, NSError * _Nullable error) {
    NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response;
    NSInteger statusCode = [httpResponse statusCode];
    completionBlock();
}];

[dataTask resume];

同样适用于&#34;代码:

function sum() {
  let array= [],
    ans = 0;
    
  // Array check.
  if (arguments[0] instanceof Array)
    array = arguments[0];
  else array = arguments;
  
  // Sum all from array
  const max = array.length;
  for (let i = 0; i < max; i++) {
    ans += array[i];
  }
  
  return ans;
}

console.log(sum(1,2,3));
console.log(sum([4,5,6]));
console.log(sum());
console.log(sum(7,8));

当您遇到JMeter问题时,习惯于查看 jmeter.log 文件,在大多数情况下,它应该包含足够的故障排除信息。

如果您需要有关该主题的进一步帮助,请使用您尝试解析的完整回复进行更新。与此同时:

答案 1 :(得分:0)

使用length()时应使用

JSONArray大小,更改代码

if(options.length() == 1 || options.length() == 2) {