Saturday, November 21, 2020

Login API with Jmeter and Python Request:

 Login API with Jmeter and Python Request:

Scenario:

  • To record a login API transaction using Jmeter
  • Run the same using Jmeter and check the response
  • Reproduce the same using Jmeter

Pre-Req:

  • FireFox Browser
  • Jmeter
  • Python 3.7+
  • pip install requests

Settings:

Configure Port numbers

  1. Inorder for Jmeter to capture recording , we need to place Jmeter in-between Browser and external connections 
  2. such that all data passes through Jmeter before reaching the Browser and vis versa. 
  3. This is done by changing the port number of Browser to use port number of Jmeter. So that Jmeter can listen to the interactions

Browser -----------> JMETER -------------> External Connections

  • open Jmeter 
  • File > templates > Select Recording >Create
  • In the folder structure > copy the port name
  • Open firefox > Preferences> 
  • Scroll to bottom > Network Settings >Click on Settings
  • Select Manul proxy config 
  • Http proxy = localhost and port = Paste the port number
  • click on OK

Installing Certificates :

Since configuration such as above is quiet dangerous if it is done by any malicious / unauthorised application . We need to install certificates from Jmeter into the Browser .

  1. In Jmeter , click on Https test Script Recorder
  2. Click on Start button
  3. You will get a message box saying a root CA certificate is created under "Jmeter bin Directory"
  4. Open FireFox preferences > Privacy and Security
  5. View Certificates
  6. Click on import 
  7. Select the certificate file created under "Jmeter/bin" 
  8. Click on Ok
  9. Close Preferences
  10. stop the recording in Jmeter

Steps to record Capture in Jmeter:

  1. Start recording in Jmeter
  2. Open FireFox 
  3. open link https://login.shrm.org/
  4. Enter email -abc@gmail.com , password = 123
  5. click on Sign in
  6. Stop recording in Jmeter
  7. Under Jmeter open Thread Group> Recording Controller and Notice the recording
  8. under Jmeter open Https Test Script Recorder > View Results Tree and Check the response as well
  9. Toggle between Http , Raw Tabs and  Sample Result , Request and Response Tabs to understand the Request method and how data is send and received
  10. Close the browser
  11. Now you can run the same for different username and password by changing values in Recording Controller > Http Header manager 

Steps to Reproduce in Python

  1. Open Terminal
  2. $python3

import requests
url="https://www.login.shrm.org/"
headers={'Content-Type': 'application/json'}
data={'LoginCredentials.Username': 'deep@xx.com', 'LoginCredential.Password': '123'}
r= requests.post(url,headers=headers,data=data)
print(r.status_code)
print(r.content)


No comments:

Post a Comment