Web ——UI 自動化測試框架 pyse

WebUI automation testing framework based on Selenium and unittest.

基于 selenium 和 unittest 的 Web UI自動化測試框架。

特點

1、默認使用CSS定位,同時支持多種定位方法(id\name\class\link_text\xpath\css)。
2、基于Selenium二次封裝,使用更簡單。
3、提供腳手架,快速生成自動化測試項目。
4、自動生成/reports/目錄,以及HTML測試報告生成。
5、自帶斷言方法,斷言title、URL 和 text。
6、支持用例參數(shù)化。

加入我們。群,642830685,免費領(lǐng)取最新軟件測試大廠面試資料和Python自動化、接口、框架搭建學(xué)習(xí)資料!

安裝

> pip install -U git+https://github.com/defnngj/pyse.git@master

pyse命令

1、查看幫助:

> pyse -h
usage: pyse [-h] [-V] [--startproject STARTPROJECT] [-r R]

WebUI automation testing framework based on Selenium.

optional arguments:
  -h, --help            show this help message and exit
  -V, --version         show version
  --startproject STARTPROJECT
                        Specify new project name.
  -r R                  run test case

2、創(chuàng)建項目:

>pyse --startproject mypro

2019-11-07 00:24:57,783 - INFO - Start to create new test project: mypro

2019-11-07 00:24:57,784 - INFO - CWD: D:\

2019-11-07 00:24:57,785 - INFO - created folder: mypro
2019-11-07 00:24:57,786 - INFO - created folder: mypro\test_dir
2019-11-07 00:24:57,787 - INFO - created folder: mypro\reports
2019-11-07 00:24:57,788 - INFO - created file: mypro\test_dir\test_sample.py
2019-11-07 00:24:57,789 - INFO - created file: mypro\run.py

3、運行項目:

> cd mypro\
> pyse -r run.py
Python 3.7.1                                                                    

 ______  __   __  _______  _______                                              
|   _  ||  | |  ||  _____||   ____|                                             
|  |_| ||  |_|  || |_____ |  |____                                              
|   ___||_     _||_____  ||   ____|                                             
|  |      |   |   _____| ||  |____                                              
|__|      |___|  |_______||_______|                                                                                     
generated html file: file:///D:\mypro\reports\2019_11_12_22_28_53_result.html   
.1

4、查看報告

你可以到 mypro\reports\ 目錄查看測試報告。


API Documents

simple demo

請查看 test_sample.py 文件

import pyse

class BaiduTest(pyse.TestCase):

    def test_baidu(self):
        ''' baidu search key : pyse '''
        self.open("https://www.baidu.com/")
        self.type("#kw", "pyse")
        self.click("#su")
        self.assertTitle("pyse_百度搜索")

if __name__ == '__main__':
    pyse.main("test_sample.py", debug=True)

說明:
1、創(chuàng)建測試類必須繼承 pyse.TestCase 。
2、測試用例文件命名必須以 test 開頭。
3、元素定位方式默認使用 CSS 語法 #kw , 也可以顯示的使用 css=>#kw 。
4、pyse的封裝了 assertTitle 、 assertUrl 和 assertText 等斷言方法。
5、通過 main() 方法運行測試用例。

API

pyse 提供的API

self.accept_alert()

self.clear("css=>#el")

self.click("css=>#el")

self.click_text("新聞")

self.dismiss_alert()

self.double_click("css=>#el")

self.drag_and_drop("css=>#el","css=>#ta")

self.get_alert_text()

self.get_attribute("css=>#el","type")

self.get_display("css=>#el")

self.get_text("css=>#el")

self.get_title()

self.get_url()

self.js("window.scrollTo(200,1000);")

self.max_window()

self.move_to_element("css=>#el")

self.open("https://www.baidu.com")

self.open_new_window("link_text=>注冊")

self.close()

self.quit()

self.refresh()

self.right_click("css=>#el")

self.screenshots('/Screenshots/foo.png')

self.select("#nr", '20')
self.select("xpath=>//[@name='NR']", '20')

self.set_window(wide, high)

self.submit("css=>#el")

self.switch_to_frame("css=>#el")

self.switch_to_frame_out()

self.type("css=>#el","selenium")

self.wait(10)

main() 方法

pyse.main(path="./",
          browser="chrome",
          title="百度測試用例", 
          description="測試環(huán)境:Firefox", 
          debug=True
)

說明:

path : 指定測試目錄。
browser: 指定測試瀏覽器,默認Chrome。
title : 指定測試項目標(biāo)題。
description : 指定測試描述。
debug : debug模式,設(shè)置為True不生成測試HTML測試。

Run the test

pyse.main(path="./")  # 當(dāng)前目錄下的所有測試文件
pyse.main(path="./test_dir/")  # 指定目錄下的所有測試文件
pyse.main(path="./test_dir/test_sample.py")  # 指定目錄下的測試文件
pyse.main(path="test_sample.py")  # 指定當(dāng)前目錄下的測試文件

說明:

test
__init__.py

支持的瀏覽器及驅(qū)動

如果你想指定測試用例在不同的瀏覽器中運行,非常簡單,只需要在 pyse.main() 方法中通過 browser 設(shè)置。

if __name__ == '__main__':
    pyse.main(browser="firefox")

支持的瀏覽器包括: "chrome" 、 "firefox" 、 "ie" 、 "opera" 、 "edge" 、 "chrome_headless" 等。

元素定位

pyse支持多種定位方式,id、name、class、link text、xpath和css。把定位方法與定位內(nèi)容一體,寫起更加簡潔。

<form id="form" class="fm" action="/s" name="f">
    <span class="bg s_ipt_wr quickdelete-wrap">
        <input id="kw" class="s_ipt" name="wd">

定位方式(推薦使用 CSS):

# 默認支持CSS語法
self.type(".s_ipt","pyse")     #css
self.type("#su","pyse")        #css

# id
self.type("id=>kw", "pyse")  #id

# class name
self.type("class=>s_ipt", "pyse")  #class定位

# name
self.type("name=>wd", "pyse")  #name

# xpath
self.type("xpath=>//*[@class='s_ipt']","pyse")  #xpath
self.type("xpath=>//*[@id='kw']","pyse")        #xpath

# link text
self.click_text("新聞") #link text (點擊百度首頁上的"新聞"鏈接)

參數(shù)化測試用例

pyse 支持參數(shù)化測試用例,集成了 [parameterized]

import pyse
from pyse import ddt


class BaiduTest(pyse.TestCase):

    @ddt.data([
        (1, 'pyse'),
        (2, 'selenium'),
        (3, 'unittest'),
    ])
    def test_baidu(self, name, keyword):
        """
         used parameterized test
        :param name: case name
        :param search_key: search keyword
        """
        self.open("https://www.baidu.com")
        self.clear("id=>kw")
        self.type("id=>kw", keyword)
        self.click("css=>#su")
        self.assertTitle(keyword)

希望本文對你有所幫助,想要了解更多。加入我們,有技術(shù)大牛解惑,同行一起交流。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容