playwright文档说明

playwright官方文档翻译


安装

<h1>安装</h1> <p>剧作家是专门为满足端到端测试的需求而创建的。Playwright支持所有现代渲染引擎,包括Chromium,WebKit和Firefox。在本地或 CI 上在 Windows、Linux 和 macOS 上进行测试,在无头或以本机移动仿真为标题。</p> <p>Playwright建议使用官方<a href="https://playwright.dev/python/docs/test-runners">Playwright Pytest插件</a>来编写端到端测试。它提供上下文隔离,开箱即用地在多个浏览器配置上运行它。或者,您可以使用<a href="https://playwright.dev/python/docs/library">该库</a>通过首选测试运行程序手动编写测试基础结构。Pytest插件使用Playwright的同步版本,还有一个可通过库访问的异步版本。</p> <p>首先安装 Playwright 并运行示例测试以查看其运行情况。</p> <p>安装<a href="https://pypi.org/project/pytest-playwright/">Pytest 插件</a>:</p> <pre><code>pip install pytest-playwright</code></pre> <p>安装所需的浏览器:</p> <pre><code>playwright install</code></pre> <h2>添加示例测试<a href="https://playwright.dev/python/docs/intro#add-example-test" title="Direct link to heading"></a></h2> <p>使用以下代码在当前工作目录或子目录中创建一个文件:<code>test_my_application.py</code></p> <p>import re from playwright.sync_api import Page, expect</p> <p>def test_homepage_has_Playwright_in_title_and_get_started_link_linking_to_the_intro_page(page: Page): page.goto(&quot;<a href="https://playwright.dev/">https://playwright.dev/</a>&quot;)</p> <pre><code># Expect a title "to contain" a substring. expect(page).to_have_title(re.compile("Playwright")) # create a locator get_started = page.get_by_role("link", name="Get started") # Expect an attribute "to be strictly equal" to the value. expect(get_started).to_have_attribute("href", "/docs/intro") # Click the get started link. get_started.click() # Expects the URL to contain intro. expect(page).to_have_url(re.compile(".*intro"))</code></pre> <h2>运行示例测试<a href="https://playwright.dev/python/docs/intro#running-the-example-test" title="Direct link to heading"></a></h2> <p>默认情况下,测试将在铬上运行。这可以通过 CLI 选项进行配置。测试以无头模式运行,这意味着在运行测试时不会打开浏览器 UI。测试结果和测试日志将显示在终端中。</p> <pre><code>pytest</code></pre>

页面列表

ITEM_HTML