本文主要是介绍python+selenium2学习笔记unittest-05测试用例实例,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
看一下非常简单的目录结构
test_baidu
from selenium import webdriver
import unittest
import timeclass MyTest(unittest.TestCase):def setUp(self):self.driver = webdriver.Firefox()self.driver.maximize_window()self.driver.implicitly_wait(10)self.driver.get("http://www.baidu.com")def test_baidu(self):driver = self.driverdriver.find_element_by_id("kw").clear()driver.find_element_by_id("kw").send_keys("selenium2")driver.find_element_by_id("su").click()time.sleep(2)title = driver.titleself.assertEqual(title,'selenium2_百度搜索')def tearDown(self):self.driver.quit()
if __name__ == '__main__':unittest.main()
test_youdao
from selenium import webdriver
import unittest
import timeclass MyTest(unittest.TestCase):def setUp(self):self.driver = webdriver.Firefox()self.driver.maximize_window()self.driver.implicitly_wait(10)self.driver.get("http://www.youdao.com")def test_youdao(self):driver = self.driverdriver.find_element_by_id("translateContent").clear()driver.find_element_by_id("translateContent").send_keys("selenium2")driver.find_element_by_id("translateContent").submit()time.sleep(2)title = driver.titleself.assertIn('selenium2',title)def tearDown(self):self.driver.quit()
if __name__ == '__main__':unittest.main()
最后看一下runtest文件
import unittest
test_dir = './test/test_case'
discover = unittest.defaultTestLoader.discover(test_dir,pattern='test_*.py')
if __name__=='__main__':runner = unittest.TextTestRunner()runner.run(discover)
两个点 运行成功
这篇关于python+selenium2学习笔记unittest-05测试用例实例的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!