I've been trying to get my head around JMeter all day but I'm struggling.
I'm trying to write a load test where I will go to a URL and then click a couple of buttons. To get to the page I use a URL in this format:
https://MYSITE?access_token=my_token
The problem is the site always returns a 200 even when the access token is expired so I'm trying to put an assertion in the test to check for an element with the css path button#save-button
This is what I have so far
Test Plan
└── Thread Group
├── HTTP Request (to the URL)
├── CSS/Extractor (Variable: saveButtonId, Expression: button#save-button, Attribute: id, Match No: 1)
├── JSR223 Assertion (Check if saveButton is NOT_FOUND)
└── View Results Tree
Manually if I paste the URL I'm providing with a valid token into Chrome I can see the save button I'm looking for, and I can confirm the css selector is correct, but when I use the same URL in my JMeter script the assertion always fails.
What am I doing wrong?
My HTTP Request
Protocol: https
Server name or IP: left empty
Port number: left empty
HTTP Request: GET
Path: https://MYSITE?access_token=my_token
CSS Selector Extractor
Variable: saveButtonId
CSS Selector expression: button#save-button
Attribute: id
Match No.: 1
Default value: left blank
JSR223 Assertion
Script:
Thread.sleep(10000); // give it time to load before looking for save button
if (vars.get("saveButtonId") == null || vars.get("saveButtonId").isEmpty()) {
AssertionResult.setFailure(true);
AssertionResult.setFailureMessage("Save button not found on the page");
} else {
AssertionResult.setFailure(false);
}
I have this sleep in the script but I know it's not a loading issue, the page displays the button within a second when I try in Chrome.
When I run the test I always get a failure from the assertion even though I know the URL is using a valid token.
Assertion error:false
Assertion failure:true
Assertion failure message:Save button not found on the page
The HTTP Request result is returning a 200:
Thread Name:Thread Group 1-1
Sample Start:2024-10-02 20:15:38 BST
Load time:105
Connect Time:68
Latency:105
Size in bytes:11121
Sent bytes:342
Headers size in bytes:1500
Body size in bytes:9621
Sample Count:1
Error Count:1
Data type ("text"|"bin"|""):text
Response code:200
Response message:OK
HTTPSampleResult fields:
ContentType: text/html
DataEncoding: UTF-8
Your Groovy code is correct, if CSS Selector Extractor fails to locate the button - the saveButtonId
variable will be null
You don't need any "sleep" because according to JMeter test elements Execution Order the sequence will be the following:
So I would suggest checking whether your CSS expression is correct and returns the button you're looking for. It can be done i.e. in Chrome Developer Tools
For example if I use a Dummy Sampler with response data like this:
<button id="save-button">Save</button>
the assertion passes and when I change the ID to something else:
<button id="something-else">Save</button>
it fails. So please re-visit your CSS selector expression