playwright结合Pytest为您的 Web 应用程序编写端到端的测试。
安装pytest插件
C:\Users\lifeng01>pip install pytest-playwright
Collecting pytest-playwright
Using cached pytest_playwright-0.2.2-py3-none-any.whl (9.8 kB)
Requirement already satisfied: pytest in d:\python\python37\lib\site-packages (from pytest-playwright) (6.2.5)
Requirement already satisfied: playwright>=1.13 in d:\python\python37\lib\site-packages (from pytest-playwright) (1.17.0)
Requirement already satisfied: python-slugify in d:\python\python37\lib\site-packages (from pytest-playwright) (5.0.2)
Requirement already satisfied: pytest-base-url in d:\python\python37\lib\site-packages (from pytest-playwright) (1.4.2)
Requirement already satisfied: typing-extensions in d:\python\python37\lib\site-packages (from playwright>=1.13->pytest-playwright) (3.7.4.3)
Requirement already satisfied: pyee>=8.0.1 in d:\python\python37\lib\site-packages (from playwright>=1.13->pytest-playwright) (8.2.2)
Requirement already satisfied: websockets>=8.1 in d:\python\python37\lib\site-packages (from playwright>=1.13->pytest-playwright) (9.1)
Requirement already satisfied: greenlet>=1.0.0 in d:\python\python37\lib\site-packages (from playwright>=1.13->pytest-playwright) (1.0.0)
Requirement already satisfied: atomicwrites>=1.0 in d:\python\python37\lib\site-packages (from pytest->pytest-playwright) (1.3.0)
Requirement already satisfied: attrs>=19.2.0 in d:\python\python37\lib\site-packages (from pytest->pytest-playwright) (19.3.0)
Requirement already satisfied: py>=1.8.2 in d:\python\python37\lib\site-packages (from pytest->pytest-playwright) (1.9.0)
Requirement already satisfied: toml in d:\python\python37\lib\site-packages (from pytest->pytest-playwright) (0.10.0)
Requirement already satisfied: colorama in d:\python\python37\lib\site-packages (from pytest->pytest-playwright) (0.4.3)
Requirement already satisfied: pluggy<2.0,>=0.12 in d:\python\python37\lib\site-packages (from pytest->pytest-playwright) (0.13.1)
Requirement already satisfied: packaging in d:\python\python37\lib\site-packages (from pytest->pytest-playwright) (20.9)
Requirement already satisfied: importlib-metadata>=0.12 in d:\python\python37\lib\site-packages (from pytest->pytest-playwright) (1.7.0)
Requirement already satisfied: iniconfig in d:\python\python37\lib\site-packages (from pytest->pytest-playwright) (1.0.1)
Requirement already satisfied: requests>=2.9 in d:\python\python37\lib\site-packages (from pytest-base-url->pytest-playwright) (2.25.0)
Requirement already satisfied: text-unidecode>=1.3 in d:\python\python37\lib\site-packages (from python-slugify->pytest-playwright) (1.3)
Requirement already satisfied: zipp>=0.5 in d:\python\python37\lib\site-packages (from importlib-metadata>=0.12->pytest->pytest-playwright) (3.1.0)
Requirement already satisfied: urllib3<1.27,>=1.21.1 in d:\python\python37\lib\site-packages (from requests>=2.9->pytest-base-url->pytest-playwright) (1.26.2)
Requirement already satisfied: certifi>=2017.4.17 in d:\python\python37\lib\site-packages (from requests>=2.9->pytest-base-url->pytest-playwright) (2020.4.5.1)
Requirement already satisfied: idna<3,>=2.5 in d:\python\python37\lib\site-packages (from requests>=2.9->pytest-base-url->pytest-playwright) (2.9)
Requirement already satisfied: chardet<4,>=3.0.2 in d:\python\python37\lib\site-packages (from requests>=2.9->pytest-base-url->pytest-playwright) (3.0.4)
Requirement already satisfied: pyparsing>=2.0.2 in d:\python\python37\lib\site-packages (from packaging->pytest->pytest-playwright) (2.4.7)
Installing collected packages: pytest-playwright
Successfully installed pytest-playwright-0.2.2
编写测试用例
import pytest
from playwright.sync_api import Page
def test_baidu_com(page: Page):
page.goto("https://www.baidu.com")
# xpath定位输入框,输入文本内容
page.fill('//*[@id="kw"]', "秦时明月")
# css点位点击“百度一下”按钮
page.click('#su')
# 打印title名称
print(page.title)
if __name__ == '__main__':
pytest.main(["-v", "test_baidu_example.py"])
名词解释:
test_baidu_com(page: Page):
自动完成配置类型 (page
是pytest_playwright
插件提供的一个夹函数,page
指定到类型为Page
,Page
是playwright
库提供的一个类,page
指定类型为Page
就可以调用Page
类中的一些列方法 )
命令行输入命令执行:
PS F:\project_gitee\Test\playwrightProject\cases> pytest --browser chromium --video on
==============================================================