修改html版本,HTMLTestRunner修改Python3的版本

在拜读虫师大神的Selenium2+Python2.7时,发现生成HTMLTestRunner的测试报告使用的HTMLTestRunner的模块是用的Python2的语法。而我本人比较习惯与Python3。而且自己也是用的Python3.4的环境,在网上找了很多资料,修改了下HTMLTestRunner.py

修改汇总:第94行    import StringIO

修改成    import io

第539行    self.outputBuffer = StringIO.StringIO()

修改成     self.outputBuffer= io.StringIO()

第642行    if not rmap.has_key(cls):

修改成     if notcls 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, '\nTimeElapsed: %s' % (self.stopTime-self.startTime))

详细修改过程如下图:

在Python3.4下使用HTMLTestRunner,开始时,引入HTMLTestRunner模块报错。

1ec706b1759f79a61f24a23a959e934e.png

在HTMLTestRunner的94行中,是使用的StringIO,但是Python3中,已经没有StringIO了。取而代之的是io.StringIO。所以将此行修改成import io

956375df68fb147cdd7286ca55e0c4bd.png

在HTMLTestRunner的539行中self.outputBuffer =StringIO.StringIO()

修改成self.outputBuffer = io.StringIO()

54030675d96e5ada9cf42ef99f4f133d.png

修改以后,成功引入模块了

213eea71d5de5d50d71ead6f9b9a7711.png

执行测试脚本:#引入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行修改代码:

5219eebfee03a416ba411864fbccf5b8.png

运行后继续报错:

AttributeError: 'str' object has noattribute 'decode'

前往766,772行继续修改(注意:766行是uo而772行是ue,当时眼瞎,没有注意到这些,以为是一样的,导致报了一些莫名其妙的错误,折腾的半天):

19b5b5e3a998f2ddae0f4fd798e6719c.png

修改后运行,发现又报错: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, '\nTimeElapsed: %s' % (self.stopTime-self.startTime

这个是2.x的写法,咱们修改成3.x的print,修改如下:print(sys.stderr, '\nTime Elapsed: %s' %(self.stopTime-self.startTime))

268daf24a2767168b070e9111df6b6f1.png

继续运行脚本,OK运行成功

34fb1fafead25ca7c0a2ab9197eae0b2.png

查看指定的目录生成了result.html

fa1c89ac08bf95888ef001f5dce35ded.png

点击打开报告:

520fc915490be1d69d758c66e29eeddf.png

1.将 HTMLTestRunner.py 放置在 C:\Python36\Lib 下 2.涉及到创建目录和时间,需要在脚本开头 import os import time 3.执行脚本中删除语句 unittest.main() ,一般在脚本最后,然后添加如下语句: #导入HTMLTestRunner库,这句也可以放在脚本开头 from HTMLTestRunner import HTMLTestRunner #定义脚本标题,加u为了防止中文乱码 report_title = u'登陆模块测试报告' #定义脚本内容,加u为了防止中文乱码 desc = u'手机JPG登陆模块测试报告详情:' #定义date为日期,time为时间 date=time.strftime("%Y%m%d") time=time.strftime("%Y%m%d%H%M%S") #定义path为文件路径,目录级别,可根据实际情况自定义修改 path= 'D:/Python_test/'+ date +"/login/"+time+"/" #定义报告文件路径和名字,路径为前面定义的path,名字为report(可自定义),格式为.html report_path = path+"report.html" #判断是否定义的路径目录存在,不能存在则创建 if not os.path.exists(path): os.makedirs(path) else: pass #定义一个测试容器 testsuite = unittest.TestSuite() #将测试用例添加到容器 testsuite.addTest(测试类名("测试方法名1")) testsuite.addTest(测试类名("测试方法名2")) #将运行结果保存到report,名字为定义的路径和文件名,运行脚本 with open(report_path, 'wb') as report: runner = HTMLTestRunner(stream=report, title=report_title, description=desc) runner.run(testsuite) #关闭report,脚本结束 report.close()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值