Skip to content

Instantly share code, notes, and snippets.

@pbandreddy
Created August 2, 2023 06:41
Show Gist options
  • Save pbandreddy/523029d38246f33538156e2e1510c5db to your computer and use it in GitHub Desktop.
Save pbandreddy/523029d38246f33538156e2e1510c5db to your computer and use it in GitHub Desktop.
Capture the oatuth 2 Access_token from jmeter using webdriver
import org.openqa.selenium.By
import org.openqa.selenium.JavascriptExecutor
import org.openqa.selenium.support.ui.FluentWait
import org.openqa.selenium.support.ui.Wait
import org.openqa.selenium.WebDriver
import org.openqa.selenium.WebElement
import org.openqa.selenium.support.ui.ExpectedConditions as EC
import java.time.Duration
try {
WDS.sampleResult.sampleStart(); //captures sampler's start time
WDS.sampleResult.getLatency();
WDS.log.info("Sample started");
// Launch website specified in 'URL'
WDS.browser.get('http://pt-app.bankopen.co/');
WDS.log.info("Sample ended - navigated to URL");
// Wait for the username element to be present
def wait = new FluentWait<WebDriver>(WDS.browser)
.pollingEvery(Duration.ofMillis(500))
.withTimeout(Duration.ofSeconds(10))
.ignoring(org.openqa.selenium.NoSuchElementException)
def usernameElementId = 'mat-input-0'
WebElement usernameElement = wait.until {
WDS.browser.findElement(By.id(usernameElementId))
}
// Enter username
usernameElement.sendKeys('USERNAME')
// Wait for the password element to be present
def passwordElementId = 'mat-input-1'
WebElement passwordElement = wait.until {
WDS.browser.findElement(By.id(passwordElementId))
}
// Enter password
passwordElement.sendKeys('PASSWORD')
// Wait for the login button to be present
def loginButtonSelector = 'button[flat-button][color="primary"]'
WebElement loginButton = wait.until {
WDS.browser.findElement(By.cssSelector(loginButtonSelector))
}
// Click the login button
loginButton.click()
// Wait for the "Dashboard" text element to be present
def dashboardTextSelector = 'span.text-primary.font-semibold.ng-star-inserted'
wait.until(EC.presenceOfElementLocated(By.cssSelector(dashboardTextSelector)))
// Extract the access_token using JavaScriptExecutor
def jsExecutor = (JavascriptExecutor) WDS.browser
def accessTokenScript = """
var accessToken = window.localStorage.getItem('_t');
return accessToken;
"""
def accessToken = jsExecutor.executeScript(accessTokenScript)
// Store the access_token in a JMeter variable
if (accessToken != null && accessToken.trim() != "") {
org.apache.jmeter.threads.JMeterContextService.getContext().getVariables().put("access_token", accessToken)
WDS.log.info("access_token: " + accessToken)
WDS.log.info("access_token stored in JMeter variable 'access_token'")
} else {
WDS.log.error("access_token not found or empty.")
WDS.sampleResult.setSuccessful(false)
}
WDS.sampleResult.setSuccessful(true)
} catch (Exception ex) {
WDS.log.error("Error occurred: " + ex.getMessage())
WDS.sampleResult.setSuccessful(false)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment