'Continue Session from UI to API in JMeter

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



Solution 1:[1]

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Dmitri T