Selenium2+python automation 10-login case

foreword    

The first few articles are about some basic positioning methods. There are no specific cases. The friends seem to be boring. Many friends give suggestions to the editor and then add some specific cases. This article is to use the tribal forum as a test project to write a simple login test script. 

When writing a login script, you must first ensure that the process can run, and then find a way to optimize the code so that your script looks more comfortable and has good readability.

1. Login

    1. Open the browser first

    2. Open the github login page: https://github.com/login

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

    4. Enter the username and password, and then click Login

Reference Code:

# 打开github首页
driver.get("https://github.com/login")
driver.implicitly_wait(10)
# 输入账号
driver.find_element_by_id("login_field").send_keys("youruser")
# 输入密码
driver.find_element_by_id("password").send_keys("yourpsw")
driver.find_element_by_name("commit").click()

 

2. Inspection results

    1. After the login is completed, you need to check whether the login is successful. There needs to be a checkpoint here. I choose the account name in the upper right corner. Is it the account I just logged in to?

    2. First locate the settings in the upper right corner, and obtain the text attribute of this element through the .text method

    3. Determine whether the obtained value is consistent with the expected result

    4. Meet the expected results and pass the test

    5. Does not meet the expected results test failed

Reference code:
# After successful login, get my account name
time.sleep(5)
# Click on the upper right corner to 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 my account name: %s" % t)

if t == "yoyoketang":
    print("Login successful!")
else:
    print("Login failed!")

 

3. Log out

    1. After the test, don't forget to log out at the end

    2. After logging out, close the browser


# 点sign out退出登录
driver.find_element_by_css_selector(".dropdown-item.dropdown-signout").click()
driver.quit()

 
4. Login function

    1. Although the above code can achieve login, the entire code is the same as keeping a running account, and it is not readable. If I want to log in with another account, I have to find the login account and password location at this time, which is time-consuming.

    2. We can write two functions for login and logout, which looks more comfortable.

    3. Parameterize the login account and password

# coding:utf-8
from selenium import webdriver
import time

def login(driver, user, password):
    '''登录github'''
    # 打开github首页
    driver.get("https://github.com/login")
    driver.implicitly_wait(10)
    # 输入账号
    driver.find_element_by_id("login_field").send_keys(user)
    # 输入密码
    driver.find_element_by_id("password").send_keys(password)
    driver.find_element_by_name("commit").click()

def logout(driver):
    '''退出github'''
    time.sleep(3)
    # 点右上角设置
    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()
    # 调用登录
    login(driver, "youruser", "yourpsw")
    print("hello  yoyo!")
    # 调用退出
    logout(driver)

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325342952&siteId=291194637