当前位置:网站首页>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步可实现接口自动化测试
边栏推荐
- qt静态编译出现Project ERROR: Library ‘odbc‘ is not defined
- 【使用pyside2遇到的问题】This application failed to start because no Qt platform plugin could be initialized.
- redis的学习笔记
- wallys/new product/WiFi6 MiniPCIe Module 2T2R 2×2.4GHz 2x5GHz MT7915 MT7975
- # 医院管理系统完整项目代码以及数据库建表语句分享
- [TypeScript] Deep Learning of TypeScript Classes (Part 1)
- RuoYi-App Startup Tutorial
- go os 包
- 任务四 机器学习库Scikit-learn
- 典型相关分析CCA计算过程
猜你喜欢
随机推荐
AcWing 2983. 玩具
centos7安装mysql5.7步骤(图解版)
today‘s task
TCP三次握手与四次挥手
Towards a General Purpose CNN for Long Range Dependencies in ND
【使用pyside2遇到的问题】This application failed to start because no Qt platform plugin could be initialized.
刚安装完win10专业工作站版,系统变量中Path默认值有哪些?重新建一个“PATH”变量名,会覆盖掉原先的“Path”。
word操作:单独调整英文字体
redis的学习笔记
The CTF command execution subject their thinking
ZCMU--5230: 排练方阵(C语言)
Learn more TypeScript 】 【 TypeScript modular
基于两级分解和长短时记忆网络的短期风速多步组合预测模型
qt静态编译出现Project ERROR: Library ‘odbc‘ is not defined
创建型模式 - 抽象工厂模式AbstractFactory
无代码开发平台表单样式设置步骤入门课程
go os 包
创建型模式 - 简单工厂模式StaticFactoryMethod
成功解决TypeError: can‘t multiply sequence by non-int of type ‘float‘
了解 NFT 质押:Web3 中赚取被动收益的另一种方式