python+selenium-登录封装与调用

一、登录封装

把登录写成一个类,里面写登录的方法,保存文件为loginclass.py

class login_go():
    u'''登录类封装'''
    def __init__(self,driver):
        u'''初始化driver参数'''
        self.driver = driver

    def login(self,username,password):
        u'''输入用户名和密码,点击登录'''
        self.driver.find_element_by_id("liger-textbox-user").clear()
        self.driver.find_element_by_id("liger-textbox-user").send_keys(username)
        self.driver.find_element_by_id("liger-textbox-pwd_old").clear()
        self.driver.find_element_by_id("liger-textbox-pwd").clear()
        self.driver.find_element_by_id("liger-textbox-pwd").send_keys(password)
        self.driver.find_element_by_id("go").click()


二、调用

 
from selenium import webdriver
import unittest
from loginclass import login_go
from time import sleep
class Login(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Chrome()
        self.driver.implicitly_wait(10)
        self.driver.maximize_window()
        self.driver.get(login_url)
    def tearDown(self):
        self.driver.quit()
    def case_login(self):
        login_go(self.driver).login("chen","chen")#调用login方法
        sleep(3)
        result1 = self.driver.find_element_by_xpath(".//*[@id='l-topmenu-r-bottm']/span[2]").text
        print(result1)
        result2 = "欢迎您"
        self.assertIn(result2,result1,msg="失败原因:%s中没有发现%s"%(result1,result2))
        sleep(2)
if __name__ == "__main__":
    login_url = "http://www.abc.com"
    unittest.main()
posted @ 2019-03-12 16:27  jojo-jiao  阅读(3148)  评论(0编辑  收藏  举报