Continue Session from UI to API in JMeter

Viewed 87

Problem Statement: In my application there is a token which is unable to generate it from the api call directly and other way because the it is implemented with OAUTH. So I took a detour and automate it through API. by using Webdriver sampler. with following script.

WDS.sampleResult.sampleStart()
WDS.browser.get('appurl');
java.lang.Thread.sleep(10000);
WDS.browser.findElement(org.openqa.selenium.By.id("details-button")).click();
WDS.browser.findElement(org.openqa.selenium.By.id("proceed-link")).click();
WDS.browser.findElement(org.openqa.selenium.By.id("i0116")).sendKeys("username"); //enter user name and password in login.microsoft.com
WDS.browser.findElement(org.openqa.selenium.By.id("idSIButton9")).click();
WDS.browser.findElement(org.openqa.selenium.By.id("i0118")).sendKeys("password");
WDS.browser.findElement(org.openqa.selenium.By.id("idSIButton9")).click();
WDS.browser.findElement(org.openqa.selenium.By.id("idSIButton9")).click();
//WDS.sampleResult.sampleEnd()

This works fine and I am able to retrieve the token by using regex extractor. enter image description here

enter image description here

Using the Token in API Call and getting retry error

JMeter Failure (getting IO error exceeded number of retry =, tried with 20, 50 ,200 and now 500)

enter image description here

Browser: enter image description here

Suspecting the session id may be I am not transferring or is there anything I am missing (remember I am not terminating the browser session)

enter image description here

2 Answers

I think you need to copy the cookies from the browser to the HTTP Request sampler

  1. Add HTTP Cookie Manager to your Test Plan

  2. Add the next line to your WebDriver Sampler code:

    WDS.vars.putObject('cookies', WDS.browser.manage().getCookies())
    
  3. Add JSR223 PreProcessor as a child of the HTTP Request sampler where you want to copy the session from the UI and put the following code into "Script" area:

    vars.getObject('cookies').each { cookie ->
        sampler.getCookieManager().add(new org.apache.jmeter.protocol.http.control.Cookie(cookie.getName(), cookie.getValue(), cookie.getDomain(), cookie.getPath(), cookie.secure, cookie.getExpiry().getTime()))
    }
    
  4. That's it, the HTTP Request sampler should send the cookies now and you should be authenticated

More information on manipulating cookies with Groovy code: Modifying Cookies in JMeter with Groovy

If I would be in your shoes, I should go with Dmitri answer. But we had a similar issue recently and we were sending JSessionId and Dead-SessionId in the cookie. We resolved by doing following

  1. Add a Http Cookie manager to your test plan.

  2. In the UI sampler please add the sample code,

    WDS.vars.putObject('sessionCookies', WDS.browser.manage().getCookies())

  3. Add a JSR223 PreProcessor to your Http request with following script

    vars.getObject('sessionCookies').each { scookie ->
    sampler.getCookieManager().add(new org.apache.jmeter.protocol.http.control.Cookie
    (scookie.getName(), 
    scookie.getValue(), 
    scookie.getDomain(), 
    scookie.getPath(), 
    scookie.secure, 
    System.currentTimeMillis() + 10000))
    

    }

In our case we were not sending the expiry which was giving us

ERROR o.a.j.m.JSR223PreProcessor: Problem in JSR223 script, JSR223 PreProcessor javax.script.ScriptException: java.lang.NullPointerException: Cannot invoke method getTime() on null object at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:320) ~[groovy-jsr223-3.0.3.jar:3.0.3]

So we replaced cookie.getExpiry().getTime() with System.currentTimeMillis() + 10000

Related