问题描述
我正在 JMeter 中创建一个 HTTP 请求采样器来自动将文件上传到 http 服务.有没有办法将随请求发送文件"中的文件路径"设置为jmx文件所在位置的相对路径?
I am creating a HTTP Request Sampler in JMeter to automate uploading file to a http service. Is there a way to set the "File Path" in the "Send Files With the Request" to a relative path from the location of the jmx file?
推荐答案
是的.(这在 JMeter v2.9 中得到了确认)
Yes. (This was confirmed in JMeter v2.9)
您可以在文件名输入字段中使用以下 BeanShell 表达式:
You can use the following BeanShell expression in the filename input field:
${__BeanShell(import org.apache.jmeter.services.FileServer; FileServer.getFileServer().getBaseDir();)}${__BeanShell(File.separator,)}<YOUR FILENAME HERE>
因此,如果您的文件upload.jpg"与 JMX 测试文件位于同一文件夹中,则完整的值为
So if your file, "upload.jpg" was located in the same folder as your JMX test file, the complete value would be
${__BeanShell(import org.apache.jmeter.services.FileServer; FileServer.getFileServer().getBaseDir();)}${__BeanShell(File.separator,)}upload.jpg
为了让它更简洁,添加一个用户定义的变量"配置元素,您可以在其中将表达式的基数分配给变量,例如
To keep it a bit cleaner, add a "User defined variables" config element where you assign the base of the expression to a variable e.g
SCRIPT_PATH = ${__BeanShell( ...etc
那么你的文件路径输入值会更易读:
Then your file path input value would read a more readable:
${SCRIPT_PATH}upload.jpg
这篇关于JMeter 从 jmx 文件上传带有相对路径的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!