Performance Testing Using Jmeter Part 2

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

Add New HTTP Request Defaults

  • 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 .

Http Request for Login 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
      Http Request for Login Page
    • 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.

Http Request for Login Page

Add assertions as shown below :-

Login Page Assertion Conditions

  • Add a listener by right clicking on Sampler (Login Page) -> Add -> Listener -> View Results Tree

Add New Login Page Tree View Listener

  • Run the test case and verify the Request and Response in the tree view listener

Request :-
Login Page Tree View Listener - Verify Request

Response :-
Login Page Tree View Listener - Verify Response

  • 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 :-
Login Page Post Request Query parameters

Post tab specifying post parameters of POST request for login :-
Login Page Post Request POST parameters

  • Set the method of Http Request to POST and specify Path as URL API and the parameters as shown below :-

Login Submission Http Request Parameters

  • Add a new Response Assertion under the Http request sampler of Login form submission and add the assertion criteria as below

Login Submission Assertions

  • 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

Login Failure due to cookies

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.

New Cookie Manager

  • 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.

Login failure due to 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

Add new Regular Expression Extractor

  • 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

    Regular Expression field details

    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.

Login failure due to Session

Make sure that Regular Exp checkbox is checked.

  • Now update the parameter wpLoginToken with the variable loginToken.

Update parameter for Submit Login Post Request

  • 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.

Leave a Reply

Your email address will not be published. Required fields are marked *