Playwright 是一个功能强大的自动化测试工具,它可以帮助测试人员轻松测试各种 Web 应用程序。Playwright 的一个重要功能是 codegen,该功能允许用户通过手动操作浏览器来自动生成测试代码,从而节省编写测试代码的时间和精力。本文将详细介绍如何使用 Playwright codegen 生成测试代码,包括如何录制用户操作、生成测试代码以及运行测试
安装Playwright和Playwright Codegen
在使用Playwright Codegen之前,你需要先安装Playwright和相关的Python依赖。你可以通过以下命令来安装:
pip install playwright
playwright install
playwright install
命令会下载并安装Playwright所需的浏览器二进制文件,包括Chromium、Firefox和WebKit等。
接下来,你还需要安装Playwright Codegen。不过需要注意的是,Playwright Codegen实际上并不需要单独安装一个Python包,它是Playwright CLI的一部分。因此,只要你安装了Playwright,就可以使用Playwright Codegen了。
启动Playwright Codegen
使用Playwright Codegen生成测试代码非常简单。你只需要在命令行中执行以下命令:
playwright codegen [URL]
这里的[URL]
是你想要生成代码的网站的URL。例如,如果你想要生成对Google主页的测试代码,可以使用以下命令:
playwright codegen https://www.google.com
执行该命令后,Playwright会打开一个浏览器窗口,并自动导航到你指定的URL。此时,你可以开始在浏览器中进行交互操作,比如点击按钮、输入文本等。Playwright会实时记录你的操作,并在命令行中生成对应的测试代码。
当你完成所有操作之后,按下Ctrl+C或直接关闭浏览器窗口,以停止录制。此时,Playwright会在当前目录下生成一个名为playwright
的文件夹,里面包含了生成的测试代码文件。
录制用户操作
在浏览器窗口中,打开你想要录制脚本的网站,并在页面上进行操作。例如,你可以访问一个在线表单页面,填写表单并提交。Playwright 将自动记录你的操作,并在 Playwright Inspector 窗口中生成对应的脚本。
你可以执行以下操作:
- 打开浏览器窗口,输入你想要测试的网站的 URL。
- 在页面上进行点击、输入文本等操作。
- Playwright 将记录你的每一步操作,并生成相应的测试代码。
例如,假设你访问了一个简单的搜索页面,并在搜索框中输入“test”,然后点击搜索按钮。Playwright 将生成类似以下的测试代码:
import re
from playwright.sync_api import Playwright, sync_playwright, expect
def run(playwright: Playwright) -> None:
browser = playwright.chromium.launch(headless=False)
context = browser.new_context()
page = context.new_page()
page.goto("https://www.baidu.com/")
page.locator("#kw").click()
page.locator("#kw").fill("东汉末年出bug")
page.locator("#kw").press("Enter")
page.get_by_role("button", name="百度一下").click()
page.locator("#content_left").click()
# ---------------------
context.close()
browser.close()
with sync_playwright() as playwright:
run(playwright)
生成测试代码
Playwright Codegen 会自动生成测试代码,并将其显示在 Playwright Inspector 窗口中。生成的测试代码包括所有你手动执行的操作,如点击、输入文本、导航等。你可以将生成的测试代码保存到文件中,以便后续使用。
你可以使用以下命令将生成的测试代码保存到文件中:
playwright codegen -o script.py
运行测试
将生成的测试代码保存到文件后,你可以使用 Playwright CLI 运行测试。运行以下命令来执行测试:
python script.py
Playwright 将读取 script.py
文件中的测试代码,并在指定的浏览器中执行测试。测试结果将显示在终端中,包括每个测试步骤的成功或失败状态。
高级功能
Playwright Codegen 还提供了许多高级功能,允许你定制生成的测试代码和定位器。以下是一些常用的高级功能:
-
指定生成代码的目标语言:你可以使用
--target
选项指定生成代码的目标语言,如 JavaScript、TypeScript、Python 等。playwright codegen --target=python-async -o script.py
-
指定要使用的浏览器:你可以使用
--browser
选项指定要使用的浏览器,如 Chromium、Firefox、WebKit 等。playwright codegen --browser=firefox -o script.py
-
模拟设备:你可以使用
--device
选项模拟不同的设备,如 iPhone、Pixel 等。playwright codegen --device=iPhone11 -o script.py
-
指定地理位置:你可以使用
--geolocation
选项指定地理位置坐标。playwright codegen --geolocation="37.819722,-122.478611" -o script.py
-
模拟颜色方案:你可以使用
--color-scheme
选项模拟首选颜色方案,如浅色模式或深色模式。playwright codegen --color-scheme=dark -o script.py
-
忽略 HTTPS 错误:你可以使用
--ignore-https-errors
选项忽略 HTTPS 错误。playwright codegen --ignore-https-errors -o script.py
-
指定代理服务器:你可以使用
--proxy-server
选项指定代理服务器。playwright codegen --proxy-server="http://myproxy:3128" -o script.py
-
保存和加载上下文存储状态:你可以使用
--load-storage
和--save-storage
选项保存和加载上下文存储状态,如 cookies 和 localStorage。playwright codegen --save-storage=state.json -o script.py playwright codegen --load-storage=state.json -o script.py