python實(shí)現(xiàn)測試工具(二)——簡單的ui測試工具
本系列教程使用的python版本是3.6.3。
背景
這一節(jié)我們實(shí)現(xiàn)一個簡單的ui測試工具。
該工具的作用是訪問某個頁面,然后根據(jù)css選擇器去定位頁面上的元素,最后判斷頁面上元素的個數(shù)與我們的預(yù)期是否相符。
舉一個具體的例子,比如我們?nèi)ピL問www.itest.info這個頁面,我們需要判斷頁面上class = thumbnail-img的元素存在,并且有4個。因?yàn)槊恳粋€元素代表一門課程,所以這個斷言的意思是重定向科技主頁上應(yīng)該有4門主要課程。
視頻講解在這里。
工具設(shè)計(jì)
我們設(shè)計(jì)一個命令行工具,給工具傳3個參數(shù)。
- 被訪問頁面的url
- 頁面上元素的css選擇器
- 預(yù)期的元素?cái)?shù)量,頁面上可以存在n個元素,如果傳入0,則表示元素不存在,做反向斷言
所以工具大概是這樣用的: python script_name.py url css_selector length
代碼實(shí)現(xiàn)
簡單起見,我們會用到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"失敗! 預(yù)期{length}個元素,實(shí)際存在{len(elements)}個元素\n")
debug()
exit(1)
else:
print(f"成功!\n")
debug()
精講
用例失敗之后使用exit(1)表示異常退出,這樣在使用jenkins運(yùn)行的時候,用例失敗jenkins的job結(jié)果也會相應(yīng)失敗
requests-html庫的基本使用參考這里
運(yùn)行示例
# 失敗情況
python html_assertion.py www.itest.info .thumbnail-img 1
失敗! 預(yù)期1個元素,實(shí)際存在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',)}
動手時間
- 抄一遍代碼,看自己能不能運(yùn)行起來
- 給這段代碼每一行都加上注釋,理解代碼做了些什么
擴(kuò)展閱讀
https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Selectors
源碼地址
以上就是python實(shí)現(xiàn)測試工具(二)——簡單的ui測試工具的詳細(xì)內(nèi)容,更多關(guān)于python ui測試的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
利用pyproj將經(jīng)緯度投影為平面坐標(biāo)以及地理坐標(biāo)系背景知識解讀
這篇文章主要介紹了利用pyproj將經(jīng)緯度投影為平面坐標(biāo)以及地理坐標(biāo)系背景知識解讀,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-06-06
Python?Requests使用Cookie的幾種方式詳解
這篇文章主要給大家介紹了關(guān)于Python?Requests使用Cookie的幾種方式,Python中的requests庫可以使用cookie來維持會話狀態(tài),實(shí)現(xiàn)登錄等操作,需要的朋友可以參考下2023-07-07
對python中的pop函數(shù)和append函數(shù)詳解
今天小編就為大家分享一篇對python中的pop函數(shù)和append函數(shù)詳解,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-05-05
python庫pycryptodom加密技術(shù)探索(公鑰加密私鑰加密)
這篇文章主要為大家介紹了python庫pycryptodom加密技術(shù)探索(公鑰加密私鑰加密),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2024-01-01
python opencv鼠標(biāo)事件實(shí)現(xiàn)畫框圈定目標(biāo)獲取坐標(biāo)信息
這篇文章主要為大家詳細(xì)介紹了python opencv鼠標(biāo)事件實(shí)現(xiàn)畫框圈定目標(biāo),獲取坐標(biāo)信息,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-08-08
詳解如何用python實(shí)現(xiàn)一個簡單下載器的服務(wù)端和客戶端
這篇文章主要介紹了詳解如何用python實(shí)現(xiàn)一個簡單下載器的服務(wù)端和客戶端,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10

