Locust: Understanding cookie creation while using locust login

3k views Asked by At

I am trying to use locust for login to my web application. I am at very beginning of using locust.

I am using the following code for login to application.

post_data = {'username': username, 'password': password,'Submit':'Login' }
with self.client.post('/mylogin-url/', post_data,
                                  catch_response=True) as response:
     print response.code
     print response.content

This part always returns status code 200 but the login is not succesful, as the response content of source is not the same as the actual source after login

My web application creates cookies and redirect to an URL based on cookie after login. I am trying to understand whether the login operation does this cookie creation automatically from locust or do I need to add that part of cookie creation logic n the script itself.

Any help regarding this is greatly appreciated.

Thanks

2

There are 2 answers

0
Leo Lee On

You may need to look at this.

In your with block, you can parse the response and check if you get the correct response.

Suppose you should get cookie field in your response cookies if you login success, then you can do like this:

post_data = {'username': username, 'password': password,'Submit':'Login' }
with self.client.post('/mylogin-url/', post_data, \
    catch_response=True) as response:
    if 'cookie' not in respone.cookies:
        response.failure('login failed')
0
Shambu On

It seems that the response in the html content tells that user hasn't logged in. In that case you can check if response text contains failure message

post_data = {'username': username, 'password': password,'Submit':'Login' }
with self.client.post('/mylogin-url/', post_data,
                                  catch_response=True) as response:
     if response.text.contains("Login failed"):
       response.failure('login failed')