python:playwright结合pytest执行用例

本文介绍了如何结合Playwright和Pytest编写端到端的Web应用程序测试。内容包括安装pytest插件、编写测试用例、忽略HTTPS错误和设置自定义视口大小,以及如何实现持久上下文。通过示例代码和命令行指令,展示了如何运行测试并模拟手机浏览器。此外,还讨论了持久上下文在测试用例执行中的作用,以及用户数据目录和区域设置的相关参数。
摘要由CSDN通过智能技术生成

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):自动完成配置类型 ( pagepytest_playwright插件提供的一个夹函数,page指定到类型为PagePageplaywright库提供的一个类,page指定类型为Page就可以调用Page类中的一些列方法 )

命令行输入命令执行:

PS F:\project_gitee\Test\playwrightProject\cases> pytest --browser chromium --video on
==============================================================
首先,需要安装 PythonPlaywrightpytest 扩展: ``` pip install playwright pytest-playwright ``` 然后,在 Python 中编写测试用例,使用 Playwright 进行浏览器自动化测试。以下是一个简单的示例: ```python import pytest from playwright.sync_api import Playwright, Browser, BrowserContext, Page @pytest.fixture(scope="session") def playwright() -> Playwright: with Playwright() as playwright: yield playwright @pytest.fixture(scope="session") def browser(playwright: Playwright) -> Browser: with playwright.chromium.launch(headless=True) as browser: yield browser @pytest.fixture(scope="session") def context(browser: Browser) -> BrowserContext: with browser.new_context() as context: yield context @pytest.fixture(scope="session") def page(context: BrowserContext) -> Page: with context.new_page() as page: yield page def test_playwright(page: Page): page.goto("https://www.google.com") assert page.title() == "Google" search_input = page.locator("[name='q']") search_input.fill("Playwright") search_input.press("Enter") assert page.title() == "Playwright - Google Search" ``` 在这个示例中,我们定义了四个 fixture,分别是: - `playwright`:Playwright 实例,用于创建浏览器实例。 - `browser`:浏览器实例,用于创建上下文。 - `context`:浏览器上下文,用于创建页面实例。 - `page`:页面实例,用于进行测试操作。 在测试函数 `test_playwright` 中,我们使用 Playwright 进行浏览器自动化测试。我们首先打开谷歌搜索页面,然后在搜索框中输入 `Playwright` 并按下回车键。最后,我们断言页面标题是否为 `Playwright - Google Search`。 最后,我们可以使用 pytest 运行测试: ``` pytest test_playwright.py ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值