当前位置:网站首页>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...
边栏推荐
猜你喜欢
随机推荐
【Harmony OS】【ArkUI】ets开发 图形与动画绘制
社交电商:链动2+1模式,为什么能在电商行业生存那么久?
I ported GuiLite to STM32F4 board
13.机器学习基础:数据预处理与特征工程
online test paper concept
2022河南萌新联赛第(四)场:郑州轻工业大学 E - 睡大觉
接口测试如何准备测试数据
7.Keras开发简介
自组织是管理者和成员的双向奔赴
IO进程线程->线程->day5
我将GuiLite移植到了STM32F4开发板上
接口测试框架实战(二)| 接口请求断言
DC-4靶场搭建及渗透实战详细过程(DC靶场系列)
深圳线下报名|StarRocks on AWS:如何对实时数仓进行极速统一分析
How to use the interface management tool YApi?Beautiful, easy to manage, super easy to use
工程水文学知识点
【Harmony OS】【ARK UI】轻量级数据存储
mysql bool blind
BIOTIN ALKYNE CAS:773888-45-2价格,供应商
测开:项目管理模块-项目curd开发









