'How to extract variable from jmeter selenium webdriver

I have a token thats being returned in the body.

WDS.browser.findElement(org.openqa.selenium.By.xpath("//*[contains(@name,'RequestVerificationToken')]")).getText();

This is the token I'm trying to extract:

enter image description here

Now what im trying to do is to pass that variable out of the selenium webdriver to the rest of the threads.

Usually when im working with normal pages I go for regular expression extractor as a post processor and set to a variable then correlate it but Im not sure where to go here.

How do I make it a global variable in JS?



Solution 1:[1]

You can consider the complete value of the name attribute. Additionally, the token is actually the value attribute, so you have to use getAttribute("value") instead as follows:

WDS.browser.findElement(org.openqa.selenium.By.xpath("//input[@name='__RequestVerificationToken']")).getAttribute("value");

Solution 2:[2]

The element you want to interact with doesn't have text, what you need is value attribute so you need to amend your code as:

var token = WDS.browser.findElement(org.openqa.selenium.By.xpath("//*[contains(@name,'RequestVerificationToken')]")).getAttribute('value')

If you want to share this token with all threads in this or other Thread Groups you can save it into a JMeter Property like:

WDS.props.put('token', token)

so in other WebDriver Samplers instances you will be able to access it as:

var token = WDS.props.get('token')

and in other JMeter test elements it will be possible to use __P() function like:

${__P(token,)}

More information: The WebDriver Sampler: Your Top 10 Questions Answered

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
Solution 2 Dmitri T