How to get the absolute folder location of a Jmeter .jmx project file from within itself? - 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.
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.
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?
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?

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();)}
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);
Your current .jmx file fill be stored as scriptFile JMeter Variable.
References:
GuiPackage class JavaDoc
How to use BeanShell: JMeter's favorite built-in component guide

The solution was (thanks to Ardesco) to do the following:
Use the following variables, set in the Test Plan global variables:
projectHome = ${__BeanShell(import org.apache.jmeter.services.FileServer; FileServer.getFileServer().getBaseDir();)}
jmeterHome = ${__BeanShell(System.getProperty("user.dir");)}
scriptHome = ${projectHome}/scripts

Thanks to the accepted answer, I was able to solve a related problem.
Now, I can calculate this parameter once and re-use it as a user variable.
JMETER_SCRIPTS_DIRECTORY=${__BeanShell(import org.apache.jmeter.services.FileServer; FileServer.getFileServer().getBaseDir();)}
Its usage is ${JMETER_SCRIPTS_DIRECTORY}.
Interestingly, it works in both GUI mode and NON GUI mode (from command line).

Related

I can't export a custom report name from html-reporter-extra

This really should be straightforward but I can't get it to work.
It's a simple setup: I have a locally run Jenkins, exported Postman collections that I'm running using newman. I got html-report-extra installed and it's generating a report but I can't get it to export the html file under a different name!
I have a locally installed Jenkins, I'm using a freestyle project and under Build - Execute Windows batch command I have this:
newman run IDMS4.postman_collection.json -e IDMS4.postman_environment.json --reporters cli,htmlextra --reporter-html-export newman/index.html --disable-unicode
This is how my Jenkins job is setup:
Jenkins job setup
Build completes but there is no index.html anywhere. This is the the part that's puzzling me.
In
.jenkins\jobs\Newman runner\htmlreports\HTMLReport
i get the default file format name (project name + timestamp).
In
.jenkins\workspace\Newman runner\newman
I also get project name + timestamp html files.
Why is this outputed to both folders and how can I get this to export just one index.html?
Please try with dot slash. eg: ./newman/index.html.
and also
if you need collection name to be in the report, please use following node module
https://www.npmjs.com/package/jaiman

Jmeter variable filepath is not resolved in FileToString function

I am using:
${__eval( ${__FileToString(${filePath}\x.xml)})}
to get an XML String from a file. The variable filePath is not resolved and I'm always getting the error "FileNotFound exception". If the absolute path is given, it works. It seems the variable is not resolved before the FileToString function call. Is there a way to use a relative path?
JMeter uses FileServer class to determine it's base directory.
If you look into JMeter log you'll be able to see something like:
2014/07/14 15:32:12 INFO - jmeter.services.FileServer: Default base='/opt/jmeter/bin'
In my case it's /opt/jmeter/bin
So if I drop x.xml file into /opt/jmeter/bin folder it would be resolved via ${__FileToString(x.xml,,)} function.
There is a property which can control base directory for relative paths resolution:
# Prefix used to identify filenames that are relative to the current base
#jmeter.save.saveservice.base_prefix=~/
You can set it to any other folder and JMeter will use it as the base for paths resolution.
The property can be changed in:
jmeter.properties file
user.properties file
overriden via -J command-line argument as
jmeter -Jjmeter.save.saveservice.base_prefix=c:/xml_payload/
jmeter.properties and user.properties files live under /bin folder of your JMeter installation, FileServer base is usually the same folder.
See Apache JMeter Properties Customization Guide for more details on JMeter tuning using properties.

Unable to locate file 'C:\BuildAgent\temp\buildTmp\SYSTEM_[AGENT NAME] 2013-02-06 16_25_11\

