import unittest from time import sleep from selenium import webdriver class TestBdai(unittest.TestCase): @classmethod#可减少浏览器关闭次数 def setUp(cls): cls.driver = webdriver.Chrome() cls.base_url = "https://baidu.com/" def baidu_search(self, search_key):#封装搜索功能 self.driver.get(self.base_url) self.driver.find_element_by_id("kw").send_keys(search_key) self.driver.find_element_by_id("su").click() sleep(2) def test_search_key_selenium(self): search_key = "selenium" self.baidu_search(search_key) self.assertEqual(self.driver.title, search_key+"_百度搜索") def test_search_key_unittest(self): search_key = "unittest" self.baidu_search(search_key) self.assertEqual(self.driver.title, search_key+"_百度搜索") @classmethod def tearDown(cls): cls.driver.quit() if __name__=="__main__": unittest.main()
注意事项:1、Unittest查找和执行测试用例时,只会把以“test”开头的方法当做测试用例。
2、把断言写在每一条测试用例里。
163邮箱登录功能测试:
import unittest from selenium import webdriver from time import sleep class Test_login(unittest.TestCase): @classmethod def setUpClass(cls): cls.driver = webdriver.Chrome() cls.base_url = "https://mail.163.com/" def login(self,username,password): self.driver.get(self.base_url) iframe = self.driver.find_element_by_css_selector("iframe") self.driver.switch_to.frame(iframe) self.driver.find_element_by_css_selector("[name = 'email']").clear() self.driver.find_element_by_css_selector("[name = 'email']").send_keys(username) self.driver.find_element_by_css_selector("[name = 'password']").clear() self.driver.find_element_by_css_selector("[name = 'password']").send_keys(password) self.driver.find_element_by_id("dologin").click() sleep(2) def test_success_login(self): username = "####" password = "$$$$" self.login(username,password) self.driver.switch_to.default_content() text = self.driver.find_element_by_id("spnUid").text self.assertIn(".com", text) def test_failed_login(self): username = "123" password = "123" self.login(username,password) text = self.driver.find_element_by_class_name("ferrorhead").text self.assertIn("错误", text) def test_null_login(self): username = "" password = "" self.login(username,password) text = self.driver.find_element_by_class_name("ferrorhead").text self.assertIn("输入", text) @classmethod def tearDownClass(cls): cls.driver.quit() if __name__=="__main__": unittest.main()
浏览器前进、后退、截屏功能测试:
from selenium import webdriver from time import sleep import unittest class brower_control(unittest.TestCase): def setUp(self): self.driver = webdriver.Chrome() self.first_url = "https://www.baidu.com/" self.driver.get(self.first_url) sleep(2) self.second_url = "http://news.baidu.com" self.driver.get(self.second_url) sleep(2) def test_back(self): self.setUp self.driver.back() print(self.driver.current_url) self.assertIn("baidu",self.driver.current_url)
self.driver.get_screenshot_as_file("E:\test\test1.png") def test_forward(self): self.test_back self.driver.forward() print(self.driver.current_url) self.assertIn("news",self.driver.current_url)
self.driver.get_screenshot_as_file("E:\test\test2.png")
def tearDown(self):
self.driver.quit()
if __name__=="__main__": unittest.main()
百度首页>设置>高级搜索>时间一栏下拉框验证
from selenium import webdriver from time import sleep import unittest class Alert_handle(unittest.TestCase): @classmethod def setUp(cls): cls.driver = webdriver.Chrome() cls.driver.get("https://www.baidu.com/") def test_aler(self): self.driver.find_element_by_id("s-usersetting-top").click() self.driver.find_element_by_link_text("高级搜索").click() sleep(2) self.driver.find_element_by_css_selector("#adv-setting-gpc > div > div.c-select-selection > i.c-icon.c-select-arrow").click() sleep(2) selection_list = self.driver.find_elements_by_css_selector("[data-for='gpc']") for i in selection_list: print(i.text) j = selection_list[1] self.assertIn("一天", j.text)
j.click()
sleep(2) @classmethod def tearDown(cls): cls.driver.quit() if __name__=="__main__": unittest.main()
多窗口切换验证:window_handles, current_window_handle, switch_to.window()
import unittest from time import sleep from selenium import webdriver class Window_handle(unittest.TestCase): @classmethod def setUp(cls): cls.driver = webdriver.Chrome() cls.base_url = "https://www.baidu.com/" def test_window_handle(self): self.driver.get(self.base_url) self.driver.maximize_window() self.driver.find_element_by_link_text("登录").click() sleep(2) self.driver.find_element_by_link_text("立即注册").click() #handles的类型是列表 handles = self.driver.window_handles print(handles) self.driver.switch_to.window(handles[-1]) print(self.driver.current_window_handle) sleep(2) text = self.driver.find_element_by_xpath("//*[@id='content']/div[3]/div[1]/h3").text self.assertEqual("欢迎注册",text) @classmethod def tearDown(cls): cls.driver.quit() if __name__=="__main__": unittest.main()
输出:
================================================== RESTART: C:UsersAdministratorAppDataLocalProgramsPythonPython38-32window_handle.py ================================================= ['CDwindow-91D6DE9C70B15E9D24870B4CD8DB1D6B', 'CDwindow-DEACE6DD9D38A30A1F3FEFA1BCEA5422'] CDwindow-DEACE6DD9D38A30A1F3FEFA1BCEA5422 . ---------------------------------------------------------------------- Ran 1 test in 12.609s OK