Objective
The previous several are talking about some basic positioning methods, no specific cases, small partners look more boring, there are a lot of small partners to make suggestions for a small number of more specific cases. This article is to take the tribal forum as a test project, write a simple login test script.
When writing login scripts, make sure the process runs, and then try to optimize the code to make your scripts look more comfortable and have good readability.
First, Login
1. Open the browser first
2. Open the GitHub login page: Https://github.com/login
3. You can set the element wait before finding the element: Implicitlywait ()
4. Enter the user name, password, and then click Login
Reference code:
# Open GitHub Homepage
Driver.get ("Https://github.com/login")
Driver.implicitly_wait (10)
# Enter your account
driver.find_element_by_id ("Login_field"). Send_keys ("Youruser")
# Enter password
driver.find_element_by_id ("password"). Send_keys ("YOURPSW")
Driver.find_element_by_name ("Commit"). Click ()
Second, the inspection results
1. After the login is completed, you need to check whether the login is successful, here need to have a checkpoint, I choose the upper right side of the account name is not I just logged in this account
2. Navigate to the upper right corner setting and get the Text property of this element through the. Text method
3. Determine whether the obtained value is consistent with the expected result
4. Test pass with expected results
5. Test does not meet the expected results
Reference code:
# After successful login, get my account name
Time.sleep (5)
# point to the upper right corner of the set
Driver.find_element_by_css_selector (". Headernavlink.name.mt-1 "). Click ()
# Get account Name
Time.sleep (1)
t = Driver.find_element_by_css_selector (". Dropdown-header.header-nav-current-user.css-truncate>. Css-truncate-target "). Text
Print ("Get to my account name:%s"% t)
If T = = "Yoyoketang":
Print ("Login successful! ")
Else
Print ("Login failed! ")
Third, sign out
1. After the test, don't forget to log out at the end
2. After exiting the login, close the browser
# Sign Out Login
Driver.find_element_by_css_selector (". Dropdown-item.dropdown-signout"). Click ()
Driver.quit ()
Four, login function
1. Although the code above can be logged in, the entire code is no more readable than the journal. If I want to change account login, this time also have to find the login account and password location, relatively time-consuming.
2. We can write two functions for login and exit, so it looks more comfortable.
3. parameterize the login account and password
# Coding:utf-8
From selenium import Webdriver
Import time
def login (driver, user, password):
"Login to GitHub"
# Open GitHub Homepage
Driver.get ("Https://github.com/login")
Driver.implicitly_wait (10)
# Enter your account
driver.find_element_by_id ("Login_field"). Send_keys (user)
# Enter password
driver.find_element_by_id ("password"). Send_keys (password)
Driver.find_element_by_name ("Commit"). Click ()
def logout (Driver):
"Exit GitHub"
Time.sleep (3)
# point to the upper right corner of the set
Driver.find_element_by_css_selector (". Headernavlink.name.mt-1 "). Click ()
Time.sleep (1)
# Sign Out
Driver.find_element_by_css_selector (". Dropdown-item.dropdown-signout"). Click ()
Driver.quit ()
if __name__ = = "__main__":
Driver = Webdriver. Firefox ()
# Call Login
Login (Driver, "Youruser", "YOURPSW")
Print ("Hello yoyo!")
# Call to Exit
Logout (Driver)
Selenium2+python Automation 10-Login Case