• selenium+python笔记3


    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    """
    @desc:学习unittest的用法
    注意setUp/setUpClass,tearDown/tearDownClass的区别
    ① setUp():每个测试函数运行前运行
    ② tearDown():每个测试函数运行完后执行
    ③ setUpClass():必须使用@classmethod 装饰器,所有test运行前运行一次
    ④ tearDownClass():必须使用@classmethod装饰器,所有test运行完后运行一次
    
    unittest还有一些不常用的装饰器:
    @unittest.skip(reason):无条件跳过测试,reason描述为什么跳过测试
    @unittest.skipif(conditition,reason):condititon为true时跳过测试
    @unittest.skipunless(condition,reason):condition不是true时跳过测试
    @unittest.expectedFailure:如果test失败了,这个test不计入失败的case数目
    """
    import unittest
    import time
    from selenium import webdriver
    
    
    class SearchTest(unittest.TestCase):
        @classmethod
        def setUpClass(cls):
            cls.driver = webdriver.Firefox()
            cls.driver.implicitly_wait(30)
            cls.driver.maximize_window()
            cls.base_url = "http://www.baidu.com"
            cls.driver.get(cls.base_url + "/")
    
            cls.search_text = cls.driver.find_element_by_id("kw")
            cls.search_btn = cls.driver.find_element_by_id("su")
    
        def test_search_btn_displayed(self):
            self.assertTrue(self.search_btn.is_displayed())
            self.assertTrue(self.search_btn.is_enabled())
    
        def test_search_text_maxlength(self):
            max_length = self.search_text.get_attribute("maxlength")
            self.assertEqual("255", max_length)
    
        def test_search(self):
            self.search_text.clear()
            self.search_text.send_keys("unittest")
            self.search_btn.click()
    
            time.sleep(2)
            title = self.driver.title
            self.assertEqual(title, u"unittest_百度搜索")
    
        @classmethod
        def tearDownClass(cls):
            # close the browser window
            cls.driver.quit()
    
    
    if __name__ == '__main__':
        unittest.main(verbosity=3)
  • 相关阅读:
    <<信息学奥赛一本通>> 昆虫养殖 题解
    C++ primer 第七章 练习7.35 类作用域练习
    C++ primer 第六章 练习6.56 函数指针练习
    P1145 约瑟夫
    C++ 查看auto delctype 后变量的类型。(小技巧)
    Educational Codeforces Round 24 题解
    HDU 5279 分治NTT 图的计数
    BZOJ 3473
    BZOJ 3514 LCT+主席树
    看无可看 分治FFT+特征值方程
  • 原文地址:https://www.cnblogs.com/kuihua/p/5507723.html
Copyright © 2020-2023  润新知