Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Better way to execute Javascript in Jmeter

Tags:

jmeter

SetUp part of my Jmeter script contains steps to bypass login page of my Web Application before generating real load. As a result of these steps, server sets specific cookie that proves successful loginization.

To do that, my script should execute Javascript function injected in response by server. Since it's a one-time procedure, I do it using WebDriver and execute Javascript within browser instance.

But I definitely can't do the same as a part of load thread as WebDriver is not a good idea (at all) for performance testing.

However, I'm still wondering is it a way to calculate JS without creating a browser instance, because my JS script is nothing but arithmetic calculations (complex though). So,

  1. Any ideas about how to execute JS without creating browser instance are very much appreciated. Since I can write BeanShell code, it shouldn't be a big issue (just share the lib name that can create "virtual" browser for calculations not related to DOM). Not sure that it'll be faster, but I'll try to.
  2. If I'm right and browser usage is the only possible solution, then I'm looking for a way to share one single browser window between different threads (even with delay for those who waits for it). Tried to use JavascriptExecutor, but don't know how to convert WebDriver to JavascriptExecutor (beanshell is written using too old Java, and I'm not familiar with its syntax and functionality). Can't use WebDriver plugin too as it doesn't allow just to create window without get("pageUrl").

Thanks for any ideas in advance.

like image 298
Konstantin Dobroliubov Avatar asked Feb 24 '16 15:02

Konstantin Dobroliubov


1 Answers

  1. You can use JSR223 Sampler, choose javascript from "Language" dropdown and place your code in "Script" area

    JSR223 JavaScript

  2. You can use single browser instance using WebDriver Sampler for all threads in a loop, something like:

    var ctx = org.apache.jmeter.threads.JMeterContextService.getContext()
    var vars = ctx.getVariables();
    
    for (var i=0; i< THREADS_NUMBER; i++) {
        WDS.browser.manage().deleteAllCookies()
        WDS.browser.get('LOGIN_PAGE_URL')
        var cookie = WDS.browser.manage().getCookieNamed("COOKIE_NAME").getValue();
        vars.put("cookie" + i, cookie);
    }
    

    it will generate JMeter Variables like:

    cookie1=foo
    cookie2=bar
    etc.
    

So you will be able to refer variable values using __threadNum() function where required like:

${__evalVar(cookie${__threadNum})}

See The WebDriver Sampler: Your Top 10 Questions Answered guide for more information on using the WebDriver Sampler.

like image 197
Dmitri T Avatar answered Sep 19 '22 22:09

Dmitri T