unittest_测试web自动化
from selenium import webdriver
import unittest
from time import sleep
class TestSearch(unittest.TestCase):
@classmethod #基于类级别只开启一次浏览器,执行当前目录下所有用例
def setUpClass(cls):
cls.dr = webdriver.Chrome()
cls.base_url = 'https://www.baidu.com'
@classmethod
def tearDownClass(cls):
cls.dr.quit()
"""
def setUp(self):
self.dr = webdriver.Chrome()
self.base_url = 'https://www.baidu.com'
def tearDown(self):
self.dr.quit()
"""
def baidu_search(self,searchkeys):
dr = self.dr
dr.get(self.base_url)
dr.find_element_by_id("kw").send_keys(searchkeys)
dr.find_element_by_id("su").click()
sleep(3)
def test_selenium(self):
self.baidu_search("selenium")
self.assertEqual(self.dr.title,"selenium_百度搜索")
def test_python(self):
self.baidu_search("python")
self.assertEqual(self.dr.title,"python_百度搜索")
if __name__ == "__main__":
unittest.main()