如何从jmeter中的字符串中替换白色字符

时间:2018-10-30 11:40:18

标签: string groovy jmeter jsr223

import com.jayway.jsonpath.JsonPath

def path = vars.get("BaseFilePath") + "/" + vars.get("FhirVersion") + "/Get/Patient/";
def newLine = System.getProperty('line.separator')
def response = prev.getResponseDataAsString()

//address
def addressCSV = new File(path + 'address.csv')
def addressCityCSV = new File(path + 'address-city.csv')
def addressCountryCSV = new File(path + 'address-country.csv')
def addressPostalCode = new File(path + 'address-postalcode.csv')
def addressState = new File(path + 'address-state.csv')

def replacement = {    
    if (it == ' ') {
        '%20'
    } else if (it == '/') {
        '%2F'    
    } else if (it == '|')
    {
        '%7C'
    }   
    else {
        null
    }
}

def address = JsonPath.read(response, '$..address')
address.each { eachAddress ->
    eachAddress.each { subAddress ->
        subAddress.get('line').each { line ->
            addressCSV << line << newLine
        }

        addressCityCSV << subAddress.get('city').collectReplacements(replacement) << newLine
        addressState << subAddress.get('state').collectReplacements(replacement) << newLine
        addressPostalCode << subAddress.get('postalCode').collectReplacements(replacement) << newLine
        addressCountryCSV << subAddress.get('country').collectReplacements(replacement) << newLine

    }
}

这是jmeter中的JSR223代码,用于从json字符串中提取值 代码一直工作到从json取值为止

subAddress.get('city')的

值=霍金市和 subAddress.get('postalCode')= http://hl7.org/fhir/sid/us-ssn|C999-87-3780

所以我想替换白色字符:,/,|具有%20,%2F,%7C 如果该字符串不包含任何这些字符串,则将该字符串写入csv文件

代码不起作用

1 个答案:

答案 0 :(得分:0)

您正在做一些奇怪的事情,我希望您正在寻找某种形式的URL Encoding,例如JavaScript encodeURIComponent()根据您的要求执行替换。

您可以使用JMeter的__javaScript() function来调用它,例如:

${__javaScript(encodeURIComponent("String you would like to encode"),)}

演示:

JMeter Javasccript

更多信息:Apache JMeter Functions - An Introduction