JMeter Script Path missing "\" so not working?

902 Views Asked by At

I am trying to use the beanshell script posted here to get the path of the jmx that is being run in my jmeter test - Access to JMeter script path

It is working and if I log the output of the path when set by beanshell or view the variables with the debugger I get the path to the script displayed as I expected -

c:\my\path\to\script

but when I then try to pass that variable into sendKeys, the slashes "\" are being removed so -

c:mypathtoscript

And this doesn't work so I am unable to attach/upload my file..

Sure I am missing something stupid

Thanks

2

There are 2 best solutions below

1
On

There are at least 2 ways to get this done without using Beanshell:

  1. Call FileServer methods from WebDriver Sampler:

    someElement.sendKeys(org.apache.jmeter.services.FileServer.getFileServer().getBaseDir())
    
  2. Get the value from JMeterVariables

    var vars = org.apache.jmeter.threads.JMeterContextService.getContext().getVariables()
    someElement.sendKeys(vars.get('homepath'))
    

Example full code:

WDS.sampleResult.sampleStart()
WDS.browser.get('http://ya.ru')
var searchInput = WDS.browser.findElement(org.openqa.selenium.By.id('text'))

//directly access function from JavaScript
searchInput.sendKeys(org.apache.jmeter.services.FileServer.getFileServer().getBaseDir())

//alternative way - getting the value from JMeter Variables
var vars = org.apache.jmeter.threads.JMeterContextService.getContext().getVariables()
searchInput.sendKeys(vars.get('homepath'))

WDS.sampleResult.sampleEnd()

Comprehensive information on accessing JMeter API classes from WebDriver Sampler and few more tips and tricks: The WebDriver Sampler: Your Top 10 Questions Answered

0
On

Needed to user vars.put to put the JMeter UDV value into a Javascript variable, then use javascript concatenate to link it all together.