简易Selenium自动化测试框架(Python)

发表于:2016-6-14 11:07

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:徐文    来源:51Testing软件测试网采编

  用例执行模块
  1. 借助外部文件记录需要执行的用例
  testcases.txt(带“#”标识的用例不会被执行):
  Test_Login.py
  Test_Login_2.py
  #Test_Login_3.py
  Test_Login_4.py
  2. 利用nose的nosetests命令执行各个用例:
  import subprocess
class RunTests(object):
"""description of class"""
def __init__(self):
self.testcaselistfile = "testcases.txt"
#use nosetests command to execute test case list
def LoadAndRunTestCases(self):
f = open(self.testcaselistfile)
testfiles = [test for test in f.readlines() if not test.startswith("#")]
f.close()
for item in testfiles:
subprocess.call("nosetests "+str(item).replace("\\n",""),shell = True)
if __name__ == "__main__":
newrun = RunTests()
newrun.LoadAndRunTestCases()
  测试结果报表生成模块
  测试报表模块写了两种格式:xml和html
  TestReport.py
from xml.etree import ElementTree as ET
import os
import lxml.etree as mytree
from lxml import html
class TestReport(object):
"""description of class"""
def __init__(self):
self.testreport = "TestResult.xml"
#If there is no "TestResult.xml", then create one
def CreateTestResultFile(self):
if os.path.exists(self.testreport) == False:
newElem = ET.Element("TestCases")
newTree = ET.ElementTree(newElem)
newTree.write(self.testreport)
#Write test result to xml
def WriteResult(self,testcaseInfo):
self.CreateTestResultFile()
testResultFile = ET.parse(self.testreport)
root = testResultFile.getroot()
newElem = ET.Element("TestCase")
newElem.attrib = {
"ID":testcaseInfo.id,
"Name":testcaseInfo.name,
"Owner":testcaseInfo.owner,
"Result":testcaseInfo.result,
"StartTime":testcaseInfo.starttime,
"EndTime":testcaseInfo.endtime,
"ErrorInfo":testcaseInfo.errorinfo
}
root.append(newElem)
testResultFile.write(self.testreport)
#If there is no "TestResult.html" file exists, then create one with default style
def CreateHtmlFile(self):
if os.path.exists("TestResult.html") == False:
f = open("TestResult.html",'w')
message = """<html>
<head>
<title>Automation Test Result</title>
<style>
table {
border-collapse: collapse;
padding: 15px;
font-family: "Trebuchet MS", Arial, Helvetica, sans-serif;
}
th{
background-color: green;
color: white;
border: 1px solid #ddd;
padding-bottom: 15px;
padding-top: 15px;
}
tr{
border: 1px solid #008000;
padding-bottom: 8px;
padding-top: 8px;
text-align: left;
}
td{
border: 1px solid #008000;
}
</style>
</head>
<body>
<h1>Automation Test Result</h1>
<table>
<tr>
<th>ID</th>
<th>Name</th>
<th>Owner</th>
<th>Result</th>
<th>StartTime</th>
<th>EndTime</th>
<th>ErrorMessage</th>
</tr>
</table>
</body>
</html>
"""
f.write(message)
f.close()
#append new test result to testresult file
def WriteHTML(self,testcaseinfo):
self.CreateHtmlFile()
f = open("TestResult.html","r")
htmlcontent = f.read()
f.close()
tree = html.fromstring(htmlcontent)
tableElem = tree.find(".//table")
if testcaseinfo.result == "Failed":
mytablerow = "<tr><td>{0}</td><td>{1}</td><td>{2}</td><td bgcolor=\"#FF0000\">{3}</td><td>{4}</td><td>{5}</td><td>{6}</td></tr>".format(testcaseinfo.id,testcaseinfo.name,testcaseinfo.owner,testcaseinfo.result,testcaseinfo.starttime,testcaseinfo.endtime,testcaseinfo.errorinfo)
else:
mytablerow = "<tr><td>{0}</td><td>{1}</td><td>{2}</td><td>{3}</td><td>{4}</td><td>{5}</td><td>{6}</td></tr>".format(testcaseinfo.id,testcaseinfo.name,testcaseinfo.owner,testcaseinfo.result,testcaseinfo.starttime,testcaseinfo.endtime,testcaseinfo.errorinfo)
tableElem.append(mytree.HTML(str(mytablerow)))
f = open("TestResult.html","w")
#html.tostring
newContent = repr(html.tostring(tree,method="html",with_tail=False))
newContent = newContent.replace(r"\n","").replace(r"\t","").replace('b\'',"")
newContent = newContent[:len(newContent)-1]
f.write(newContent)
f.close()
  ok,最后看一下生成的测试报表:
  总结
  在网上有很多关于Selenium自动化的Best Practice,当然大家也可以根据自己的需求来DIY自己的框架,不管简陋与否,好用才是硬道理:)!
22/2<12
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号