python 通過xml獲取測試節(jié)點(diǎn)和屬性的實(shí)例
寫在前面:通過xml獲取測試數(shù)據(jù),主要是為了使數(shù)據(jù)參數(shù)化。測試腳本和測試數(shù)據(jù)分離,使得腳本清晰容易維護(hù),方便排查問題。
XML:可擴(kuò)展的標(biāo)記語言,是一種用于標(biāo)記電子文件使其具有結(jié)構(gòu)行的標(biāo)記語言。
自動化測試中的使用場景:
1. 經(jīng)常變動的測試數(shù)據(jù);
2. 數(shù)據(jù)量大,不方便放在腳本中;
3. 數(shù)據(jù)作用于多個地方;
4. 相同測試用例,可以使用不同的數(shù)據(jù);
5. 例:不穩(wěn)定,后續(xù)改動較多功能;容易出錯的功能
XML特征
• 具有自我描述性,本身不做任何事情
• 聲明部分 <?xml version="1.0" encoding="UTF-8"?>
• 有開就有閉,成對出現(xiàn) <data></data>
• 可以有屬性值 <data type='demo'></data>
• xml用于傳輸數(shù)據(jù) <data type='demo'> message </data>
• 可以嵌入子標(biāo)簽 <data type='demo'><text>message</text></data>
python獲取xml文件方法集合
引入模塊處理xml文件
from xml.dom.minidom import parse
打開xml文檔,
DOMTree = xml.dom.minidom.parse(data_path)
根據(jù)xml文檔,得到文檔元素的對象
data = DOMTree.documentElement
獲取節(jié)點(diǎn)列表
nodelist = data.getElementsByTagName(大類名稱)
獲取第一個節(jié)點(diǎn)的子節(jié)點(diǎn)列表
childlist = nodelist[0].childNodes
獲取XML節(jié)點(diǎn)屬性值
node.getAttribute(AttributeName)
獲取XML節(jié)點(diǎn)對象集合
node.getElementsByTagName(TagName)
返回子節(jié)點(diǎn)列表
node.childNodes
獲取XML節(jié)點(diǎn)值
node.childNodes[index].nodeValue
訪問第一個節(jié)點(diǎn)
node.firstChild ,等價(jià)于pagexml.childNodes[0]
訪問元素屬性 例如:
Node.attributes["id"] a.name #就是上面的 "id" a.value #屬性的值
以下為具體的demo內(nèi)容:
方法調(diào)用
#方法調(diào)用
text = get_data_vaule('account','type','createText','text')
print text
#結(jié)果
test data
xml文件
<?xml version="1.0" encoding="UTF-8"?>
<data>
<account type='createText'>
<text>test data</text>
</account>
<account type='email'>
<username>11111@qq.com</username>
<password>11111111</password>
</account>
</data>
下面方法相對應(yīng)取數(shù)據(jù)的格式,# style = xml中的大類 ; typename = 細(xì)分屬性; typevalue = 細(xì)分屬性的值; valuename = xml文件,需要獲取的值的tag;
如果需要獲取相對應(yīng)的上面XML文件中
“test data”的值,那么style = data ; typename = type; typevalue = createText; valuename = text “11111@qq.com”的值,那么style = data ; typename = type; typevalue = email; valuename = username
方法文件
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import unittest
import os
import time
from xml.dom.minidom import parse
import xml.dom.minidom
#獲取xml文件地址
path = os.path.abspath('.')
data_path = os.path.join(path,'features/data/data.xml') #獲取xml文件地址
DOMTree = xml.dom.minidom.parse(data_path)
data = DOMTree.documentElement
def get_attrvalue(node, attrname):
return node.getAttribute(attrname)
# style = xml中的大類 ; typename = 細(xì)分屬性; typevalue = 細(xì)分屬性的值; valuename = xml文件,需要獲取的值的tag;
def get_data_vaule(style, typename, typevalue, valuename):
nodelist = data.getElementsByTagName(style)
for node in nodelist:
if typevalue == node.getAttribute(typename):
node_name = node.getElementsByTagName(valuename)
value = node_name[0].childNodes[0].nodeValue
print value
return value
return
以上這篇python 通過xml獲取測試節(jié)點(diǎn)和屬性的實(shí)例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
- Python實(shí)現(xiàn)針對給定單鏈表刪除指定節(jié)點(diǎn)的方法
- Python基于lxml模塊解析html獲取頁面內(nèi)所有葉子節(jié)點(diǎn)xpath路徑功能示例
- python+matplotlib繪制簡單的海豚(頂點(diǎn)和節(jié)點(diǎn)的操作)
- Python算法之求n個節(jié)點(diǎn)不同二叉樹個數(shù)
- python xml.etree.ElementTree遍歷xml所有節(jié)點(diǎn)實(shí)例詳解
- Python selenium 父子、兄弟、相鄰節(jié)點(diǎn)定位方式詳解
- Python獲取任意xml節(jié)點(diǎn)值的方法
- python實(shí)現(xiàn)單鏈表中刪除倒數(shù)第K個節(jié)點(diǎn)的方法
相關(guān)文章
Python實(shí)現(xiàn)自動化處理Word文檔的方法詳解
本文主要介紹了如何使用Python實(shí)現(xiàn)Word文檔的自動化處理,包括批量生成Word文檔、在Word文檔中批量進(jìn)行查找和替換、將Word文檔批量轉(zhuǎn)換成PDF等,希望對你有所幫助2022-08-08
Python 獲取ftp服務(wù)器文件時(shí)間的方法
今天小編就為大家分享一篇Python 獲取ftp服務(wù)器文件時(shí)間的方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-07-07
python錯誤:AttributeError: ''module'' object has no attribute
這篇文章主要介紹了python錯誤:AttributeError: 'module' object has no attribute 'setdefaultencoding'問題的解決方法,需要的朋友可以參考下2014-08-08
10個Python常用的損失函數(shù)及代碼實(shí)現(xiàn)分享
損失函數(shù)是一種衡量模型與數(shù)據(jù)吻合程度的算法。損失函數(shù)測量實(shí)際測量值和預(yù)測值之間差距的一種方式。本文為大家總結(jié)了10個常用的損失函數(shù)及Python代碼實(shí)現(xiàn),需要的可以參考一下2022-09-09
python實(shí)現(xiàn)sm2和sm4國密(國家商用密碼)算法的示例
這篇文章主要介紹了python實(shí)現(xiàn)sm2和sm4國密(國家商用密碼)算法的示例,幫助大家使用python加密文件,感興趣的朋友可以了解下2020-09-09

