Performance Testing using JMeter (Part 2)- Login into a website
In this blog, I will explain steps needed to create a JMeter workflow for login into a website. I will also cover various components of a JMeter test plan components like HTTP Cookie Manager, Regular Expression Extractor and Response Assertions.
I will use http://en.wikipedia.org website for demonstrating the login workflow.
- Start with adding a new thread group and a new config element HTTP Request Defaults with Server Name as en.wikipedia.org and Protocol as https
- Now for accessing the Login Page, add a new Sampler as Http Request with Path set as /w/index.php and parameter title with value Special:UserLogin and parameter returnto with value Main Page .
Make sure to check Encode chekboxes, It will make sure that URL constructed by appending parameters is correct.
Now you will be wondering how to get these Path and Parameter values.
You can easily get these values using the Network traffic analyzer (Net) provided by firebug plugin of firefox browser.
Open firebug and open Net tab.
- Click on the Log in link on Home page of https://en.wikipedia.org
- Monitor the API calls in the Net tab
- Copy the URL and the parameters passed to the URL of the Get API for Login page and paste the same under the Http sampler of Login page in JMeter. The link in blue show the complete URL including all the parameters , that will be send by the browser to the webserver to access the login page.
Looking at the URL of GET API, it can be easily identified that whatever is being specified after the question mark (?) symbol are the parameters passed to the API call and whatever before the question mark (?) symbol is the actual URL of the API.
Also we can get the parameters of the API from the Params tab, as shown above.
Add a Response Assertion by right clicking on Sampler (Login Page) -> Add -> Assertions -> Response Assertion for verifying whether login page was open successfully or not.
Add assertions as shown below :-
- Add a listener by right clicking on Sampler (Login Page) -> Add -> Listener -> View Results Tree
- Run the test case and verify the Request and Response in the tree view listener
- Now for submission of Login form, add one more HTTP Request sampler under the same thread group.
We know that, for submission of any form to the webserver, the web-browser will invoke the POST request with specified paramaters.
To get the parameters needed by the POST request, use the same method of firebug's Net plugin , as we used above to get the parameters for GET request.
Login to the website in firefox browser and get the API query parameters from Params tab and API POST Reqeust parameters from Post tab as shown below:-
Params tab specifying query parameters of POST request for login :-
Post tab specifying post parameters of POST request for login :-
- Set the method of Http Request to POST and specify Path as URL API and the parameters as shown below :-
- Add a new Response Assertion under the Http request sampler of Login form submission and add the assertion criteria as below
Add a new View Tree Listener under the under the Http request sampler of Login form submission and run the test plan.
But, when we run the program and verify the response, we found that its a failure
So, the response data clearly shows that the login was not successful and it can be due to disabled cookies.
- So, how to solve this problem. Just add a new config element HTTP Cookie Manager under the thread group Login Threads and again run the test plan.
- Run the test again after making the changes. You will see that the test failed with an error message that there seems to be a problem with your login session.
This clearly shows that there is a problem with the session that was established with webserver.
In the previous step of POST request, we hard-coded the parameter wpLoginToken, but in actual scenrio this session variable is sent by the webserver when we load the Login Page. So, we need to create this parameter value dynamically at run time.
To get the value of parameter wpLoginToken, add a new Post Processor – Regular Expression Extractor under sampler LoginPage
Populate following values in fields of Regular Expression Extractor :-
- Reference Name : loginToken
- Regular Expression : value="(.+?)" name="wpLoginToken"
- Template : $1$
- Match no : 1
- Default Value : NOT_FOUND
Note :- The special characters above in Regular Expression are:
( and ) - these enclose the portion of the match string to be returned . - match any character + - one or more times ? - stop when first match succeeds
We can validate the regular expression by using the Regular Expression Search of Tree View of Login Page.
Make sure that Regular Exp checkbox is checked.
- Now update the parameter wpLoginToken with the variable loginToken.
- Now run the test plan again and validate that login was successful to the website.
So, using the above steps, we were successfully able to login to a website for a single user workflow. In the next blogs, I will cover, how we can get multiple user's data from a csv file and perform the login workflow for multiple user's.