最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

python實(shí)現(xiàn)測(cè)試工具(二)——簡(jiǎn)單的ui測(cè)試工具

 更新時(shí)間:2020年10月19日 15:47:25   作者:乙醇  
這篇文章主要介紹了python如何實(shí)現(xiàn)簡(jiǎn)單的ui測(cè)試工具,幫助大家更好的利用python進(jìn)行測(cè)試工作,感興趣的朋友可以了解下

本系列教程使用的python版本是3.6.3。

背景

這一節(jié)我們實(shí)現(xiàn)一個(gè)簡(jiǎn)單的ui測(cè)試工具。

該工具的作用是訪問某個(gè)頁(yè)面,然后根據(jù)css選擇器去定位頁(yè)面上的元素,最后判斷頁(yè)面上元素的個(gè)數(shù)與我們的預(yù)期是否相符。

舉一個(gè)具體的例子,比如我們?nèi)ピL問www.itest.info這個(gè)頁(yè)面,我們需要判斷頁(yè)面上class = thumbnail-img的元素存在,并且有4個(gè)。因?yàn)槊恳粋€(gè)元素代表一門課程,所以這個(gè)斷言的意思是重定向科技主頁(yè)上應(yīng)該有4門主要課程。

視頻講解在這里。

工具設(shè)計(jì)

我們?cè)O(shè)計(jì)一個(gè)命令行工具,給工具傳3個(gè)參數(shù)。

  • 被訪問頁(yè)面的url
  • 頁(yè)面上元素的css選擇器
  • 預(yù)期的元素?cái)?shù)量,頁(yè)面上可以存在n個(gè)元素,如果傳入0,則表示元素不存在,做反向斷言

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

代碼實(shí)現(xiàn)

簡(jiǎn)單起見,我們會(huì)用到requests-html庫(kù)。安裝文檔在這里。

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)}個(gè)元素\n")
  for element in elements:
   print(element.html)
   print(element.attrs)
   print()


if len(elements) != int(length):
 print(f"失敗! 預(yù)期{length}個(gè)元素,實(shí)際存在{len(elements)}個(gè)元素\n")
 debug()
 exit(1)
else:
 print(f"成功!\n")
 debug()

精講

用例失敗之后使用exit(1)表示異常退出,這樣在使用jenkins運(yùn)行的時(shí)候,用例失敗jenkins的job結(jié)果也會(huì)相應(yīng)失敗
requests-html庫(kù)的基本使用參考這里

運(yùn)行示例

# 失敗情況
python html_assertion.py www.itest.info .thumbnail-img 1
失敗! 預(yù)期1個(gè)元素,實(shí)際存在4個(gè)元素

****************************************************************************************************
css選擇器: .thumbnail-img, 共找到4個(gè)元素

<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個(gè)元素

<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',)}

動(dòng)手時(shí)間

  • 抄一遍代碼,看自己能不能運(yùn)行起來
  • 給這段代碼每一行都加上注釋,理解代碼做了些什么

擴(kuò)展閱讀

https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Selectors

源碼地址

github地址

以上就是python實(shí)現(xiàn)測(cè)試工具(二)——簡(jiǎn)單的ui測(cè)試工具的詳細(xì)內(nèi)容,更多關(guān)于python ui測(cè)試的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評(píng)論

双牌县| 千阳县| 津南区| 无极县| 松原市| 肇东市| 苍南县| 铜陵市| 鹰潭市| 南部县| 溆浦县| 郯城县| 东莞市| 平湖市| 曲周县| 凤凰县| 额尔古纳市| 桐乡市| 怀柔区| 海南省| 紫阳县| 仁寿县| 石楼县| 西平县| 磴口县| 缙云县| 云林县| 肇东市| 丹阳市| 长葛市| 资阳市| 惠来县| 蓝山县| 西吉县| 临泉县| 上虞市| 赤城县| 葫芦岛市| 虎林市| 辽宁省| 洛阳市|