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

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

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

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

背景

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

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

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

視頻講解在這里。

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

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

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

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

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

簡單起見,我們會(huì)用到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)}個(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庫的基本使用參考這里

運(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è)試工具(二)——簡單的ui測(cè)試工具的詳細(xì)內(nèi)容,更多關(guān)于python ui測(cè)試的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • python淘寶準(zhǔn)點(diǎn)秒殺搶單的實(shí)現(xiàn)示例

    python淘寶準(zhǔn)點(diǎn)秒殺搶單的實(shí)現(xiàn)示例

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

最新評(píng)論