• selenium3+python3学习——构建测试套件


    1、按顺序执行

    from selenium import webdriver
    import unittest
    import time
    
    class BaiduTest(unittest.TestCase):
        @classmethod
        def setUpClass(cls):
            cls.driver = webdriver.Chrome()
            cls.driver.maximize_window()
            url = 'http://www.baidu.com'
            cls.driver.get(url)
            cls.driver.implicitly_wait(30)
        @classmethod
        def tearDownClass(cls):
            cls.driver.quit()
        def test_baidu_news(self):
           self.driver.find_element_by_link_text("新闻").click()
           url = self.driver.current_url
           self.assertEqual(url,'http://news.baidu.com/')
        def test_baidu_map(self):
            self.driver.find_element_by_link_text("地图").click()
            self.driver.get("http://www.baidu.com")
    if __name__ =='__main__':
        suite = unittest.TestSuite()  # TestSuite类实例化
        suite.addTest(BaiduTest('test_baidu_map')) # 调用TestSuite类中addTest方法,将测试用例添加到测试套件中
        suite.addTest(BaiduTest('test_baidu_news'))
        unittest.TextTestRunner().run(suite)

    2、按测试类执行

    from selenium import webdriver
    import unittest
    import time
    
    
    class BaiduTest(unittest.TestCase):
        @classmethod
        def setUpClass(cls):
            cls.driver = webdriver.Chrome()
            cls.driver.maximize_window()
            url = 'http://www.baidu.com'
            cls.driver.get(url)
            cls.driver.implicitly_wait(30)
        @classmethod
        def tearDownClass(cls):
            cls.driver.quit()
    
        def test_baidu_news(self):
           self.driver.find_element_by_link_text("新闻").click()
           url = self.driver.current_url
           self.assertEqual(url,'http://news.baidu.com/')
    
        def test_baidu_map(self):
            self.driver.find_element_by_link_text("地图").click()
            self.driver.get("http://www.baidu.com")
    
        def test_baidu_feiyan(self):
            self.driver.find_element_by_link_text("抗击肺炎").click()
            self.driver.get("http://www.baidu.com")
    
    
    if __name__ =='__main__':
        # suite = unittest.TestSuite()
        # suite.addTest(BaiduTest('test_baidu_map'))
        # suite.addTest(BaiduTest('test_baidu_news'))
        # unittest.TextTestRunner().run(suite)
        suite = unittest.TestSuite(unittest.makeSuite(BaiduTest))    # 调用了makeSuite
        unittest.TextTestRunner().run(suite)

    3、加载测试类

    from selenium import webdriver
    import unittest
    import time
    
    
    class BaiduTest(unittest.TestCase):
        @classmethod
        def setUpClass(cls):
            cls.driver = webdriver.Chrome()
            cls.driver.maximize_window()
            url = 'http://www.baidu.com'
            cls.driver.get(url)
            cls.driver.implicitly_wait(30)
        @classmethod
        def tearDownClass(cls):
            cls.driver.quit()
    
        def test_baidu_news(self):
           self.driver.find_element_by_link_text("新闻").click()
           url = self.driver.current_url
           self.assertEqual(url,'http://news.baidu.com/')
    
        def test_baidu_map(self):
            self.driver.find_element_by_link_text("地图").click()
            self.driver.get("http://www.baidu.com")
    
        def test_baidu_feiyan(self):
            self.driver.find_element_by_link_text("抗击肺炎").click()
            self.driver.get("http://www.baidu.com")
    
    
    if __name__ =='__main__':
        # suite = unittest.TestSuite()
        # suite.addTest(BaiduTest('test_baidu_map'))
        # suite.addTest(BaiduTest('test_baidu_news'))
        # unittest.TextTestRunner().run(suite)
        # suite = unittest.TestSuite(unittest.makeSuite(BaiduTest))
        suite = unittest.TestLoader().loadTestsFromTestCase(BaiduTest)       # 使用TestLoader类来加载测试类
        unittest.TextTestRunner().run(suite)

    4、按测试模块执行

    from selenium import webdriver
    import unittest
    import time
    
    
    class BaiduTest(unittest.TestCase):
        @classmethod
        def setUpClass(cls):
            cls.driver = webdriver.Chrome()
            cls.driver.maximize_window()
            url = 'http://www.baidu.com'
            cls.driver.get(url)
            cls.driver.implicitly_wait(30)
        @classmethod
        def tearDownClass(cls):
            cls.driver.quit()
    
        def test_baidu_news(self):
           self.driver.find_element_by_link_text("新闻").click()
           url = self.driver.current_url
           self.assertEqual(url,'http://news.baidu.com/')
    
        def test_baidu_feiyan(self):
            self.driver.find_element_by_link_text("抗击肺炎").click()
            self.driver.get("http://www.baidu.com")
    
    
    class BaiduMap(unittest.TestCase):
        @classmethod
        def setUpClass(cls):
            cls.driver = webdriver.Chrome()
            cls.driver.maximize_window()
            url = 'http://www.baidu.com'
            cls.driver.get(url)
            cls.driver.implicitly_wait(30)
        @classmethod
        def tearDownClass(cls):
            cls.driver.quit()
    
        def test_baidu_map(self):
            self.driver.find_element_by_link_text("地图").click()
            self.driver.get("http://www.baidu.com")
    
    
    if __name__ =='__main__':
        # suite = unittest.TestSuite()
        # suite.addTest(BaiduTest('test_baidu_map'))
        # suite.addTest(BaiduTest('test_baidu_news'))
        # unittest.TextTestRunner().run(suite)
        # suite = unittest.TestSuite(unittest.makeSuite(BaiduTest))
        # suite = unittest.TestLoader().loadTestsFromTestCase(BaiduTest)       # 使用TestLoader类来加载测试类
        suite = unittest.TestLoader().loadTestsFromModule('dd.py')            # 两个测试类在同一个模块下
        unittest.TextTestRunner().run(suite)

    5、优化测试套件(将测试套件写成一个方法来调用)

    from selenium import webdriver
    import unittest
    
    
    
    class BaiduTest(unittest.TestCase):
        @classmethod
        def setUpClass(cls):
            cls.driver = webdriver.Chrome()
            cls.driver.maximize_window()
            url = 'http://www.baidu.com'
            cls.driver.get(url)
            cls.driver.implicitly_wait(30)
        @classmethod
        def tearDownClass(cls):
            cls.driver.quit()
    
        def test_baidu_news(self):
           self.driver.find_element_by_link_text("新闻").click()
           url = self.driver.current_url
           self.assertEqual(url,'http://news.baidu.com/')
    
        def test_baidu_feiyan(self):
            self.driver.find_element_by_link_text("抗击肺炎").click()
            self.driver.get("http://www.baidu.com")
    
        @staticmethod
        def suite(testCaseClass):
            suite = unittest.TestLoader().loadTestsFromTestCase(testCaseClass)
            return suite
    
    
    if __name__ == '__main__':
        unittest.TextTestRunner().run(BaiduTest.suite(BaiduTest))
  • 相关阅读:
    cc.Component
    cc.Node—坐标系统
    cc.Node—事件响应
    cc.Node—Action
    cc.Node—场景树
    console.log格式化及console对象
    网站开发学习Python实现-Django项目部署-介绍(6.2.1)
    个人博客搭建Python实现-尝试-遇到的问题(10.1.1)
    工具-Redis-django存储session(99.6.4)
    工具-Redis-与Python一起使用(99.6.3)
  • 原文地址:https://www.cnblogs.com/yuer02/p/12661503.html
Copyright © 2020-2023  润新知