当前位置:网站首页>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 01:04:00 【Test the world rejoice】
在服务端自动化测试过程中,发起请求之后还需要对响应值进行验证,验证响应信息符合预期值之后,这一条接口自动化测试用例才算完整的通过.所以这一章节,将会讲解在接口自动化测试中,How it is returned to the server XML Format response content for assertion verification.
环境准备
Python 版本
安装 requests_xml
pip install requests_xml
Java 版本
Rest-Assured 支持对xml 进行断言,Refer to the interface test framework chapter for installation Rest-Assured 即可.
Xml 解析方式
Python 有三种 XML 解析方式.
DOM 方式:它是文档对象模型,是 W3C 组织推荐的标准编程接口,它将 XML 数据在内存中解析成一个树,通过对树的操作来操作 XML.
SAX 方式:It's one for processing XML 事件驱动的模型,它逐行扫描文档,一边扫描一边解析,对于大型文档的解析拥有巨大优势,尽管不是 W3C 标准,但它却得到了广泛认可.
ElementTree 方式:相对于 DOM 来说拥有更好的性能,与 SAX 性能差不多,API 使用也很方便.
Python 版本
request 对 XML Format encapsulation is not strong,可以使用 request_xml 第三方库,Or you can package one yourself XML 的解析.
XML 响应断言
from requests_xml import XMLSession
# 设置session
session = XMLSession()
r = session.get("https://www.nasa.gov/rss/dyn/lg_image_of_the_day.rss")
# 打印所有的内容
r.text
# linksYou can get all the link addresses in the response
r.xml.links
# raw_xmlReturns the response content in bytes
r.xml.raw_xml
# textReturns the content in the label
r.xml.text
使用 xpath 断言
requests_xml 库也支持 XPath 表达式.可以通过 XPath Get the data of the corresponding field in the response,Put the extracted data in result 列表中,Convenient for use case assertions.
XPath 用法:
def xpath(self, selector: str, *, first: bool = False, _encoding: str = None) -> _XPath:
"""Given an XPath selector, returns a list of
:class:`Element <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 表达式
first: Whether to return only the result of the first lookup
xpath() The method returns a list of the found objects.
def test_xpath():
session = XMLSession()
r = session.get("https://www.nasa.gov/rss/dyn/lg_image_of_the_day.rss")
# 通过xpath获取所有link标签的内容
item = r.xml.xpath("//link")
result = []
for i in item:
# Put the obtained results into a list
result.append(i.text)
# 断言
assert 'http://www.nasa.gov/' in result
XML 解析
XML 是一种结构化、Hierarchical data format,Best suited for manifestation XML The data structure is the tree.可以使用 python 自带的 xml.etree.ElementTree 来解析 XML 结构.ElementTree 可以将整个 XML 文档转化为树,对整个 XML 文档的交互(读取,写入,查找元素),一般是在 ElementTree 层面进行的.
然后再使用 findall 方法,to find what you need XPath 的数据.
import xml.etree.ElementTree as ET
# 自己封装xml解析方法
session = XMLSession()
r = session.get("https://www.nasa.gov/rss/dyn/lg_image_of_the_day.rss")
# 获取响应内容
root = ET.fromstring(r.text)
# 查找根元素
em = root.findall(".")
# print(item)
items = root.findall(".//link")
result = []
# 遍历
for i in items:
result.append(i.text)
assert "http://www.nasa.gov/" in result
Java 版本
调用 body() 方法,第一个传入 XPath 表达式,The second pass in 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();
}
}
Below is the request XML 响应内容,rss.channel.item[0].link 这种类型的 XPath The expressions are easy to understand,就是根据 XPath Its own level is positioned level by level.rss is its outermost label,然后依次是 channel 标签、item 标签、link 标签,of the same level item 有多个标签,So you need to pass the subscript [0] 定位到第一个 item 标签.through this positioning,You can also get the desired response content.
<rss version="2.0" xml:base="http://www.nasa.gov/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" xmlns:media="http://search.yahoo.com/mrss/">
<channel>
<item>
<title>Mocha Swirls in Jupiter’s Turbulent Atmosphere</title>
<link>http://www.nasa.gov/image-feature/mocha-swirls-in-jupiter-s-turbulent-atmosphere</link>
...省略
</item>
...省略
<item>
...省略...
</item>
</channel>
</rss>
最后: 可以在公众号:伤心的辣条 ! 自行领取一份216页软件测试工程师面试宝典文档资料【免费的】.以及相对应的视频学习教程免费分享!,其中包括了有基础知识、Linux必备、Shell、互联网程序原理、Mysql数据库、抓包工具专题、接口测试工具、测试进阶-Python编程、Web自动化测试、APP自动化测试、接口自动化测试、测试高级持续集成、测试架构开发测试框架、性能测试、安全测试等.
现在我邀请你进入我们的软件测试学习交流群:【746506216
】,备注“入群”, 大家可以一起探讨交流软件测试,共同学习软件测试技术、面试等软件测试方方面面,还会有免费直播课,收获更多测试技巧,我们一起进阶Python自动化测试/测试开发,走向高薪之路.
喜欢软件测试的小伙伴们,如果我的博客对你有帮助、如果你喜欢我的博客内容,请 “点赞” “评论” “收藏” 一 键三连哦!
软件测试工程师自学教程:
这才是2022最精细的自动化测试自学教程,我把它刷了无数遍才上岸字节跳动,做到涨薪20K【值得自学软件测试的人刷】
软件测试工程师月薪2W以上薪资必学技能 — Python接口自动化框架封装.
美团面试真题_高级测试25K岗位面试 — 软件测试人都应该看看
软件测试必会_Jmeter大厂实战 — 仅6步可实现接口自动化测试
边栏推荐
- [TypeScript] Deep Learning of TypeScript Classes (Part 1)
- Software testing pen questions 1 (with answers)
- Word operation: adjust the English font individually
- 目前为止 DAO靠什么盈利?
- B站回应HR称用户是Loser:涉事面试官去年底已被劝退
- Towards a General Purpose CNN for Long Range Dependencies in ND
- If the watermark according to how to realize the function
- 若依集成minio实现分布式文件存储
- 牛客每日刷题之链表
- ssm整合(三)Controller 和 视图层编写
猜你喜欢
WebShell 木马免杀过WAF
mysql根据多字段分组——group by带两个或多个参数
万物智联时代,悄然走入生活
kubernetes pod podsecurityPolicies(PSP)
FastCorrect:语音识别快速纠错模型丨RTC Dev Meetup
采用QT进行OpenGL开发(三)着色器编程
Matplotlib drawing core principles explain (more detailed)
resubmit 渐进式防重复提交框架简介
工业元宇宙的价值和发展
The only way to go from a monthly salary of 10k to 30k: automated testing
随机推荐
ROS2初级知识(9):bag记录过程数据和重放
Jmeter二次开发实现rsa加密
TDengine 在中天钢铁 GPS、 AIS 调度中的落地
【斯坦福计网CS144项目】Lab5: NetworkInterface
基于两级分解和长短时记忆网络的短期风速多步组合预测模型
Image recognition from zero to write DNF script key points
How many ways do you know the singleton pattern?
IDO预售代币合约系统开发技术说明及源码分析
MDL 内存描述符链表
从月薪10k到30k的必走之路:自动化测试
go 反射 reflect 包
在迁移测试中,源表、中间表、目标表的迁移规则
创建型模式 - 简单工厂模式StaticFactoryMethod
Web APIs BOM- 操作浏览器-Window对象
How to seize the new trend of NFT, yuan|universe|universe?
学习基因富集工具DAVID(3)
# 医院管理系统完整项目代码以及数据库建表语句分享
你离「TDengine 开发者大会」只差一条 SQL 语句!
测试ESP32-Zigbee转发命令 : 滑轨、继电器控制
today‘s task