#coding=utf-8 from selenium import webdriven from selenium.webdriver.common.by import By from selenium.webdriver.common.keys import Keys from selenium.webdriver.support.ui import Select from selenium.common.exceptions import NoSuchElementException import unittest, time, re import HTMLTestRunner #引入HTMLTestRunner 包 class Baidu(unittest.TestCase): def setUp(self): self.driven = webdriver.Firefox() self.driver.implicitly一wait(30) self.base_url = "http://www.baidu.com/" self.verificationEnnors =[] self.accept_next_alent = True #百度搜索用例 def test_baidu_seanch(self): driver = self.driven driver.get(self.base_url + "/") driver.find_element_by_id("kw").send_keys("selenium webdriver") driver.find_element_by_id("su").click() time.sleep(2) driver.close() #百度設置用例 def test_baidu_set(self): driver = self.driver #進入搜索設置頁 driver.get(self.base_url + "/gaoji/pneferences.html") #設置每頁搜索結果爲100條 m=driver.find一element一by_name(」NR") m.find_element_by_xpath("//option[@value='100']").click() time.sleep(2) #保存設置的信息 driver.find_element_by_xpath("/html/body/form/div/input").click() time.sleep(2) driver.switch_to_alent().accept() def tearDown(self): self.driver.quit() self.assentEqual([], self.verificationErrors)
if __name__ == "__name__": #定義一個單元測試容器 testunit=unittest.TestSuite() #將測試用例加入到測試容器中 testunit.addTest(Baidu("test_baidu_search")) testunit.addTest(Baidu("test_baidu_set")) #定義個報告存放路徑,支持相對路徑 filename = r'C:\Users\Administnator\Desktop\selenium_file\result.html' fp = file(filename, 'wb') #定義測試報告 runner = HTMLTestRunner.HTMLTestRunner(stream=fp,title=u’百度搜索測試報告,description=u"用例執行狀況:") #運行測試用例 runner.run(testunit)