Selenium2+python Automation 10-login Case

Source: Internet
Author: User

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 Forum homepage: "http://www.hordehome.com/"

3. You can set the element wait before finding the element: implicitlywait ()

4. Click the login button to pop up the login box

5. Enter the user name, password, and then click Login

# Coding:utf-8
from Selenium Import Webdriver
Driver = Webdriver. Firefox ()
Driver.get ("http://www.hordehome.com")
# Set the implicit retry wait for 10 seconds
driver.implicitly_wait (ten)
# Click the login button, enter your account password and log in
driver.find_element_by_id ("ember886"). Click ()
driver.find_element_by_id ("login-account-name"). Send_keys ("YOYO")
driver.find_element_by_id ("login-account-password"). Send_keys ("xxxooo")
driver.find_element_by_css_selector (". btn.btn-large.btn-primary"). Click ()

second, The inspection results

1. After the login is completed, you need to check whether the login is successful, here you need to have a checkpoint, I choose to view the login profile picture properties

2. Navigate to the login avatar and get to the title property of the object via the Get_attribute () 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

# Gets the Title property value of the login avatar
t = driver.find_element_by_id ("current-user"). get_attribute ("title")
# Determine the return result
If t = = u "personal pages, messages, bookmarks and settings":
Print ("login Successful")
Else
Print ("login Failed")

(note: the site has been updated here, learn the idea on the line, do not copy)

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
driver.find_element_by_id ("current-user"). Click ()
driver.find_element_by_id ("ember1097"). 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
def Login (user,password):
Driver.get ("http://www.hordehome.com")
driver.implicitly_wait (ten)
driver.find_element_by_id ("ember886"). Click ()
driver.find_element_by_id ("login-account-name"). Send_keys (user)
driver.find_element_by_id ("login-account-password"). Send_keys (password)
driver.find_element_by_css_selector (". btn.btn-large.btn-primary"). Click ()
return driver
def logout ():
driver.find_element_by_id ("current-user"). Click ()
driver.find_element_by_id ("ember1097"). Click ()
driver.quit ()

V. Running Use Cases

1. Call the login function first

2. Check the login results

3. Log out and close the browser

if __name__ = = "__main__":
Driver = Webdriver. Firefox ()
Driver = login ("YOYO", "xxxooo")
t = driver.find_element_by_id ("current-user"). get_attribute ("title")
if T = = "personal pages, messages, bookmarks, and Settings":
Print ("login Successful")
else:
Print ("login Failed")
Logout ()

The above is a script reference to the Tribal forum www.hordehome.com, and the small partners can take the forum as a test project in Practice.

Selenium2+python Automation 10-login Case

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.