How to define WDS variable in Webdriver Sampler

2,998 views
Skip to first unread message

Tom Tran

unread,
Jul 7, 2016, 11:04:03 AM7/7/16
to jmeter-plugins
As the subject, I would like to ask a question related to JMeter Plugins - Webdriver Sampler.

We already know that in Webdriver Sampler we use WDS variable as the key of this: WDS.browser, WDS.log, etc.

In normal Selenium Webdriver, we use
driver.get('http://www.google.com');
or
var element = driver.findElement(By.id('elementID'));


but in Webdriver we use WDS.browser instead:
WDS.browser.get('http://www.google.com');
var element = WDS.browser.findElement(pkg.By.id('elementID'));

So, could anyone please tell me, how to define this variable (WDS) like that, I try to investigate the source code here, it looks like the defined class for the variable nameparametersargslogbrowser and sampleResult which use for WDS.

But where is the text "WDS", how to define this? I'm very very appreciate if anyone can show me.

Thanks in advance.

Andrey Pokhilko

unread,
Jul 7, 2016, 11:32:57 AM7/7/16
to jmeter-...@googlegroups.com
-- You received this message because you are subscribed to the Google Groups "jmeter-plugins" group. To unsubscribe from this group and stop receiving emails from it, send an email to jmeter-plugin...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.

Tom Tran

unread,
Jul 7, 2016, 12:37:19 PM7/7/16
to jmeter-plugins
Many thanks Andrey Pokhilko,

I just wanna use WDS for define some functions, then call those function from Webdriver Sampler. So, can I call the API to use that variable WDS in the other places, such as BSF/BeanShell/JSR233?

If I define the function in Webdriver Sampler, it works well, but my test plan has many Thread Groups, so when one of the Thread Group end, the webdriver will be closed and functions will not be effected anymore. That's why I have to define function in BSF/BeanShell/JSR233.

My function in BSF Sampler (in 1st Thread Group) like this:

var Sampler = com.googlecode.jmeter.plugins.webdriver.sampler;
var WDS     = new Sampler.WebDriverSampler.createScriptEngineWith();


function Command() {
 
this.URL = function (URL){
 
return WDS.browser.get(URL);
 
}
}

var command = new Command();
props
.put("COMMAND", command);

And in my Webdriver Sampler (2nd Thread Group), I call that function

var props = org.apache.jmeter.util.JMeterUtils.getJMeterProperties();
var command = props.get("COMMAND");

command
.URL('http://www.google.com')


But it didn't work, and show error in BSF Sampler:

2016/07/07 23:24:39 WARN  - jmeter.protocol.java.sampler.BSFSampler: BSF error org.apache.bsf.BSFException: JavaScript Error: Java class "com.googlecode.jmeter.plugins.webdriver.sampler.WebDriverSampler" has no public instance field or method named "createScriptEngineWith".
 at org.apache.jmeter.util.BSFJavaScriptEngine.handleError(BSFJavaScriptEngine.java:202)
 at org.apache.jmeter.util.BSFJavaScriptEngine.eval(BSFJavaScriptEngine.java:152)
 at org.apache.jmeter.protocol.java.sampler.BSFSampler.sample(BSFSampler.java:98)
 at org.apache.jmeter.threads.JMeterThread.executeSamplePackage(JMeterThread.java:465)
 at org.apache.jmeter.threads.JMeterThread.processSampler(JMeterThread.java:410)
 at org.apache.jmeter.threads.JMeterThread.run(JMeterThread.java:241)
 at java.lang.Thread.run(Thread.java:745)

Can you help me, what api should I call to use the variable WDS?

Thanks,

Andrey Pokhilko

unread,
Jul 7, 2016, 1:10:00 PM7/7/16
to jmeter-...@googlegroups.com
Are you sure you want use JMeter for that? I'd rather use Taurus tool. JMeter requires you to rewrite your scripts using WDS, while Taurus allows just to use existing Selenium scripts.

http://gettaurus.org/docs/Selenium/

Andrey Pokhilko

Tom Tran

unread,
Jul 7, 2016, 1:26:32 PM7/7/16
to jmeter-plugins
Yes, of course I want.

I know there are some other tools which support writing Selenium scripts, such as Maven, Taurus...better than JMeter.

But I really wanna know can I do this in JMeter? Just help me how to call the api to use WDS variable in BSF, that's enough.

I would really appreciate your help.

Thanks,
Tom

Andrey Pokhilko

unread,
Jul 7, 2016, 1:47:18 PM7/7/16
to jmeter-...@googlegroups.com
This is why I asked about other tools, because I don't see the way for you to acces the WDS. It's hidden inside non-public methods of WebDriverConfig class.

Andrey Pokhilko

Tom Tran

unread,
Jul 7, 2016, 9:41:44 PM7/7/16
to jmeter-plugins
Oops, so I cannot do this way anymore. But I still not give up. I'm thinking another way to do this.

Could you help to confirm:
  • Can we leave the browser to keep opening after launched? I saw it automatically closed after each Thread Group.
  • Can we call the other libs from file (ex: .js) to use inside Webdriver Sampler?
Thanks,
Tom

gli...@gmail.com

unread,
Jul 11, 2016, 4:15:22 AM7/11/16
to jmeter-plugins
  1. I don't fully understand why do you need access to that WDS bit as given you use BSF you should be able to instantiate your own WebDriver and have full control on it. However if for any reason you need to re-use the associated Config element - you can access underlying WebDriver as simple as:
    import org.openqa.selenium.WebDriver;
    import com.googlecode.jmeter.plugins.webdriver.config.WebDriverConfig;

    WebDriver driver = (WebDriver) ctx.getCurrentSampler().getThreadContext().getVariables().getObject(WebDriverConfig.BROWSER);
    The above example assumes Beanshell language which in its turn provides bsh.shared namespace to share arbitrary objects across Thread Groups 
  2. You won't be able to keep this "WDS.browser" opened, I would suggest having your own WebDriver instance and pass it between Thread Groups via aforementioned bsh.shared namespace
  3. Check out JavascriptExecutor

Tom Tran

unread,
Jul 11, 2016, 2:15:18 PM7/11/16
to jmeter-plugins, gli...@gmail.com
Thanks so much for your help.

I can solve my problem now :)

amedi...@gmail.com

unread,
Apr 13, 2017, 4:42:04 PM4/13/17
to jmeter-plugins, gli...@gmail.com
Hi Tom,

can you please share the solution that you have to resolve the issue?

vlat...@gmail.com

unread,
Mar 27, 2020, 10:03:25 AM3/27/20
to jmeter-plugins
var vars = org.apache.jmeter.threads.JMeterContextService.getContext().getVariables()
vars.put('foo','bar')
var foo = vars.get('foo')
//etc
Reply all
Reply to author
Forward
0 new messages