当前位置:网站首页>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_xmlJava 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 resultJava 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...
边栏推荐
- 三丁基-巯基膦烷「tBuBrettPhos Pd(allyl)」OTf),1798782-17-8
- DC-3靶场搭建及渗透实战详细过程(DC靶场系列)
- MySql 创建索引
- 私域流量引流方法?分享购火爆的商业模式,你值得拥有
- 接口测试 Mock 实战(二) | 结合 jq 完成批量化的手工 Mock
- [Developers must see] [push kit] Collection of typical problems of push service service 2
- 接口和协议
- 【Harmony OS】【ArkUI】ets开发 图形与动画绘制
- MediaRecorder录制屏幕时在部分机型上报错prepare failed:-22
- LeetCode算法日记:面试题 03.04. 化栈为队
猜你喜欢
随机推荐
Live | StarRocks technology insider: low base dictionary global optimization
Problems that need to be solved for interrupting the system
mysql bool盲注
leetcode刷题学习之路
【Harmony OS】【ARK UI】Date 基本操作
DC-3靶场搭建及渗透实战详细过程(DC靶场系列)
Redis连接不上的报错解决方案汇总
表的创建、修改与删除
flink sql任务变更,在sql里面增加几个字段后,从以前保存的savepoint恢复启动出错。
12.机器学习基础:评估机器学习模型
Shell之条件语句
StarRocks 7 月社区动态
技术分享 | 接口自动化测试中如何对xml 格式做断言验证?
Oracle EMCC可以独立安装吗?还是必须安装到数据库服务器上?
【Harmony OS】【ARK UI】轻量级数据存储
社交电商:链动2+1模式,为什么能在电商行业生存那么久?
Record some bugs encountered - when mapstruct and lombok are used at the same time, the problem of data loss when converting entity classes
2022/08/02 Study Notes (day22) Multithreading
中断系统需要解决的问题
Browser listens for tab closing









