2

I am executing the load test by going through a login using Jmeter and unfortunately, I received a response forbidden code 403. While sending the login details a security token(named in the field _csrf) is also sent by the server that is a random token generated at the time of login due to that my test case not passed. Here below I am sending the response HTML data in which It has stored in the input value.

I am trying it to save into the variable using Regular Expression Extractor so that I can use that variable in the login request directly instead os hard coding invalid token(_csrf).

HTML DATA


<body>
<!-- Used for modals to show login screen once timed out -->
<script type="text/javascript">
    if (window.self != window.top) {
        window.parent.location.reload();
    }
</script>

<div id="main">
    <div id="innermain" class="standalone-page x-narrow">
        <div class=" container-fluid formcontainer">
            <h2>Login</h2>
            <form id="command" action="/login" method="POST"><fieldset>
                    <div class="form-row">
                        <div class="form-group col label-input">
                            <input name="username" id="username"/>
                            <label for="username">Username</label>
                        </div>
                    </div>
                    <div class="form-row">
                        <div class="form-group col label-input">
                            <input type="password" name="currentPassword" id="password"/>
                            <label for="password">Password</label>
                        </div>
                    </div>
                    <div class="form-row actions">
                        <div class="col">
                            <input type="submit" value="Login" class="mat-btn mat-btn-floating"/>
                        </div>
                    </div>
                </fieldset>
            <div>
<input type="hidden" name="_csrf" value="94sfdefa-6e8p-4a-a2-73720ffab5" />
</div></form></div>
    </div>
</div>
</body>
</html>

<input type="hidden" name="_csrf" value="94sfdefa-6e8p-4a-a2-73720ffab5" />

Look for the above line in the HTML Response Data.

My request data look like:

enter image description here

Can anyone please help me out of this it would be really appreciable ??

3

Using regular expressions for working with HTML data is not recommended, consider using CSS Selector Extractor

  1. Add CSS Selector Extractor as a child of the previous sampler which returns the login page
  2. Configure it as follows:

    • Name of created variable: anything meaningful, i.e. token
    • CSS Selector Expression: input[name=_csrf]
    • Attribute: value

      enter image description here

  3. In next POST request where you sending the credentials just use ${token} instead of the 94sfdefa-6e8p-4a-a2-73720ffab5 hard-coded value

    enter image description here

See How to Load Test CSRF-Protected Web Sites guide for more comprehensive explanation with examples.

Also don't forget to add HTTP Cookie Manager to your Test Plan, upon successful login most probably you will receive the cookie and you will need to have it for all subsequent requests

| improve this answer | |
  • OMG!! Thank you for this amazing solution. It worked now. Thanks a lot !! And is there any way to follow you for future references. You are just amazing. :) – Rajan Apr 2 at 9:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.