数据驱动xml驱动的方式
存数据的xml文件:
TestData.xml:
<?xml version="1.0" encoding="utf-8"?>
<bookList type="technology">
<book>
<name>Selenium WebDriver实战宝典</name>
<author>吴晓华</author>
</book>
<book>
<name>HTTP权威指南</name>
<author>古尔利</author>
</book>
<book>
<name>探索式软件测试</name>
<author>惠特克</author>
</book>
</bookList>
XmlUtil.py:#用来读取xml的数据
#encoding=utf-8
from xml.etree import ElementTree
'''
步骤:
1 读取xml --__init__()
2 获取root节点--getroot()
3 获取root节点下的book所有节点 ---findNodeByName
4 将book节点下的所有信息放到dict里面---findNodeByName
5 将所有book节点的dict放到datalist,来做数据驱动————getDataFromXml
'''
class ParseXML(object):
def __init__(self, xmlPath):
self.xmlPath = xmlPath
def getRoot(self):
# 打开将要解析的xml文件
tree = ElementTree.parse(self.xmlPath)
# 获取xml文件的根节点对象,也就是树的根
# 然后返回给调用者
print "tree.getroot:",tree.getroot()
return tree.getroot()#获取根节点
def findNodeByName(self, parentNode, nodeName):#子孙节点都可以找到
# 通过节点的名字,获取节点对象
nodes = parentNode.findall(nodeName)
print "nodes:",nodes
return nodes#返回一个list
def getNodeOfChildText(self, node):
# 获取节点node下所有子节点的节点名作为key,
# 文本节点value作为value组成的字典对象,若从0开始,则包含父节点的标签,例如book
#childrenTextDict = {i.tag: i.text for i in list(node.iter())[1:]}#把0节点排除,就是父节点本身,不要
#print"childrenTextDict:", childrenTextDict
# 上面代码等价于下面代码
childrenTextDict = {}
print " node.iter():",list(node.iter())#node.iter()-包含自己及子孙节点的迭代器,转成list后,每个元素是一个节点信息
for i in list(node.iter())[1:]:#排除父节点本身-book,剩下name和author
childrenTextDict[i.tag] = i.text
print" childrenTextDict:", childrenTextDict
return childrenTextDict#返回一个字典
def getDataFromXml(self):
# 获取xml文档树的根节点对象
root = self.getRoot()
# 获取根节点下所有名叫book的节点对象
books = self.findNodeByName(root, "book")
dataList = []
# 遍历获取到的所有book节点对象,
# 取得需要的测试数据
for book in books:
childrenText = self.getNodeOfChildText(book)
dataList.append(childrenText)
print "dataList:",dataList
return dataList
if __name__ == '__main__':
xml = ParseXML(r"D:\\test\\0629\\TestData.xml")
datas = xml.getDataFromXml()
for i in datas:
print ' i["name"], i["author"]:',i["name"], i["author"]
单独运行结果:
d:\test\0629>python XmlUtil.py
tree.getroot: <Element 'bookList' at 0x591e5b0>
nodes: [<Element 'book' at 0x591e770>, <Element 'book' at 0x591e790>, <Element 'book' at 0x591e910>]
node.iter(): [<Element 'book' at 0x591e770>, <Element 'name' at 0x591e7d0>, <Element 'author' at 0x591e850>]
childrenTextDict: {'name': u'Selenium WebDriver\u5b9e\u6218\u5b9d\u5178'}
childrenTextDict: {'name': u'Selenium WebDriver\u5b9e\u6218\u5b9d\u5178', 'author': u'\u5434\u6653\u534e'}
node.iter(): [<Element 'book' at 0x591e790>, <Element 'name' at 0x591e8d0>, <Element 'author' at 0x591e8f0>]
childrenTextDict: {'name': u'HTTP\u6743\u5a01\u6307\u5357'}
childrenTextDict: {'name': u'HTTP\u6743\u5a01\u6307\u5357', 'author': u'\u53e4\u5c14\u5229'}
node.iter(): [<Element 'book' at 0x591e910>, <Element 'name' at 0x591e9b0>, <Element 'author' at 0x591e9d0>]
childrenTextDict: {'name': u'\u63a2\u7d22\u5f0f\u8f6f\u4ef6\u6d4b\u8bd5'}
childrenTextDict: {'name': u'\u63a2\u7d22\u5f0f\u8f6f\u4ef6\u6d4b\u8bd5', 'author': u'\u60e0\u7279\u514b'}
dataList: [{'name': u'Selenium WebDriver\u5b9e\u6218\u5b9d\u5178', 'author': u'\u5434\u6653\u534e'}, {'name': u'HTTP\u6743\u5a01\u6307\u5357', 'author': u'\u53e4\u5c14\u5229'}, {'name': u'\u63a2\u7d22\u5f0f\u8f6f\u4ef6\u6d4b\u8bd5', 'author': u'\u60e0\u7279\u514b'}]
i["name"], i["author"]: Selenium WebDriver实战宝典 吴晓华
i["name"], i["author"]: HTTP权威指南 古尔利
i["name"], i["author"]: 探索式软件测试 惠特克
修改后代替childrenTextDict = {i.tag: i.text for i in list(node.iter())[1:]}#:
#encoding=utf-8
from xml.etree import ElementTree
'''
步骤:
1 读取xml --__init__()
2 获取root节点--getroot()
3 获取root节点下的book所有节点 ---findNodeByName
4 将book节点下的所有信息放到dict里面---findNodeByName
5 将所有book节点的dict放到datalist,来做数据驱动————getDataFromXml
'''
class ParseXML(object):
def __init__(self, xmlPath):
self.xmlPath = xmlPath
def getRoot(self):
# 打开将要解析的xml文件
tree = ElementTree.parse(self.xmlPath)
# 获取xml文件的根节点对象,也就是树的根
# 然后返回给调用者
print "tree.getroot:",tree.getroot()
return tree.getroot()#获取根节点
def findNodeByName(self, parentNode, nodeName):#子孙节点都可以找到
# 通过节点的名字,获取节点对象
nodes = parentNode.findall(nodeName)
print "nodes:",nodes
return nodes#返回一个list
def getNodeOfChildText(self, node):
# 获取节点node下所有子节点的节点名作为key,
# 文本节点value作为value组成的字典对象,若从0开始,则包含父节点的标签,例如book
childrenTextDict = {i.tag: i.text for i in list(node.iter())[1:]}#把0节点排除,就是父节点本身,不要
print" childrenTextDict:", childrenTextDict
return childrenTextDict#返回一个字典
#上面代码等价于下面代码
'''
childrenTextDict = {}
print " node.iter():",list(node.iter())#node.iter()-包含自己及子孙节点的迭代器,转成list后,每个元素是一个节点信息
for i in list(node.iter())[1:]:#排除父节点本身-book,剩下name和author
childrenTextDict[i.tag] = i.text
print" childrenTextDict:", childrenTextDict
return childrenTextDict#返回一个字典,每次返回包含一个name,一个author的字典
'''
def getDataFromXml(self):
# 获取xml文档树的根节点对象
root = self.getRoot()
# 获取根节点下所有名叫book的节点对象
books = self.findNodeByName(root, "book")
dataList = []
# 遍历获取到的所有book节点对象,
# 取得需要的测试数据
for book in books:
childrenText = self.getNodeOfChildText(book)
dataList.append(childrenText)
print "dataList:",dataList
return dataList
if __name__ == '__main__':
xml = ParseXML(r"D:\\test\\0629\\TestData.xml")
datas = xml.getDataFromXml()
for i in datas:
print ' i["name"], i["author"]:',i["name"], i["author"]
单独运行结果:
d:\test\0629>python XmlUtil.py
tree.getroot: <Element 'bookList' at 0x532e630>
nodes: [<Element 'book' at 0x532e7f0>, <Element 'book' at 0x532e810>, <Element 'book' at 0x532e990>]
childrenTextDict: {'name': u'Selenium WebDriver\u5b9e\u6218\u5b9d\u5178', 'author': u'\u5434\u6653\u534e'}
childrenTextDict: {'name': u'HTTP\u6743\u5a01\u6307\u5357', 'author': u'\u53e4\u5c14\u5229'}
childrenTextDict: {'name': u'\u63a2\u7d22\u5f0f\u8f6f\u4ef6\u6d4b\u8bd5', 'author': u'\u60e0\u7279\u514b'}
dataList: [{'name': u'Selenium WebDriver\u5b9e\u6218\u5b9d\u5178', 'author': u'\u5434\u6653\u534e'}, {'name': u'HTTP\u6743\u5a01\u6307\u5357', 'author': u'\u53e4\u5c14\u5229'}, {'name': u'\u63a2\u7d22\u5f0f\u8f6f\u4ef6\u6d4b\u8bd5', 'author': u'\u60e0\u7279\u514b'}]
i["name"], i["author"]: Selenium WebDriver实战宝典 吴晓华
i["name"], i["author"]: HTTP权威指南 古尔利
i["name"], i["author"]: 探索式软件测试 惠特克
data_drivern_by_xml.py:脚本文件
# encoding=utf-8
from selenium import webdriver
import unittest, time
import logging, traceback
import ddt
from XmlUtil import ParseXML
from selenium.common.exceptions import NoSuchElementException
# 初始化日志对象
logging.basicConfig(
# 日志级别
level = logging.INFO,
# 日志格式
# 时间、代码所在文件名、代码行号、日志级别名字、日志信息
format = '%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',
# 打印日志的时间
datefmt = '%a, %Y-%m-%d %H:%M:%S',
# 日志文件存放的目录(目录必须存在)及日志文件名
filename = 'd:/dataDriveRreport.log',
# 打开日志文件的方式
filemode = 'a'
)
# 创建ParseXML类实例对象
xml = ParseXML(ur"d:\test\TestData.xml")
@ddt.ddt
class TestDemo(unittest.TestCase):
def setUp(self):
self.driver=webdriver.Firefox(executable_path="c:\\geckodriver")
@ddt.data(*xml.getDataFromXml())
def test_dataDrivenByFile(self, data):
testData, expectData = data["name"], data["author"]
url = "http://www.baidu.com"
# 访问百度首页
self.driver.get(url)
# 将浏览器窗口最大化
self.driver.maximize_window()
print testData, expectData
# 设置隐式等待时间为10秒
self.driver.implicitly_wait(10)
try:
# 找到搜索输入框,并输入测试数据
self.driver.find_element_by_id("kw").send_keys(testData)
# 找到搜索按钮,并点击
self.driver.find_element_by_id("su").click()
time.sleep(3)
# 断言期望结果是否出现在页面源代码中
self.assertTrue(expectData in self.driver.page_source)
except NoSuchElementException, e:
logging.error(u"查找的页面元素不存在,异常堆栈信息:"\
+ str(traceback.format_exc()))
except AssertionError, e:
logging.info(u"搜索“%s”,期望“%s”,失败" %(testData, expectData))
except Exception, e:
logging.error(u"未知错误,错误信息:" + str(traceback.format_exc()))
else:
logging.info(u"搜索“%s”,期望“%s”通过" %(testData, expectData))
def tearDown(self):
self.driver.quit()
if __name__ == '__main__':
unittest.main()
结果:
d:\test\0629>python test.py
tree.getroot: <Element 'bookList' at 0x5e771b0>
nodes: [<Element 'book' at 0x5e77370>, <Element 'book' at 0x5e774b0>, <Element 'book' at 0x5e77590>]
childrenTextDict: {'name': u'Selenium WebDriver\u5b9e\u6218\u5b9d\u5178', 'author': u'\u5434\u6653\u534e'}
childrenTextDict: {'name': u'HTTP\u6743\u5a01\u6307\u5357', 'author': u'\u53e4\u5c14\u5229'}
childrenTextDict: {'name': u'\u63a2\u7d22\u5f0f\u8f6f\u4ef6\u6d4b\u8bd5', 'author': u'\u60e0\u7279\u514b'}
dataList: [{'name': u'Selenium WebDriver\u5b9e\u6218\u5b9d\u5178', 'author': u'\u5434\u6653\u534e'}, {'name': u'HTTP\u6743\u5a01\u6307\u5357', 'author': u'\u53e4\u5c14\u5229'}, {'name': u'\u63a2\u7d22\u5f0f\u8f6f\u4ef6\u6d4b\u8bd5', 'author': u'\u60e0\u7279\u514b'}]
Selenium WebDriver实战宝典 吴晓华
.HTTP权威指南 古尔利
.探索式软件测试 惠特克
.
----------------------------------------------------------------------
Ran 3 tests in 52.651s
OK
dataDriveReport.log:
Fri, 2018-06-29 22:08:20 test.py[line:61] INFO 搜索“Selenium WebDriver实战宝典”,期望“吴晓华”通过
Fri, 2018-06-29 22:08:38 test.py[line:61] INFO 搜索“HTTP权威指南”,期望“古尔利”通过
Fri, 2018-06-29 22:08:54 test.py[line:61] INFO 搜索“探索式软件测试”,期望“惠特克”通过