欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python實現(xiàn)測試工具(二)——簡單的ui測試工具

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

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

背景

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

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

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

視頻講解在這里。

工具設(shè)計

我們設(shè)計一個命令行工具,給工具傳3個參數(shù)。

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

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

代碼實現(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}個元素,實際存在{len(elements)}個元素\n")
 debug()
 exit(1)
else:
 print(f"成功!\n")
 debug()

精講

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

運行示例

# 失敗情況
python html_assertion.py www.itest.info .thumbnail-img 1
失敗! 預(yù)期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',)}

動手時間

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

擴展閱讀

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

源碼地址

github地址

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

相關(guān)文章

  • python淘寶準點秒殺搶單的實現(xiàn)示例

    python淘寶準點秒殺搶單的實現(xiàn)示例

    為了想要搶到想要的商品,想了個用Python實現(xiàn)python淘寶準點秒殺搶單方案,需要的朋友們下面隨著小編來一起學習學習吧
    2021-05-05
  • python工具快速為音視頻自動生成字幕(使用說明)

    python工具快速為音視頻自動生成字幕(使用說明)

    這篇文章主要介紹了python工具快速為音視頻自動生成字幕(使用說明),本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-01-01
  • Python如何生成指定區(qū)間中的隨機數(shù)

    Python如何生成指定區(qū)間中的隨機數(shù)

    這篇文章主要介紹了Python如何生成指定區(qū)間中的隨機數(shù),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-07-07
  • Python3內(nèi)置模塊之base64編解碼方法詳解

    Python3內(nèi)置模塊之base64編解碼方法詳解

    這篇文章主要介紹了Python3內(nèi)置模塊之base64編解碼方法詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2019-07-07
  • python實現(xiàn)分析apache和nginx日志文件并輸出訪客ip列表的方法

    python實現(xiàn)分析apache和nginx日志文件并輸出訪客ip列表的方法

    這篇文章主要介紹了python實現(xiàn)分析apache和nginx日志文件并輸出訪客ip列表的方法,涉及Python操作日志文件的技巧,非常具有實用價值,需要的朋友可以參考下
    2015-04-04
  • python中繞過反爬蟲的方法總結(jié)

    python中繞過反爬蟲的方法總結(jié)

    在本篇文章里小編給大家整理的是一篇關(guān)于python中繞過反爬蟲的方法總結(jié)內(nèi)容,需要的朋友們可以參考下。
    2020-11-11
  • python回調(diào)函數(shù)的使用方法

    python回調(diào)函數(shù)的使用方法

    在計算機程序設(shè)計中,回調(diào)函數(shù),或簡稱回調(diào)(Callback),是指通過函數(shù)參數(shù)傳遞到其它代碼的,某一塊可執(zhí)行代碼的引用。這一設(shè)計允許了底層代碼調(diào)用在高層定義的子程序
    2014-01-01
  • Python中g(shù)etattr函數(shù)詳解

    Python中g(shù)etattr函數(shù)詳解

    getattr是Python中的內(nèi)置函數(shù),用于獲取一個對象的屬性值,下面這篇文章主要給大家介紹了關(guān)于Python中g(shù)etattr函數(shù)的相關(guān)資料,文中通過代碼介紹的非常詳細,需要的朋友可以參考下
    2023-11-11
  • Python?OpenCV?Canny邊緣檢測算法的原理實現(xiàn)詳解

    Python?OpenCV?Canny邊緣檢測算法的原理實現(xiàn)詳解

    這篇文章主要介紹了Python?OpenCV?Canny邊緣檢測算法的原理實現(xiàn)詳解,由于邊緣檢測對噪聲敏感,因此對圖像應(yīng)用高斯平滑以幫助減少噪聲,具體詳情需要的小伙伴可以參考一下
    2022-07-07
  • Python中異常重試的解決方案詳解

    Python中異常重試的解決方案詳解

    這篇文章主要給大家介紹了在Python中異常重試的解決方案,文中介紹的非常詳細,相信對大家學習或者使用python具有一定的參考學習價值,需要的朋友們下面隨著小編一起來看看吧。
    2017-05-05

最新評論