封装模块文件public_1.py

#coding=utf-8
from selenium import webdriver
from time import sleep
class Login():
        
    #登陆
    def user_login(self,driver,username,password):
        driver.find_element_by_id("loginform-username").clear()
        driver.find_element_by_id("loginform-username").send_keys(username)
        driver.find_element_by_id("loginform-password").clear()
        driver.find_element_by_id("loginform-password").send_keys(password)
        driver.find_element_by_name("login-button").submit()
    #退出
    def user_logout(self,driver):
         driver.find_element_by_link_text("[退出]").click()
         sleep(5)
         driver.quit()

测试文件:test22.py

from selenium import webdriver
from time import sleep
from public_1 import Login
 
class LoginTest():
    #初始化函数:__init__(self)
    def __init__(self):
        self.driver = webdriver.Chrome()
        sleep(2)
        #目的网址:51CTO.com
        url = "http://home.51cto.com/index?reback=http://9603083.blog.51cto.com/9593083/d-2"
        self.driver.get(url)

    def test_admin_login(self):
        username = 'admin'
        password = '123'
        Login().user_login(self.driver,username,password)
        self.driver.quit()

    def test_guest_login(self):
        username = '91ctt'
        password = 'CTT1106648034'
        Login().user_login(self.driver,username,password)
        sleep(5)
        self.driver.quit()

LoginTest().test_guest_login()