当前位置:网站首页>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...
边栏推荐
猜你喜欢
那些让电子工程师崩溃瞬间,你经历了几个呢?
普乐蛙VR台风体验馆厂家VR防震减灾模拟VR沉浸式体验设备
探索性测试的概念及方法
IDEC和泉触摸屏维修HG2F-SS22V HG4F软件通信分析
2022/08/02 学习笔记 (day22) 多线程
Record some bugs encountered - when mapstruct and lombok are used at the same time, the problem of data loss when converting entity classes
Redis缓存雪崩、缓存穿透、缓存击穿
CobalStrike(CS)基础超级详细版
redis键值出现 xacxedx00x05tx00&的解决方法
Jmeter 模拟多用户登录的两种方法
随机推荐
测开:项目管理模块-项目curd开发
EssilorLuxottica借助Boomi的智能集成平台实现订单处理的现代化
「短视频+社交电商」营销模式爆发式发展,带来的好处有什么?
ScanNet数据集讲解与点云数据下载
Mysql如何建立索引实现语句优化
在线密码生成工具推荐
传统企业如何转型社交电商,泰山众筹的玩法有哪些?
刚上线就狂吸70W粉,新型商业模式“分享购”来了,你知道吗?
Smart fitness gesture recognition: PP - TinyPose build AI virtual trainer!
寄存器(内存访问)
DDL操作数据库、表、列
数值类型转换02
c语言结构体中的冒泡排序
2022/08/02 学习笔记 (day22) 多线程
LeetCode算法日记:面试题 03.04. 化栈为队
Can Oracle EMCC be installed independently?Or does it have to be installed on the database server?
install ambari
v-text指令:设置标签内容
刚上线就狂吸70W粉,新型商业模式“分享购”来了,你知道吗?
Shell编程的条件语句