“This is the 13th day of my participation in the First Challenge 2022. For details: First Challenge 2022”
preface
In the process of UI automatic test, login is the first step of the test. Generally, websites can be logged in by user name and password. You only need to open the test site, locate the user name and password input box, and log in. If the website login this for the sake of safety, opens the MFA verification, such as extra binding mobile phone, or through a third party software to generate the verification code to log in and so on, we are going to get the UI third-party authentication code, to implement more difficult struggle, still have a plenty of through the SSO login, if not in order to verify the login function strictly, but in order to verify the other functions, We can bypass the login part.
How to bypass the login section, there are many ways, you can tell the developer to give you a fixed verification code, or to let him turn off the MFA, or you in the test code, add a certain delay, in this time manual input verification code, etc., although the above can be solved, but not perfect method. Now the most commonly used is that we get cookie, through the cookie login, can understand the access to our login state.
Gets the cookie of the test site
non-sso
The website is not logged in through SSO. We log in manually. After successful login, refresh the page, find the cookie, copy it and save the cookie.
sso
For example, if you log in to your test website through a Mocker, first of all, the Mocker also needs login and verification. Then you log in to the Mocker manually first, get the cookie to save, and then log in through the mocker cookie, and then jump to your test website. Obtain the cookie of the test site through the code, and then log in to the subsequent test through the cookie of the obtained test site
Selenium in Python
The obtained cookie is saved in the form of dictionary {‘name’:key, ‘value’:value}. If there are multiple values in the cookie, it can be written in the form of dictionary list
cookies = [{'name':key1, 'value':value1},
{'name':key2, 'value':value2},
{'name':key3, 'value':value3},
...]
Copy the code
Selenium then calls selenium’s cookie add cookie method. This example involves privacy, and the cookie itself is written blind as follows:
from selenium import webdriver
def main() :
print("write code here")
driver = webdriver.Firefox()
login_url = ""
cookie_dict = [
{'name': 'WEB_ID'.'value': '1c267560'},
{'name': 'token'.'value': 'fac97060cbd99fe927'},
{'name': 'sessionid'.'value': '2a8497fe047d597a8be66dbb'}
]
driver.get(login_url)
for i in cookie_dict:
driver.add_cookie(i)
driver.refresh()
Copy the code
The RF writing
Userlogin [Arguments] ${mocker_cookie_dict} ${test_website_url} [Documentation] OpenBroswer ${test_website_url} firefox FOR ${KEY} ${VALUE} IN &{mocker_cookie_dict} add cookie ${KEY} ${VALUE} END go to ${test_website_url}Copy the code