从今天起,咱们从零开始完整地实践一个Web的自动化测试项目。javascript
新建虚拟环境
当咱们开始一个新的项目时,使用虚拟环境是一个良好的习惯。管理虚拟环境的工具备不少,既有python3.3开始内置的venv模块,也有第三方工具pipenv、poetry等。这里,咱们选择poetry。html
首先,新建项目,命名为rafiki。java
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来自于纪录片塞伦盖蒂中一只狒狒的名字。python
poetry默认将pytest做为dev依赖添加到项目的配置文件中:git
# pyproject.toml [tool.poetry.dependencies] python = "^3.8" [tool.poetry.dev-dependencies] pytest = "^5.2" 由于pytest是咱们的主要依赖,因此咱们将上述部分修改为:github
# pyproject.toml [tool.poetry.dependencies] python = "^3.8" pytest = "^6.0" [tool.poetry.dev-dependencies] 目前pytest的最新版本是6.0.0。web
而后,安装虚拟环境:chrome
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了。shell
最后,查看咱们建立的虚拟环境的信息:浏览器
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一步步定制咱们本身的测试框架。