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
- Inorder for Jmeter to capture recording , we need to place Jmeter in-between Browser and external connections
- such that all data passes through Jmeter before reaching the Browser and vis versa.
- 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 .
- In Jmeter , click on Https test Script Recorder
- Click on Start button
- You will get a message box saying a root CA certificate is created under "Jmeter bin Directory"
- Open FireFox preferences > Privacy and Security
- View Certificates
- Click on import
- Select the certificate file created under "Jmeter/bin"
- Click on Ok
- Close Preferences
- stop the recording in Jmeter
Steps to record Capture in Jmeter:
- Start recording in Jmeter
- Open FireFox
- open link https://login.shrm.org/
- Enter email -abc@gmail.com , password = 123
- click on Sign in
- Stop recording in Jmeter
- Under Jmeter open Thread Group> Recording Controller and Notice the recording
- under Jmeter open Https Test Script Recorder > View Results Tree and Check the response as well
- Toggle between Http , Raw Tabs and Sample Result , Request and Response Tabs to understand the Request method and how data is send and received
- Close the browser
- 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
- Open Terminal
- $python3
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