如何从自身内部获取Jmeter .jmx项目文件的绝对文件夹位置? [英] How to get the absolute folder location of a Jmeter .jmx project file from within itself?

查看:736
本文介绍了如何从自身内部获取Jmeter .jmx项目文件的绝对文件夹位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个由Maven执行的Jmeter项目,并且能够通过路径src/test/jmeter/external-scripts-dir/script1.bsh定位外部Beanshell脚本,但是当我直接在计算机上的GUI中运行Jmeter时,相对位置不起作用,并且测试不能独立运行.这迫使我从Maven运行Jmeter.

I have a Jmeter project that is executed by Maven and is able to locate external Beanshell scripts by the path src/test/jmeter/external-scripts-dir/script1.bsh , but when I run Jmeter directly in the GUI on my computer the relative location doesn't work and the tests cannot be ran standalone. This forces me to run Jmeter from Maven.

因此,我有一个项目文件位于Maven布局位置,例如C:\files\git\projectA\src\test\jmeter\Project.jmx,但是由于我从C:\Jmeter2.12的安装文件夹中运行了jmeter,因此无法找到我前面提到的外部脚本的相对位置.

So, I have a project file located at a Maven layout location like C:\files\git\projectA\src\test\jmeter\Project.jmx but since I ran jmeter from its installation folder at C:\Jmeter2.12 , it cannot find the relative location of the external script I mentioned earlier.

要解决此问题,我所需要做的就是将一个变量设置为包含.jmx文件的目录.有什么可行的方法吗?

To solve this, all I need is to set a variable to the directory containing the .jmx file. Is there any possible way to do this?

我可以很容易地动态确定Jmeter(C:\Jmeter2.12)的位置(使用以下代码),但这并不能帮助我获取项目文件的位置.

I can dynamically determine the home of Jmeter ( C:\Jmeter2.12 ) pretty easily (using the following code) but that doesn't help me get the location of the project file.

${__BeanShell(import org.apache.jmeter.services.FileServer; FileServer
  .getFileServer().getBaseDir();)}${__BeanShell(File.separator,)}

是否有类似于上面的代码的内容,可以让我推断项目文件的位置?

Is there something similar to the above code that would allow me to deduce the project file location?

推荐答案

如果在GUI模式下运行JMeter时正在寻找查找当前脚本的方法,则可以尝试以下Beanshell表达式:

If you're looking for the way to locate current script when you run JMeter in GUI mode you can try the following Beanshell expression:

${__BeanShell(import org.apache.jmeter.gui.GuiPackage;GuiPackage.getInstance().getTestPlanFile();)}

如果将其分为5行,则会出现:

If you brake this down into 5 lines there will be:

import org.apache.jmeter.gui.GuiPackage;
import org.apache.commons.io.FilenameUtils;
String testPlanFile = GuiPackage.getInstance().getTestPlanFile();
String testPlanFileDir = FilenameUtils.getFullPathNoEndSeparator(testPlanFile);
vars.put("testPlanFileDir", testPlanFileDir);
log.info("testPlanFileDir:" + testPlanFileDir);

您当前的.jmx文件填充存储为scriptFile JMeter变量.

Your current .jmx file fill be stored as scriptFile JMeter Variable.

参考文献:

  • GuiPackage class JavaDoc
  • How to use BeanShell: JMeter's favorite built-in component guide

这篇关于如何从自身内部获取Jmeter .jmx项目文件的绝对文件夹位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