I have an MSTest build step in my TeamCity build. Some of the tests are looking for a file in a relative path which they cannot find and the tests are throwing the error...
Cannot find path...
'C:\BuildAgent\temp\buildTmp\SYSTEM_[AGENT NAME] 2013-02-06 16_25_11\Documents\json.value.list.txt'
I have a PowerShell script that I want to use to create and copy the file out to the above path. However, I cant seem to figure out the appropriate TeamCity parameters to use to construct the path...
I have...
%system.teamcity.build.tempDir%\%teamcity.agent.name%\documents
However, that gives me...
'C:\BuildAgent\temp\buildTmp[AGENT NAME]\Documents\'
What can parameters or TeamCity variables can I use to construct...
C:\BuildAgent\temp\buildTmp\SYSTEM_[AGENT NAME] 2013-02-06 16_25_11\Documents
Thanks!
If your test is reading the contents of a file, this will ensure that relative paths are resolved correctly so tests can be run on TeamCity as well as locally:
string currentDir = new System.Diagnostics.StackFrame(true).GetFileName();
var workingFile = new FileInfo(currentDir);
string fileContents = File.ReadAllText(workingFile.Directory + relativeFilePath);
relativeFilePath is the variable containing the file. fileContents will contain the contents of this file when run from TeamCity or locally.

Use relative path in Firefox extension

I develop Firefox extension with bundled executable file which should be run on browser startup.
To run process I need get nsIFile or nsILocalFile instance which points to executable file.
I know one solution how to get it using directory service:
var file = Components.classes["#mozilla.org/file/directory_service;1"].getService(Components.interfaces.nsIProperties).get("ProfD", Components.interfaces.nsIFile);
file.append("extensions");
file.append("<extension id>");
file.append("<relative path>");
But this solution has two disadvantages:
It doesn't work in development mode, when instead of installed extension I have only text file with real extension path
I'm not sure that it will work on all Firefox configurations because of hardcoded "extensions" part of the path
So is there any nicer way to run executable file which comes with Firefox extension?
Thanks.
You are making way too many assumptions about the directory structure of the Firefox profile - don't. The Add-on Manager API lets you get the path of a file inside the extension, you should use it:
Components.utils.import("resource://gre/modules/AddonManager.jsm");
AddonManager.getAddonByID("<extension id>", function(addon)
{
var uri = addon.getResourceURI("<relative path>");
var file = uri.QueryInterface(Components.interfaces.nsIFileURL).file;
...
});
A restartless addon's startup function (in the bootstrap.js file) will receive, as its first parameter, the path where the addon is installed. You can then play various tricks to read files inside the .jar file, if any: see https://github.com/protz/GMail-Conversation-View/blob/master/bootstrap.js#L55 as an example.
In a non-restartless case, I must confess I don't have much of an idea :).
I found this thread looking for a way to reference a path to an image hosted in extension's directory from a content script. Here's a solution:
Include your files in web_accessible_resources in the extension's manifest.
"web_accessible_resources": [
"images/*"
]
Absolute paths to these resources contain randomly generated UUID, therefore we're using runtime.getUrl() giving it the path relative to manifest.json. Example:
let myImg = document.createElement('img');
myImg.src = browser.runtime.getURL("images/my-img.png")

Perl::WSDL. Generated methods in different directory than script

I am trying to write a script for using with op5/nagios.
What it tries to do is to connect to database an get a value there, and then send a request to a webservice and match the response from the webservice with the value from the database.
Now writing the script wasn't the problem. The problem arrises when trying to run it from OP5.
When including the interface to script I use the following syntax
use MyInterfaces::PortalServicesImplService::PortalServicesImplPort;
This works just fine when I execute the script located on the same level as "MyInterfaces".
The problem is that the script itself is located in the folder /opt/plugins/custom/SOAP where both the script and the folders generated by wsdl2perl.pl is located. However when OP5 executes the script is does so from the folder /opt/monitor.
So I tried to include the "PortalServicesImplPort" using this syntax
use lib "/opt/plugins/custom/SOAP/MyInterfaces/PortalservicesImplServices/PortalServicesImplPort
The code compiles but when trying to run it I get an error message saying
Can't locate object method "new" via package "MyInterfaces::PortalServicesImplService::PortalServicesImplPort" (perhaps you forgot to load "MyInterfaces::PortalServicesImplService::PortalServicesImplPort"?)
Am I doing something wrong when importing methods from another location or doesn't SOAP::WSDL support placing the auto generated files in a different directory from where you are executing the script?
Seems I managed to solve this myself by adding use lib "/opt/plugins/custom/SOAP

Resources