python如何实现ui测试工具

发布时间:2020-10-27 18:07:56 作者:Leah
来源:亿速云 阅读:208

python如何实现ui测试工具?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。

背景

这一节我们实现一个简单的ui测试工具。

该工具的作用是访问某个页面,然后根据css选择器去定位页面上的元素,最后判断页面上元素的个数与我们的预期是否相符。

举一个具体的例子,比如我们去访问www.itest.info这个页面,我们需要判断页面上class = thumbnail-img的元素存在,并且有4个。因为每一个元素代表一门课程,所以这个断言的意思是重定向科技主页上应该有4门主要课程。

视频讲解在这里。

工具设计

我们设计一个命令行工具,给工具传3个参数。

所以工具大概是这样用的: python script_name.py url css_selector length

代码实现

简单起见,我们会用到requests-html库。安装文档在这里。

from requests_html import HTMLSession
from sys import argv
DEBUG = True

USAGE = '''
USAGE:
python html_assertion.py www.itest.info .thumbnail-img 4
'''

if len(argv) != 4:
 print(USAGE)
 exit(1)

script_name, url, css_selector, length = argv

if url[:4] != 'http':
 url = 'http://' + url

session = HTMLSession()
r = session.get(url)

elements = r.html.find(css_selector)


def debug():
 if DEBUG:
 print('*' * 100)
 print(f"css选择器: {css_selector}, 共找到{len(elements)}个元素\n")
 for element in elements:
  print(element.html)
  print(element.attrs)
  print()


if len(elements) != int(length):
 print(f"失败! 预期{length}个元素,实际存在{len(elements)}个元素\n")
 debug()
 exit(1)
else:
 print(f"成功!\n")
 debug()

精讲

用例失败之后使用exit(1)表示异常退出,这样在使用jenkins运行的时候,用例失败jenkins的job结果也会相应失败
requests-html库的基本使用参考这里

运行示例

# 失败情况
python html_assertion.py www.itest.info .thumbnail-img 1
失败! 预期1个元素,实际存在4个元素

****************************************************************************************************
css选择器: .thumbnail-img, 共找到4个元素

<div class="thumbnail-img"><div class="overflow-hidden"><img class="img-responsive" src="/uploads/course/image/7/mission_impossible_cut.jpg"/></div><a class="btn-more hover-effect" href="/courses/7" rel="external nofollow" rel="external nofollow" >更多</a></div>
{'class': ('thumbnail-img',)}

<div class="thumbnail-img"><div class="overflow-hidden"><img class="img-responsive" src="/uploads/course/image/6/120606ineam4nspdc6qdaw.jpg"/></div><a class="btn-more hover-effect" href="/courses/6" rel="external nofollow" rel="external nofollow" >更多</a></div>
{'class': ('thumbnail-img',)}

<div class="thumbnail-img"><div class="overflow-hidden"><img class="img-responsive" src="/uploads/course/image/3/12.jpg"/></div><a class="btn-more hover-effect" href="/courses/3" rel="external nofollow" rel="external nofollow" >更多</a></div>
{'class': ('thumbnail-img',)}

<div class="thumbnail-img"><div class="overflow-hidden"><img class="img-responsive" src="/uploads/course/image/2/13.jpg"/></div><a class="btn-more hover-effect" href="/courses/2" rel="external nofollow" rel="external nofollow" >更多</a></div>
{'class': ('thumbnail-img',)}

# 成功情况
python html_assertion.py www.itest.info .thumbnail-img 4
成功!

****************************************************************************************************
css选择器: .thumbnail-img, 共找到4个元素

<div class="thumbnail-img"><div class="overflow-hidden"><img class="img-responsive" src="/uploads/course/image/7/mission_impossible_cut.jpg"/></div><a class="btn-more hover-effect" href="/courses/7" rel="external nofollow" rel="external nofollow" >更多</a></div>
{'class': ('thumbnail-img',)}

<div class="thumbnail-img"><div class="overflow-hidden"><img class="img-responsive" src="/uploads/course/image/6/120606ineam4nspdc6qdaw.jpg"/></div><a class="btn-more hover-effect" href="/courses/6" rel="external nofollow" rel="external nofollow" >更多</a></div>
{'class': ('thumbnail-img',)}

<div class="thumbnail-img"><div class="overflow-hidden"><img class="img-responsive" src="/uploads/course/image/3/12.jpg"/></div><a class="btn-more hover-effect" href="/courses/3" rel="external nofollow" rel="external nofollow" >更多</a></div>
{'class': ('thumbnail-img',)}

<div class="thumbnail-img"><div class="overflow-hidden"><img class="img-responsive" src="/uploads/course/image/2/13.jpg"/></div><a class="btn-more hover-effect" href="/courses/2" rel="external nofollow" rel="external nofollow" >更多</a></div>
{'class': ('thumbnail-img',)}

关于python如何实现ui测试工具问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注亿速云行业资讯频道了解更多相关知识。

推荐阅读:
  1. salt-UI的实现
  2. python如何实现自动化UI工具发送QQ消息

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

python ui

上一篇:Java:String.split()特殊字符如何进行处理

下一篇:django使用channels如何实现通信

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》