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

推荐订阅源

J
Java Code Geeks
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
有赞技术团队
有赞技术团队
博客园 - 【当耐特】
云风的 BLOG
云风的 BLOG
Martin Fowler
Martin Fowler
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
量子位
Engineering at Meta
Engineering at Meta
博客园 - 叶小钗
T
The Blog of Author Tim Ferriss
Recent Announcements
Recent Announcements
罗磊的独立博客
B
Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
U
Unit 42
Microsoft Azure Blog
Microsoft Azure Blog
D
Docker
N
Netflix TechBlog - Medium
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
B
Blog RSS Feed
I
InfoQ
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
V
V2EX

博客园 - Chen.HJ

python用正则对字符串进行运算 Nginx安装和遇到过的坑 Redis安装与常用命令 Nginx、Tomcat和Redis配置文件说明 python + selenium 获取标签文本的为空解决办法 Python读取Excel Python使用paramiko的SFTP get或put整个目录 Linux系统性能监控工具nmon python+selenium个人学习笔记11-登录封装与调用 python+selenium+unittest测试框架4-邮件发送最新测试报告 python+selenium+unittest测试框架3-项目构建和发送邮件 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+unittest测试框架2-装饰器@classmethod
Chen.HJ · 2018-03-14 · via 博客园 - Chen.HJ

装饰器@classmethod

一、装饰器@classmethod

多个用例可能需要多次打开浏览器,装饰器@classmethod只打开一次浏览器。classmethod是python里的类方法,@是修饰符号。

1、setUpClass():

    @classmethod
    def setUpClass(cls):

2、tearDownClass():

    @classmethod
    def tearDownClass(cls):

示例:

from selenium import webdriver
from time import sleep
import unittest

class Login(unittest.TestCase):

    @classmethod
    def setUpClass(cls):
        cls.driver = webdriver.Chrome()
        cls.driver.get("http://www.anenda.com")
        cls.driver.implicitly_wait(30)
        cls.driver.maximize_window()
        sleep(2)
        cls.driver.find_element_by_id("liger-textbox-user").send_keys("chen")
        cls.driver.find_element_by_id("liger-textbox-pwd_old").clear()
        cls.driver.find_element_by_id("liger-textbox-pwd").send_keys("chen")
        cls.driver.find_element_by_id("go").click()
        sleep(3)

    def test01(self):
        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)

    def test02(self):
        result1 = self.driver.find_element_by_id("hour").text
        print(result1)
        result2 = "2018年"
        self.assertIn(result2,result1,msg="失败原因:%s中没有发现%s"%(result1,result2))
        sleep(2)

    @classmethod
    def tearDownClass(cls):
        cls.driver.quit()

if __name__ == '__main__':
    unittest.main()