在拜讀蟲師大神的Selenium2+Python2.7時,發現生成HTMLTestRunner的測試報告使用的HTMLTestRunner的模塊是用的Python2的語法。而我本人比較習慣與Python3。而且自己也是用的Python3.4的環境,在網上找了很多資料,修改了下HTMLTestRunner.py
參考:http://bbs.chinaunix.net/thread-4154743-1-1.html
下載地址:http://tungwaiyip.info/software/HTMLTestRunner.html
修改后下載地址:http://pan.baidu.com/s/1tp3Ts
修改匯總:
第94行,將import StringIO修改成import io
第539行,將self.outputBuffer = StringIO.StringIO()修改成self.outputBuffer = io.StringIO()
第642行,將if not rmap.has_key(cls):修改成if not cls in rmap:
第766行,將uo = o.decode('latin-1')修改成uo = e
第775行,將ue = e.decode('latin-1')修改成ue = e
第631行,將print >> sys.stderr, '\nTime Elapsed: %s' % (self.stopTime-self.startTime)修改成print(sys.stderr, '\nTime Elapsed: %s' % (self.stopTime-self.startTime))
在Python3.4下使用HTMLTestRunner,開始時,引入HTMLTestRunner模塊報錯。
在HTMLTestRunner的94行中,是使用的StringIO,但是Python3中,已經沒有StringIO了。取而代之的是io.StringIO。所以將此行修改成import io
在HTMLTestRunner的539行中,self.outputBuffer = StringIO.StringIO()修改成self.outputBuffer = io.StringIO()
修改以后,成功引入模塊了
執行腳本代碼:
# -*- coding: utf-8 -*- #引入webdriver和unittest所需要的包 from selenium import webdriver 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 from selenium.common.exceptions import NoAlertPresentException import unittest, time, re #引入HTMLTestRunner包 import HTMLTestRunner class Baidu(unittest.TestCase): #初始化設置 def setUp(self): self.driver = webdriver.Firefox() self.driver.implicitly_wait(30) self.base_url = "http://www.baidu.com/" self.verificationErrors = [] self.accept_next_alert = True #百度搜索用例 def test_baidu(self): driver = self.driver driver.get(self.base_url) driver.find_element_by_id("kw").click() driver.find_element_by_id("kw").clear() driver.find_element_by_id("kw").send_keys("Selenium Webdriver") driver.find_element_by_id("su").click() time.sleep(2) driver.close() def tearDown(self): self.driver.quit() self.assertEqual([], self.verificationErrors) if __name__ == "__main__": #定義一個測試容器 test = unittest.TestSuite() #將測試用例,加入到測試容器中 test.addTest(Baidu("test_baidu")) #定義個報告存放的路徑,支持相對路徑 file_path = "F:\\RobotTest\\result.html" file_result= open(file_path, 'wb') #定義測試報告 runner = HTMLTestRunner.HTMLTestRunner(stream = file_result, title = u"百度搜索測試報告", description = u"用例執行情況") #運行測試用例 runner.run(test) file_result.close()
運行測試腳本后,發現報錯:
File "C:\Python34\lib\HTMLTestRunner.py", line 642, in sortResult
if not rmap.has_key(cls):
所以前往642行修改代碼:
運行后繼續報錯:
AttributeError: 'str' object has no attribute 'decode'
前往766, 772行繼續修改(注意:766行是uo而772行是ue,當時眼瞎,沒有注意到這些,以為是一樣的,導致報了一些莫名其妙的錯誤,折騰的半天):
修改后運行,發現又報錯:
File "C:\Python34\lib\HTMLTestRunner.py", line 631, in run
print >> sys.stderr, '\nTime Elapsed: %s' % (self.stopTime-self.startTime)
TypeError: unsupported operand type(s) for >>: 'builtin_function_or_method' and '_io.TextIOWrapper'
前往631查看,發現整個程序中,唯一一個print:
print >> sys.stderr, '\nTime Elapsed: %s' % (self.stopTime-self.startTime
這個是2.x的寫法,咱們修改成3.x的print,修改如下:
print(sys.stderr, '\nTime Elapsed: %s' % (self.stopTime-self.startTime))
繼續運行腳本,OK運行成功
查看指定的目錄生成了result.html
點擊打開報告: