当前位置:网站首页>Technology Sharing | How to do assertion verification for xml format in interface automation testing?
Technology Sharing | How to do assertion verification for xml format in interface automation testing?
2022-08-03 04:33:00 【Yehenara Hermione】
In the process of server-side automated testing, the response value needs to be verified after the request is initiated. After verifying that the response information conforms to the expected value, this interface automated test case is considered a complete pass.Therefore, in this chapter, we will explain how to perform assertion verification on the XML format response content returned by the server in the interface automation test.
Environment preparation
Python version
Install requests_xml
pip install requests_xml
Java version
Rest-Assured supports xml assertion, refer to the interface testing framework chapter to install Rest-Assured.
XML parsing
Python has three ways of parsing XML.
DOM mode: It is the document object model and a standard programming interface recommended by the W3C organization. It parses XML data into a tree in memory, and manipulates XML by manipulating the tree.
SAX mode: It is an event-driven model for processing XML. It scans the document line by line and parses it as it scans. It has huge advantages for parsing large documents. Although it is not a W3C standard, it getswidely recognized.
ElementTree method: Compared with DOM, it has better performance, similar to SAX performance, and the API is also very convenient to use.
Python version
request is not strong in encapsulating the XML format, you can use the request_xml third-party library, or you can encapsulate an XML parsing by yourself.
- XML Response Assertion
from requests_xml import XMLSession# set sessionsession = XMLSession()r = session.get("https://www.nasa.gov/rss/dyn/lg_image_of_the_day.rss")# print everythingr.text# links can get all the link addresses in the responser.xml.links# raw_xml returns the response content in bytesr.xml.raw_xml# text returns the content of the labelr.xml.text
- Assertions using xpath
requests_xml library also supports XPath expressions.You can retrieve the data of the corresponding field in the response through XPath, and put the retrieved data in the result list, which is convenient for use case assertion.
XPath usage:
def xpath(self, selector: str, *, first: bool = False, _encoding: str = None) -> _XPath:"""Given an XPath selector, returns a list of:class:`Element ` objects or a single one.:param selector: XPath Selector to use.:param first: Whether or not to return just the first result.:param _encoding: The encoding format."""
selector: XPath expression used
first: whether to return only the first search result
Thexpath() method returns a list of found objects.
def test_xpath():session = XMLSession()r = session.get("https://www.nasa.gov/rss/dyn/lg_image_of_the_day.rss")# Get the content of all link tags through xpathitem = r.xml.xpath("//link")result = []for i in item:# put the obtained results into a listresult.append(i.text)# assertassert 'http://www.nasa.gov/' in result
- XML parsing
XML is a structured, hierarchical data format, and the most suitable data structure for XML is a tree.The XML structure can be parsed using the xml.etree.ElementTree that comes with python.ElementTree can convert the entire XML document into a tree, and the interaction (reading, writing, finding elements) of the entire XML document is generally carried out at the ElementTree level.
Then use the findall method to find the required XPath data.
import xml.etree.ElementTree as ET# Encapsulate the xml parsing method yourselfsession = XMLSession()r = session.get("https://www.nasa.gov/rss/dyn/lg_image_of_the_day.rss")# Get the response contentroot = ET.fromstring(r.text)# find root elementem = root.findall(".")# print(item)items = root.findall(".//link")result = []# traversefor i in items:result.append(i.text)assert "http://www.nasa.gov/" in result
Java version
Call the body() method, the first passing in the XPath expression and the second passing the desired result.
import static io.restassured.RestAssured.*;import static org.hamcrest.core.IsEqual.equalTo;public class Requests {public static void main(String[] args) {given().contentType("application/rss+xml; charset=utf-8").when().get("https://www.nasa.gov/rss/dyn/lg_image_of_the_day.rss").then().body("rss.channel.item[0].link",equalTo("http://www.nasa.gov/image-feature/mocha-swirls-in-jupiter-s-turbulent-atmosphere")).log().all();}}
The following is the XML response content of this request. This type of XPath expression rss.channel.item[0].link is easy to understand, and it is located according to the level of XPath itself.rss is its outermost label, followed by the channel label, item label, and link label. The item at the same level has multiple labels, so it is necessary to locate the first item label by subscript [0].Through this positioning method, the desired response content can also be obtained.
Mocha Swirls in Jupiter’s Turbulent Atmosphere http://www.nasa.gov/image-feature/mocha-swirls-in-jupiter-s-turbulent-atmosphere...omit ...omit- ...omit...
边栏推荐
猜你喜欢
Dialog manager in the fourth chapter: the dialog message loop
数字化时代,企业如何建立自身的云平台与商业模式的选择?
StarRocks July Community Update
【生物素叠氮化物|cas:908007-17-0】价格_厂家
私域流量时代来临,电商企业如何布局?
2022/08/02 学习笔记 (day22) 多线程
mysql 创建索引的三种方式
t conditional judgment statement and if loop
Smart fitness gesture recognition: PP - TinyPose build AI virtual trainer!
三丁基-巯基膦烷「tBuBrettPhos Pd(allyl)」OTf),1798782-17-8
随机推荐
肖sir__面试接口测试
MediaRecorder录制屏幕时在部分机型上报错prepare failed:-22
UV 裂解的生物素-PEG2-叠氮|CAS:1192802-98-4生物素接头
寄存器(内存访问)
普乐蛙VR台风体验馆厂家VR防震减灾模拟VR沉浸式体验设备
【Harmony OS】【ArkUI】ets开发 图形与动画绘制
刚上线就狂吸70W粉,新型商业模式“分享购”来了,你知道吗?
redis键值出现 xacxedx00x05tx00&的解决方法
正则表达式绕过
【Harmony OS】【ARK UI】轻量级数据存储
深圳线下报名|StarRocks on AWS:如何对实时数仓进行极速统一分析
v-on指令:为元素绑定事件
计组错题集
4.深度学习的几何解释与梯度的优化
excerpt from compilation book
Kotlin-Flow常用封装类:StateFlow的使用
汇编题答案
打破传统电商格局,新型社交电商到底有什么优点?
自考六级雅思托福备战之路
安装ambari