惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

J
Java Code Geeks
博客园 - 聂微东
人人都是产品经理
人人都是产品经理
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
博客园_首页
量子位
阮一峰的网络日志
阮一峰的网络日志
酷 壳 – CoolShell
酷 壳 – CoolShell
H
Hackread – Cybersecurity News, Data Breaches, AI and More
云风的 BLOG
云风的 BLOG
D
DataBreaches.Net
B
Blog
L
LangChain Blog
Apple Machine Learning Research
Apple Machine Learning Research
Vercel News
Vercel News
博客园 - 三生石上(FineUI控件)
爱范儿
爱范儿
Microsoft Azure Blog
Microsoft Azure Blog
IT之家
IT之家
aimingoo的专栏
aimingoo的专栏
B
Blog RSS Feed
H
Help Net Security
The Cloudflare Blog
U
Unit 42

博客园 - Chen.HJ

python用正则对字符串进行运算 Nginx安装和遇到过的坑 Redis安装与常用命令 Nginx、Tomcat和Redis配置文件说明 python + selenium 获取标签文本的为空解决办法 Python读取Excel Python使用paramiko的SFTP get或put整个目录 Linux系统性能监控工具nmon python+selenium+unittest测试框架4-邮件发送最新测试报告 python+selenium+unittest测试框架3-项目构建和发送邮件 python+selenium+unittest测试框架2-装饰器@classmethod python+selenium+unittest测试框架1-unittest单元测试框架和断言 python+selenium个人学习笔记10-调用JavaScript和截图 python+selenium个人学习笔记9-文件上传和cookie操作 python+selenium个人学习笔记8-获取信息和勾选框 python+selenium个人学习笔记7-警告框处理和下拉框选择 python+selenium个人学习笔记6-元素等待 python+selenium个人学习笔记5-多窗口和多表单切换 python+selenium个人学习笔记4-鼠标和键盘操作
python+selenium个人学习笔记11-登录封装与调用
Chen.HJ · 2018-03-19 · via 博客园 - Chen.HJ

登录封装与调用

一、登录封装

把登录写成一个类,里面写登录的方法,保存文件为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("http://wwww.abc.com")
    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__":
    unittest.main()