I believe that some children in the process of doing interface, or interface automation testing will encounter such a scenario: test interface, must be the need to log in to launch a successful request.
So what’s the solution? In the spirit of teamwork, we will let the development students open a back door, give you a “universal” value, so you take this “universal” value in the request, you can be unimpeded.
But not all developers are willing to do this, and many testers have thin skin, which is not recommended. Then you can take care of your own business.
Since you need to log in to request it, let’s log in. How do I log in? Selenium library, and then selenium to get browser cookies. The following is a demo login operation to log in to the system under test.
The from the selenium import webdriver driver = webdriver. Chrome () driver. Get # open the login address (" http://xxx.xx.com/xxx/login ") Driver.find_element_by_id ("password").send_keys(" XXXX ") Driver.find_element_by_xpath ("//button[.= 'logon ']").click() # logon cookie = driver.get_cookies( print(cookie)Copy the code
Let’s see kangkang did kangkang get the cookie or not?
[
{
'domain': 'xxxx.xx.xxxx.com',
'httpOnly': True,
'name': 'SESSION',
'path': '/xxx/',
'secure': False,
'value': 'YjRjYzY1ODMtNjA1ZS00OWZmLWFmODQtZjhmY2ZjMmIxNzlj'
}
]
Copy the code
Look at browser F12, find the cookie, check it out
OK, this is the value, so you can happily test the interface with this value.
Interface automation can use pyTest unit test framework + Requests library, simple and crude, sei use SEI channel. Cuihua, go to demo
import requests
import pytest
HEADER = {
'Cookie': "SESSION=YjRjYzY1ODMtNjA1ZS00OWZmLWFmODQtZjhmY2ZjMmIxNzlj"
}
def test_01():
r = requests.get("http://xxxx/finance/detail" ,headers=HEADER)
result = r.json()
print(result)
if __name__ == '__main__':
pytest.main(['-s', 'demo1.py'])
Copy the code
Take a look at the printed request result:
Of course, interface automation tests do not forget to add assertions oh, demo will not write.
In short, live learning and use.