簡單Web UI 自動化測試框架 seldom


pyse 更名為 seldom

WebUI automation testing framework based on Selenium and unittest.

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

特點

  • 提供更加簡單API編寫自動化測試。
  • 提供腳手架,快速生成自動化測試項目。
  • 自動生成HTML測試報告生成。
  • 自帶斷言方法,斷言title、URL 和 text。
  • 支持用例參數化。
  • 支持用例失敗重跑。
  • 用例失敗/錯誤截圖。

安裝

> pip install seldom

If you want to keep up with the latest version, you can install with github repository url:

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

Quick Start

1、查看幫助:

> seldom -h
usage: seldom [-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、創建項目:

>seldom --startproject mypro

3、目錄結構:

mypro/
├── test_dir/
│   ├── test_sample.py
├── report/
└── run.py
  • test_dir/目錄實現用例編寫。
  • report/ 目錄存放生成的測試報告。
  • run.py 文件運行測試用例。

3、運行項目:

> seldom -r run.py
Python 3.7.1                                                                    

            _      _
           | |    | |
 ___   ___ | |  __| |  ___   _ __ ___
/ __| / _ \| | / _` | / _ \ | '_ ` _ \
\__ \|  __/| || (_| || (_) || | | | | |
|___/ \___||_| \__,_| \___/ |_| |_| |_|
-----------------------------------------
                             @itest.info

generated html file: file:///D:\mypro\reports\2019_11_12_22_28_53_result.html   
.1                                                                              

4、查看報告

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

API Documents

simple demo

請查看 demo/test_sample.py 文件

import seldom


class YouTest(seldom.TestCase):

    def test_case(self):
        """a simple test case """
        self.open("https://www.baidu.com")
        self.type(id_="kw", text="seldom")
        self.click(css="#su")
        self.assertTitle("seldom")


if __name__ == '__main__':
    seldom.main("test_sample.py")

說明:

  • 創建測試類必須繼承 seldom.TestCase
  • 測試用例文件命名必須以 test 開頭。
  • seldom的封裝了assertTitleassertUrlassertText等斷言方法。

main() 方法

import seldom

# ...

if __name__ == '__main__':
    
    seldom.main(path="./",
              browser="chrome",
              title="百度測試用例", 
              description="測試環境:chrome", 
              debug=False,
              rerun=0,
              save_last_run=False
    )

說明:

  • path : 指定測試目錄或文件。
  • browser: 指定測試瀏覽器,默認Chrome
  • title : 指定測試報告標題。
  • description : 指定測試報告描述。
  • debug : debug模式,設置為True不生成測試HTML測試,默認為False
  • rerun : 設置失敗重新運行次數,默認為 0
  • save_last_run : 設置只保存最后一次的結果,默認為False

Run the test

import seldom

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

說明:

  • 如果指定的目錄,測試文件必須以test 開頭。
  • 如果要運行子目錄下的文件,必須在子目錄下加 __init__.py 文件。

支持的瀏覽器及驅動

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

import seldom

if __name__ == '__main__':
    seldom.main(browser="chrome") # chrome瀏覽器,默認值
    seldom.main(browser="firefox") # firefox瀏覽器
    seldom.main(browser="ie")  # IE瀏覽器
    seldom.main(browser="opera") # opera瀏覽器
    seldom.main(browser="edge") # edge瀏覽器
    seldom.main(browser="chrome_headless") # chrome瀏覽器headless模式
    seldom.main(browser="firefox_headless") # Firefox瀏覽器headless模式

不同瀏覽器驅動下載地址:

geckodriver(Firefox):https://github.com/mozilla/geckodriver/releases

Chromedriver(Chrome):https://sites.google.com/a/chromium.org/chromedriver/home

IEDriverServer(IE):http://selenium-release.storage.googleapis.com/index.html

operadriver(Opera):https://github.com/operasoftware/operachromiumdriver/releases

MicrosoftWebDriver(Edge):https://developer.microsoft.com/en-us/microsoft-edge/tools/webdriver

==========================================================

元素定位

<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">

定位方式:

self.type(id_="kw", text="seldom")
self.type(name="wd", text="seldom")
self.type(class_name="s_ipt", text="seldom")
self.type(tag="input", text="seldom")
self.type(link_text="hao123", text="seldom")
self.type(partial_link_text="hao", text="seldom")
self.type(xpath="//input[@id='kw']", text="seldom")
self.type(css="#kw", text="seldom")

參數化測試用例

seldom 支持參數化測試用例,集成了parameterized


import seldom
from seldom import ddt

# ...

class BaiduTest(seldom.TestCase):

    @ddt.data([
        (1, 'seldom'),
        (2, 'selenium'),
        (3, 'unittest'),
    ])
    def test_baidu(self, name, keyword):
        """
         used parameterized test
        :param name: case name
        :param keyword: search keyword
        """
        self.open("https://www.baidu.com")
        self.type(id_="kw", text=keyword)
        self.click(css="#su")
        self.assertTitle(search_key+"_百度搜索")

page objects 設計模式

seldom 支持Page objects設計模式,可以配合poium 使用。

import seldom
from poium import Page, PageElement


class BaiduPage(Page):
    """baidu page"""
    search_input = PageElement(id_="kw")
    search_button = PageElement(id_="su")


class BaiduTest(seldom.TestCase):
    """Baidu serach test case"""

    def test_case(self):
        """
        A simple test
        """
        page = BaiduPage(self.driver)
        page.get("https://www.baidu.com")
        page.search_input = "seldom"
        page.search_button.click()
        self.assertTitle("seldom_百度搜索")


if __name__ == '__main__':
    seldom.main("test_po_demo.py")

poium提供了更多好用的功能,使Page層的創建更加簡單。

感謝

感謝從以下項目中得到思路和幫助。

交流

QQ群:948994709


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM