0
votes

I have jMeter test case already written, it has requests for logging in and some POST requests to my table. Now I want to measure render time of the page (response of the POSTS). I want to use Selenium so I read: Running Selenium scripts with JMeter

The problem is that i want Selenium to use the same session (in other words: to be already logged in) as logging in was handled by jMeter already. How can i archive that ?

My TestPlan:

enter image description here

[UPDATE] @Dmitri T

Thanks for answer! I did what you have suggested but it still don't work. Maybe I am doing something wrong but still just after Selenium opens browser it goes to login page. I put JSR223 PostProcessor under my LOGIN POST request and this is my WebDriver Sampler:

var pkg = JavaImporter(org.openqa.selenium);
var support_ui = JavaImporter(org.openqa.selenium.support.ui.WebDriverWait);
var wait = new support_ui.WebDriverWait(WDS.browser, 5000);

WDS.sampleResult.sampleStart(); 
WDS.sampleResult.getLatency();
WDS.log.info("Sample started");

WDS.browser.get(WDS.parameters);
WDS.log.info("LOGGING INTO: " + (WDS.parameters))

var cookieManager = WDS.vars.getObject('cookieManager')

for (var i=0; i < cookieManager.getCookieCount(); i++) {
    var jmeterCookie = cookieManager.getCookies().get(i)
    var seleniumCookie =  new org.openqa.selenium.Cookie(jmeterCookie.name, jmeterCookie.value, jmeterCookie.domain, jmeterCookie.path, java.util.Date.from(java.   time.Instant.ofEpochMilli(jmeterCookie.expiresMillis)), jmeterCookie.secure)
    WDS.browser.manage().addCookie(seleniumCookie)
}

java.lang.Thread.sleep(5000)

WDS.sampleResult.sampleEnd();

enter image description here

{UPDATE 2}

Ok, i think that htere is something wrong with Cookies in all Thread Group. I have [no cookies] on every request:

enter image description here

1

1 Answers

1
votes

In order to pass the "session" you need to copy all the cookies from JMeter's HTTP Cookie Manager into the Selenium session.

This can be done in the WebDriver Sampler directly as follows:

  1. Add JSR223 PostProcessor as a child of the request which session you want to copy
  2. Put the following code into "Script" area

    vars.putObject('cookieManager', sampler.getCookieManager())
    

    it will store the current state of the HTTP Cookie Manager into JMeter Variables

  3. In the WebDriver Sampler you can copy the cookies from the HTTP Cookie Manager and add them to the WebDriver instance using WDS.browser.manage().addCookie() function as follows:

    var cookieManager = WDS.vars.getObject('cookieManager')
    
    for (var i=0; i < cookieManager.getCookieCount(); i++) {
        var jmeterCookie = cookieManager.getCookies().get(i)
        var seleniumCookie =  new org.openqa.selenium.Cookie(jmeterCookie.name, jmeterCookie.value, jmeterCookie.domain, jmeterCookie.path, java.util.Date.from(java.time.Instant.ofEpochMilli(jmeterCookie.expiresMillis)), jmeterCookie.secure)
        WDS.browser.manage().addCookie(seleniumCookie)
    }