Selenium是当前主流的Web自动化测试工具。几乎到了统治地位,它也是无数其他浏览器自动化工具、API和框架中的核心技术。

    1. import unittest
    2. from time import sleep
    3. from selenium import webdriver
    4. class MyTest(unittest.TestCase):
    5. @classmethod
    6. def setUpClass(cls):
    7. cls.dr = webdriver.Chrome()
    8. cls.base_url = "https://www.baidu.com"
    9. @classmethod
    10. def tearDownClass(cls):
    11. cls.dr.close()
    12. # 这是普通的方法
    13. def search_keyword(self, keyword):
    14. self.dr.find_element_by_name("wd").send_keys(keyword)
    15. self.dr.find_element_by_id("su").click()
    16. sleep(2)
    17. def test_baidu_search_selenium(self):
    18. dr = self.dr
    19. dr.get(self.base_url)
    20. self.search_keyword("selenium")
    21. self.assertEqual(dr.title, "selenium_百度搜索")
    22. def test_baidu_search_unittest(self):
    23. dr = self.dr
    24. dr.get(self.base_url)
    25. self.search_keyword("unittest")
    26. self.assertEqual(dr.title, "unittest_百度搜索")
    27. if __name__ == '__main__':
    28. unittest.main()