#coding=utf-8 import unittest import HTMLTestRunner import time def creatsuite(): testunit=unittest.TestSuite() #定義測試文件查找的目錄 test_dir='F:\\python\\selemium\\program' #定義 discover 方法的參數 discover=unittest.defaultTestLoader.discover(test_dir, pattern ='test_*.py', top_level_dir=None) #discover 方法篩選出來的用例,循環添加到測試套件中 for test_suite in discover: for test_case in test_suite: testunit.addTests(test_case) print testunit return testunit alltestnames = creatsuite() if __name__ == '__main__': now = time.strftime("%Y-%m-%d %H_%M_%S") filename = 'F:\\python\\selemium\\program\\'+now+'result.html' fp = file(filename,'wb') runner = HTMLTestRunner.HTMLTestRunner( stream = fp, title = u'測試報告', description = u'用例的執行狀況') runner.run(alltestnames) fp.close()
2.login.pycss
#coding=utf-8 from selenium import webdriver import time driver = webdriver.Firefox() driver.maximize_window() driver.implicitly_wait(10) driver.get("http://www.baidu.com") def login(): driver.find_element_by_xpath("//div/div/div[@id='u1']/a[7]").click() driver.find_element_by_css_selector("#TANGRAM__PSP_8__userName").clear() driver.find_element_by_css_selector("#TANGRAM__PSP_8__userName").send_keys("xxxx") driver.find_element_by_css_selector("#TANGRAM__PSP_8__password").send_keys("xxxxxxx") driver.find_element_by_css_selector("#TANGRAM__PSP_8__submit").click()
3.test_1.pyhtml
#coding=utf-8 from selenium import webdriver import unittest, time, login import random random1 = random.randint(100000, 200000) class MyTest(unittest.TestCase): u'''百度搜索用例''' def setUp(self): self.driver = login.driver #self.driver.maximize_window() #self.driver.implicitly_wait(10) #self.base_url = "http://www.baidu.com" def test_baidu(self): driver = self.driver login.login() time.sleep(2) #driver.get(self.base_url + "/") driver.find_element_by_id("kw").clear() driver.find_element_by_id("kw").send_keys(random1) driver.find_element_by_id("su").click() time.sleep(2) title = driver.title #self.assertEqual(title, random1+u"_百度搜索") def tearDown(self): self.driver.quit() if __name__ == "__main__": unittest.main()
最後的結果是這樣子的:python