從今天起,我們從零開始完整地實踐一個Web的自動化測試項目。
新建虛擬環境
當我們開始一個新的項目時,使用虛擬環境是一個良好的習慣。管理虛擬環境的工具有很多,既有python3.3開始內置的venv模塊,也有第三方工具pipenv、poetry等。這裏,我們選擇poetry。
首先,新建項目,命名爲rafiki。
PS G:\luizyao> poetry new rafiki
Created package rafiki in rafiki
PS G:\luizyao>
PS G:\luizyao> tree /f rafiki # 查看項目的組織結構
Folder PATH listing for volume Windows
Volume serial number is A2F6-0F5B
G:\LUIZYAO\RAFIKI
│ pyproject.toml
│ README.rst
│
├─rafiki
│ __init__.py
│
└─tests
test_rafiki.py
__init__.py
rafiki來自於紀錄片塞倫蓋蒂中一隻狒狒的名字。
poetry默認將pytest作爲dev依賴添加到項目的配置文件中:
# pyproject.toml [tool.poetry.dependencies] python = "^3.8" [tool.poetry.dev-dependencies] pytest = "^5.2"
因爲pytest是我們的主要依賴,所以我們將上述部分修改成:
# pyproject.toml [tool.poetry.dependencies] python = "^3.8" pytest = "^6.0" [tool.poetry.dev-dependencies]
目前pytest的最新版本是6.0.0。
然後,安裝虛擬環境:
PS G:\luizyao\rafiki> poetry install --no-root
Creating virtualenv rafiki-v2Ofcj9r-py3.8 in C:\Users\Administrator\AppData\Local\pypoetry\Cache\virtualenvs
Updating dependencies
Resolving dependencies...
Writing lock file
Package operations: 12 installs, 0 updates, 0 removals
- Installing pyparsing (2.4.7)
- Installing six (1.15.0)
- Installing atomicwrites (1.4.0)
- Installing attrs (19.3.0)
- Installing colorama (0.4.3)
- Installing iniconfig (1.0.0)
- Installing more-itertools (8.4.0)
- Installing packaging (20.4)
- Installing pluggy (0.13.1)
- Installing py (1.9.0)
- Installing toml (0.10.1)
- Installing pytest (6.0.0)
這時,poetry已經自動幫我們安裝好最新的pytest了。
最後,查看我們創建的虛擬環境的信息:
PS G:\luizyao\rafiki> poetry env info
Virtualenv
Python: 3.8.4
Implementation: CPython
Path: C:\Users\Administrator\AppData\Local\pypoetry\Cache\virtualenvs\rafiki-v2Ofcj9r-py3.8
Valid: True
System
Platform: win32
OS: nt
Python: G:\Program Files\python38
selenium vs puppeteer
WEB測試最常用的工具可能就是selenium了,但是我在之前的使用中也遇到了一些不方便的情況:
- WebDriver和瀏覽器版本需要對應;
- 雖然是跨瀏覽器的腳本,但有時還是要對不同的瀏覽器做一些適配;
- selenium通過WebDriver驅動瀏覽器,執行的不夠快;
最好是能夠跳過WebDriver,直接驅動瀏覽器。目前這種測試框架有cypress、testcafe、puppeteer等,不過它們都是Node.js的框架,而javascript足以讓大多數測試人員望而卻步。
puppeteer嚴格的說並不是一個測試框架,它的官方描述是:
Puppeteer is a Node library which provides a high-level API to control Chrome or Chromium over the DevTools Protocol. Puppeteer runs headless by default, but can be configured to run full (non-headless) Chrome or Chromium
它有一些限制:
- 只適用於Chrome和Chromium瀏覽器,不過官方也有支持其它瀏覽器的計劃。
更多熱點問題可以參考:https://github.com/puppeteer/puppeteer#faq
我之所以選擇puppeteer,而不是selenium的原因有以下幾點:
- selenium致力於跨瀏覽器的解決方案,而現實中Chrome的市場份額已經很高了。我之前用selenium寫的腳本,很少要求在其它的瀏覽器上執行,反而增加了維護的成本;
- selenium配置繁瑣,而puppeteer幾乎是免配置的;
- puppeteer方便進行異步操作,比如同時執行兩個瀏覽器同時下發配置;之所以有這種場景,是我的工作性質決定的,我的測試對象是通信設備,每臺設備都會提供WEB服務,而且通常需要多臺設備組成一個場景進行測試,那麼同時配置多臺設備肯定要方便的多;
當然,這只是我一家之言,如果你還是期望跨瀏覽器的解決方案,除了selenium,我想cypress是一個更好的選擇,只是要多些學習成本。
pyppeteer
puppeteer有一個非官方的Python版本----pyppeteer,雖然它已經落後puppeteer很多版本,但是基本功能還是可用的,而且已經有一羣人在接手這個項目,並開始追趕puppeteer了,所以我覺得還是可以期待的。
安裝pyppeteer:
PS G:\luizyao\rafiki> poetry add pyppeteer
Using version ^0.2.2 for pyppeteer
Updating dependencies
Resolving dependencies...
Writing lock file
Package operations: 6 installs, 0 updates, 0 removals
- Installing appdirs (1.4.4)
- Installing pyee (7.0.2)
- Installing tqdm (4.48.0)
- Installing urllib3 (1.25.10)
- Installing websockets (8.1)
- Installing pyppeteer (0.2.2)
pytest-asyncio
因爲我們的測試代碼是asyncio的,所有我們需要將測試用例當成一個協程來處理,pytest-asyncio插件可以幫助我們實現這個效果。
安裝pytest-asyncio:
PS G:\luizyao\rafiki> poetry add pytest-asyncio
Using version ^0.14.0 for pytest-asyncio
Updating dependencies
Resolving dependencies...
Writing lock file
Package operations: 1 install, 0 updates, 0 removals
- Installing pytest-asyncio (0.14.0)
總結
本文我們確定了最核心的第三方庫---- pyppeteer,後面我們就結合pytest一步步定製我們自己的測試框架。