发行说明
版本 1.52
¥Version 1.52
高亮
¥Highlights
-
新的方法 assertThat(locator).containsClass(),以符合人机工程学的方式在元素上声明单独的类名。
¥New method assertThat(locator).containsClass() to ergonomically assert individual class names on the element.
assertThat(page.getByRole(AriaRole.LISTITEM, new Page.GetByRoleOptions().setName("Ship v1.52"))).containsClass("done");
-
Aria 快照 新增两个属性:
/children
用于严格匹配,/url
用于链接。¥Aria Snapshots got two new properties:
/children
for strict matching and/url
for links.assertThat(locator).toMatchAriaSnapshot("""
- list
- /children: equal
- listitem: Feature A
- listitem:
- link "Feature B":
- /url: "https://playwright.nodejs.cn"
""");
杂项
¥Miscellaneous
-
APIRequest.newContext() 中的新选项 setMaxRedirects 用于控制最大重定向次数。
¥New option setMaxRedirects in APIRequest.newContext() to control the maximum number of redirects.
-
Locator.ariaSnapshot() 中的新选项 setRef 用于为快照中的每个元素生成引用,稍后可用于定位元素。
¥New option setRef in Locator.ariaSnapshot() to generate reference for each element in the snapshot which can later be used to locate the element.
重大变化
¥Breaking Changes
-
像 Page.route() 这样的方法中的 Glob URL 模式不再支持
?
和[]
。我们建议改用正则表达式。¥Glob URL patterns in methods like Page.route() do not support
?
and[]
anymore. We recommend using regular expressions instead. -
方法 Route.resume() 不再允许覆盖
Cookie
标头。如果提供了Cookie
标头,它将被忽略,并从浏览器的 Cookie 存储中加载 Cookie。要设置自定义 Cookie,请使用 BrowserContext.addCookies()。¥Method Route.resume() does not allow to override the
Cookie
header anymore. If aCookie
header is provided, it will be ignored, and the cookie will be loaded from the browser's cookie store. To set custom cookies, use BrowserContext.addCookies(). -
macOS 13 现已弃用,将不再接收 WebKit 更新。请升级到较新的 macOS 版本以继续受益于最新的 WebKit 改进。
¥macOS 13 is now deprecated and will no longer receive WebKit updates. Please upgrade to a more recent macOS version to continue benefiting from the latest WebKit improvements.
浏览器版本
¥Browser Versions
-
Chromium 136.0.7103.25
-
Mozilla Firefox 137.0
-
WebKit 18.4
该版本还针对以下稳定渠道进行了测试:
¥This version was also tested against the following stable channels:
-
Google Chrome 135
-
Microsoft Edge 135
版本 1.51
¥Version 1.51
高亮
¥Highlights
-
BrowserContext.storageState() 的新选项 setIndexedDB 允许保存和恢复 IndexedDB 内容。当你的应用使用 IndexedDB API 存储身份验证令牌(如 Firebase 身份验证)时很有用。
¥New option setIndexedDB for BrowserContext.storageState() allows to save and restore IndexedDB contents. Useful when your application uses IndexedDB API to store authentication tokens, like Firebase Authentication.
以下是 身份验证指南 之后的示例:
¥Here is an example following the authentication guide:
// Save storage state into the file. Make sure to include IndexedDB.
context.storageState(new BrowserContext.StorageStateOptions()
.setPath(Paths.get("state.json"))
.setIndexedDB(true));
// Create a new context with the saved storage state.
BrowserContext context = browser.newContext(new Browser.NewContextOptions()
.setStorageStatePath(Paths.get("state.json"))); -
Locator.filter() 的新选项 setVisible 允许仅匹配可见元素。
¥New option setVisible for Locator.filter() allows matching only visible elements.
// Ignore invisible todo items.
Locator todoItems = page.getByTestId("todo-item")
.filter(new Locator.FilterOptions().setVisible(true));
// Check there are exactly 3 visible ones.
assertThat(todoItems).hasCount(3); -
方法 Page.emulateMedia() 和 Browser.newContext() 的新选项
setContrast
允许模拟prefers-contrast
媒体功能。¥New option
setContrast
for methods Page.emulateMedia() and Browser.newContext() allows to emulate theprefers-contrast
media feature. -
新选项 setFailOnStatusCode 使通过 APIRequestContext 发送的所有获取请求都抛出除 2xx 和 3xx 之外的响应代码。
¥New option setFailOnStatusCode makes all fetch requests made through the APIRequestContext throw on response codes other than 2xx and 3xx.
浏览器版本
¥Browser Versions
-
Chromium 134.0.6998.35
-
Mozilla Firefox 135.0
-
WebKit 18.4
该版本还针对以下稳定渠道进行了测试:
¥This version was also tested against the following stable channels:
-
Google Chrome 133
-
Microsoft Edge 133
版本 1.50
¥Version 1.50
杂项
¥Miscellaneous
-
添加了方法 assertThat(locator).hasAccessibleErrorMessage() 来断言定位器指向具有给定 咏叹调错误消息 的元素。
¥Added method assertThat(locator).hasAccessibleErrorMessage() to assert the Locator points to an element with a given aria errormessage.
UI 更新
¥UI updates
-
Codegen 中的新按钮用于选择元素以生成 aria 快照。
¥New button in Codegen for picking elements to produce aria snapshots.
-
其他详细信息(例如按下的键)现在与跟踪中的操作 API 调用一起显示。
¥Additional details (such as keys pressed) are now displayed alongside action API calls in traces.
-
在跟踪中显示
canvas
内容容易出错。显示现在默认禁用,可以通过Display canvas content
UI 设置启用。¥Display of
canvas
content in traces is error-prone. Display is now disabled by default, and can be enabled via theDisplay canvas content
UI setting. -
Call
和Network
面板现在显示额外的时间信息。¥
Call
andNetwork
panels now display additional time information.
重点
¥Breaking
-
如果目标元素不是
<input>
、<select>
或其他可编辑元素,则 assertThat(locator).isEditable() 和 Locator.isEditable() 现在会抛出异常。¥assertThat(locator).isEditable() and Locator.isEditable() now throw if the target element is not
<input>
,<select>
, or a number of other editable elements.
浏览器版本
¥Browser Versions
-
Chromium 133.0.6943.16
-
Mozilla Firefox 134.0
-
WebKit 18.2
该版本还针对以下稳定渠道进行了测试:
¥This version was also tested against the following stable channels:
-
Google Chrome 132
-
Microsoft Edge 132
版本 1.49
¥Version 1.49
Aria 快照
¥Aria snapshots
新断言 assertThat(locator).matchesAriaSnapshot() 通过与预期的可访问性树(以 YAML 表示)进行比较来验证页面结构。
¥New assertion assertThat(locator).matchesAriaSnapshot() verifies page structure by comparing to an expected accessibility tree, represented as YAML.
page.navigate("https://playwright.nodejs.cn");
assertThat(page.locator("body")).matchesAriaSnapshot("""
- banner:
- heading /Playwright enables reliable/ [level=1]
- link "Get started"
- link "Star microsoft/playwright on GitHub"
- main:
- img "Browsers (Chromium, Firefox, WebKit)"
- heading "Any browser • Any platform • One API"
""");
你可以使用 测试生成器 或调用 Locator.ariaSnapshot() 来生成此断言。
¥You can generate this assertion with Test Generator or by calling Locator.ariaSnapshot().
在 aria 快照指南 中了解更多信息。
¥Learn more in the aria snapshots guide.
跟踪组
¥Tracing groups
新方法 Tracing.group() 允许你在跟踪查看器中直观地对操作进行分组。
¥New method Tracing.group() allows you to visually group actions in the trace viewer.
// All actions between group and groupEnd
// will be shown in the trace viewer as a group.
page.context().tracing().group("Open Playwright.dev > API");
page.navigate("https://playwright.nodejs.cn/");
page.getByRole(AriaRole.LINK, new Page.GetByRoleOptions().setName("API")).click();
page.context().tracing().groupEnd();
重点:chrome
和 msedge
通道切换到新的无头模式
¥Breaking: chrome
and msedge
channels switch to new headless mode
如果你在 playwright.config.ts
中使用以下通道之一,则此更改会影响你:
¥This change affects you if you're using one of the following channels in your playwright.config.ts
:
-
chrome
、chrome-dev
、chrome-beta
或chrome-canary
¥
chrome
,chrome-dev
,chrome-beta
, orchrome-canary
-
msedge
、msedge-dev
、msedge-beta
或msedge-canary
¥
msedge
,msedge-dev
,msedge-beta
, ormsedge-canary
更新到 Playwright v1.49 后,运行你的测试套件。如果仍然通过,你就可以开始了。如果没有,你可能需要更新快照,并围绕 PDF 查看器和扩展调整一些测试代码。详细信息请参见 问题 #33566。
¥After updating to Playwright v1.49, run your test suite. If it still passes, you're good to go. If not, you will probably need to update your snapshots, and adapt some of your test code around PDF viewers and extensions. See issue #33566 for more details.
尝试新的 Chromium 无头
¥Try new Chromium headless
你可以使用 'chromium'
通道选择新的无头模式。如 Chrome 官方文档介绍 所示:
¥You can opt into the new headless mode by using 'chromium'
channel. As official Chrome documentation puts it:
另一方面,New Headless 是真正的 Chrome 浏览器,因此更加真实、可靠并提供更多功能。这使其更适合高精度端到端 Web 应用测试或浏览器扩展测试。
¥New Headless on the other hand is the real Chrome browser, and is thus more authentic, reliable, and offers more features. This makes it more suitable for high-accuracy end-to-end web app testing or browser extension testing.
请参阅 问题 #33566 了解你可能遇到的可能故障列表以及有关 Chromium headless 的更多详细信息。如果你在选择加入后发现任何问题,请提交问题。
¥See issue #33566 for the list of possible breakages you could encounter and more details on Chromium headless. Please file an issue if you see any problems after opting in.
Browser browser = playwright.chromium().launch(new BrowserType.LaunchOptions().setChannel("chromium"));
杂项
¥Miscellaneous
-
Ubuntu 20.04 和 Debian 11 上的 WebKit 将不再有更新。我们建议将你的操作系统更新到更高版本。
¥There will be no more updates for WebKit on Ubuntu 20.04 and Debian 11. We recommend updating your OS to a later version.
-
快照内的
<canvas>
元素现在绘制预览。¥
<canvas>
elements inside a snapshot now draw a preview.
浏览器版本
¥Browser Versions
-
Chromium 131.0.6778.33
-
Mozilla Firefox 132.0
-
WebKit 18.2
该版本还针对以下稳定渠道进行了测试:
¥This version was also tested against the following stable channels:
-
Google Chrome 130
-
Microsoft Edge 130
版本 1.48
¥Version 1.48
WebSocket 路由
¥WebSocket routing
新方法 Page.routeWebSocket() 和 BrowserContext.routeWebSocket() 允许拦截、修改和模拟在页面中发起的 WebSocket 连接。下面是一个简单的示例,通过使用 "response"
响应 "request"
来模拟 WebSocket 通信。
¥New methods Page.routeWebSocket() and BrowserContext.routeWebSocket() allow to intercept, modify and mock WebSocket connections initiated in the page. Below is a simple example that mocks WebSocket communication by responding to a "request"
with a "response"
.
page.routeWebSocket("/ws", ws -> {
ws.onMessage(frame -> {
if ("request".equals(frame.text()))
ws.send("response");
});
});
详细信息请参见 WebSocketRoute。
¥See WebSocketRoute for more details.
UI 更新
¥UI updates
-
HTML 报告中用于注释和测试位置的新 "copy" 按钮。
¥New "copy" buttons for annotations and test location in the HTML report.
-
像 Route.fulfill() 这样的路由方法调用不再显示在报告和跟踪查看器中。你可以在网络选项卡中查看哪些网络请求被路由。
¥Route method calls like Route.fulfill() are not shown in the report and trace viewer anymore. You can see which network requests were routed in the network tab instead.
-
网络选项卡中用于请求的新 "复制为 cURL" 和 "复制为 fetch" 按钮。
¥New "Copy as cURL" and "Copy as fetch" buttons for requests in the network tab.
杂项
¥Miscellaneous
-
新方法 Page.requestGC() 可能有助于检测内存泄漏。
¥New method Page.requestGC() may help detect memory leaks.
-
APIRequestContext 发送的请求现在在 HAR 中记录详细的时间和安全信息。
¥Requests made by APIRequestContext now record detailed timing and security information in the HAR.
浏览器版本
¥Browser Versions
-
Chromium 130.0.6723.19
-
Mozilla Firefox 130.0
-
WebKit 18.0
该版本还针对以下稳定渠道进行了测试:
¥This version was also tested against the following stable channels:
-
Google Chrome 129
-
Microsoft Edge 129
版本 1.47
¥Version 1.47
网络选项卡改进
¥Network Tab improvements
跟踪查看器中的“网络”选项卡有几项不错的改进:
¥The Network tab in the trace viewer has several nice improvements:
-
按资源类型和 URL 过滤
¥filtering by asset type and URL
-
更好地显示查询字符串参数
¥better display of query string parameters
-
字体资源预览
¥preview of font assets
杂项
¥Miscellaneous
-
mcr.microsoft.com/playwright/java:v1.47.0
现在提供基于 Ubuntu 24.04 Noble 的 Playwright 图片。要使用基于 22.02 jammy 的镜像,请改用mcr.microsoft.com/playwright/java:v1.47.0-jammy
。¥The
mcr.microsoft.com/playwright/java:v1.47.0
now serves a Playwright image based on Ubuntu 24.04 Noble. To use the 22.02 jammy-based image, please usemcr.microsoft.com/playwright/java:v1.47.0-jammy
instead. -
Playwright Docker 镜像的
:latest
/:focal
/:jammy
标签已不再发布。固定到特定版本以获得更好的稳定性和可重复性。¥The
:latest
/:focal
/:jammy
tag for Playwright Docker images is no longer being published. Pin to a specific version for better stability and reproducibility. -
现在可以通过将 setClientCertificates.setCert 和 setClientCertificates.setKey 作为字节数组(而不是文件路径)传递,从内存传递 TLS 客户端证书。
¥TLS client certificates can now be passed from memory by passing setClientCertificates.setCert and setClientCertificates.setKey as byte arrays instead of file paths.
-
Locator.selectOption() 中的 setNoWaitAfter 已被弃用。
¥setNoWaitAfter in Locator.selectOption() was deprecated.
-
我们已经看到有关 Webkit 中的 WebGL 在 GitHub Actions
macos-13
上行为不当的报告。我们建议将 GitHub Actions 升级到macos-14
。¥We've seen reports of WebGL in Webkit misbehaving on GitHub Actions
macos-13
. We recommend upgrading GitHub Actions tomacos-14
.
浏览器版本
¥Browser Versions
-
Chromium 129.0.6668.29
-
Mozilla Firefox 130.0
-
WebKit 18.0
该版本还针对以下稳定渠道进行了测试:
¥This version was also tested against the following stable channels:
-
Google Chrome 128
-
Microsoft Edge 128
版本 1.46
¥Version 1.46
TLS 客户端证书
¥TLS Client Certificates
Playwright 现在允许提供客户端证书,以便服务器能够按照 TLS 客户端身份验证的要求进行验证。
¥Playwright now allows to supply client-side certificates, so that server can verify them, as specified by TLS Client Authentication.
你可以将客户端证书作为 Browser.newContext() 和 APIRequest.newContext() 的参数提供。以下代码片段为 https://example.com
设置了客户端证书:
¥You can provide client certificates as a parameter of Browser.newContext() and APIRequest.newContext(). The following snippet sets up a client certificate for https://example.com
:
BrowserContext context = browser.newContext(new Browser.NewContextOptions()
.setClientCertificates(asList(new ClientCertificate("https://example.com")
.setCertPath(Paths.get("client-certificates/cert.pem"))
.setKeyPath(Paths.get("client-certificates/key.pem")))));
跟踪查看器更新
¥Trace Viewer Updates
-
文本附件的内容现在在附件窗格中以内联方式渲染。
¥Content of text attachments is now rendered inline in the attachments pane.
-
新设置显示/隐藏路由操作,如 Route.resume()。
¥New setting to show/hide routing actions like Route.resume().
-
请求方法和状态显示在网络详细信息选项卡中。
¥Request method and status are shown in the network details tab.
-
新按钮用于将源文件位置复制到剪贴板。
¥New button to copy source file location to clipboard.
-
元数据窗格现在显示
baseURL
。¥Metadata pane now displays the
baseURL
.
杂项
¥Miscellaneous
-
APIRequestContext.fetch() 中的新
maxRetries
选项可在ECONNRESET
网络错误时重试。¥New
maxRetries
option in APIRequestContext.fetch() which retries on theECONNRESET
network error.
浏览器版本
¥Browser Versions
-
Chromium 128.0.6613.18
-
Mozilla Firefox 128.0
-
WebKit 18.0
该版本还针对以下稳定渠道进行了测试:
¥This version was also tested against the following stable channels:
-
Google Chrome 127
-
Microsoft Edge 127
版本 1.45
¥Version 1.45
时钟
¥Clock
利用新的 时钟 API 允许在测试中操纵和控制时间来验证与时间相关的行为。此 API 涵盖许多常见场景,包括:
¥Utilizing the new Clock API allows to manipulate and control time within tests to verify time-related behavior. This API covers many common scenarios, including:
-
使用预定义时间进行测试;
¥testing with predefined time;
-
保持一致的时间和计时器;
¥keeping consistent time and timers;
-
监控不活动;
¥monitoring inactivity;
-
手动计时。
¥ticking through time manually.
// Initialize clock with some time before the test time and let the page load
// naturally. `Date.now` will progress as the timers fire.
page.clock().install(new Clock.InstallOptions().setTime("2024-02-02T08:00:00"));
page.navigate("http://localhost:3333");
Locator locator = page.getByTestId("current-time");
// Pretend that the user closed the laptop lid and opened it again at 10am.
// Pause the time once reached that point.
page.clock().pauseAt("2024-02-02T10:00:00");
// Assert the page state.
assertThat(locator).hasText("2/2/2024, 10:00:00 AM");
// Close the laptop lid again and open it at 10:30am.
page.clock().fastForward("30:00");
assertThat(locator).hasText("2/2/2024, 10:30:00 AM");
详细信息请参见 时钟指南。
¥See the clock guide for more details.
杂项
¥Miscellaneous
-
方法 Locator.setInputFiles() 现在支持上传
<input type=file webkitdirectory>
元素的目录。¥Method Locator.setInputFiles() now supports uploading a directory for
<input type=file webkitdirectory>
elements.page.getByLabel("Upload directory").setInputFiles(Paths.get("mydir"));
-
现在,Locator.click() 或 Locator.press() 等多种方法都支持
ControlOrMeta
修饰键。此键在 macOS 上映射到Meta
,在 Windows 和 Linux 上映射到Control
。¥Multiple methods like Locator.click() or Locator.press() now support a
ControlOrMeta
modifier key. This key maps toMeta
on macOS and maps toControl
on Windows and Linux.// Press the common keyboard shortcut Control+S or Meta+S to trigger a "Save" operation.
page.keyboard.press("ControlOrMeta+S"); -
APIRequest.newContext() 中的新属性
httpCredentials.send
允许始终发送Authorization
标头或仅在响应401 Unauthorized
时发送它。¥New property
httpCredentials.send
in APIRequest.newContext() that allows to either always send theAuthorization
header or only send it in response to401 Unauthorized
. -
Playwright 现在支持 Ubuntu 24.04 上的 Chromium、Firefox 和 WebKit。
¥Playwright now supports Chromium, Firefox and WebKit on Ubuntu 24.04.
-
v1.45 是接收 macOS 12 Monterey 的 WebKit 更新的最后一个版本。请更新 macOS 以继续使用最新的 WebKit。
¥v1.45 is the last release to receive WebKit update for macOS 12 Monterey. Please update macOS to keep using the latest WebKit.
浏览器版本
¥Browser Versions
-
Chromium 127.0.6533.5
-
Mozilla Firefox 127.0
-
网络工具包 17.4
¥WebKit 17.4
该版本还针对以下稳定渠道进行了测试:
¥This version was also tested against the following stable channels:
-
Google Chrome 126
-
Microsoft Edge 126
版本 1.44
¥Version 1.44
新 API
¥New APIs
可访问性断言
¥Accessibility assertions
-
assertThat(locator).hasAccessibleName() 检查元素是否具有指定的可访问名称:
¥assertThat(locator).hasAccessibleName() checks if the element has the specified accessible name:
Locator locator = page.getByRole(AriaRole.BUTTON);
assertThat(locator).hasAccessibleName("Submit"); -
assertThat(locator).hasAccessibleDescription() 检查元素是否具有指定的可访问描述:
¥assertThat(locator).hasAccessibleDescription() checks if the element has the specified accessible description:
Locator locator = page.getByRole(AriaRole.BUTTON);
assertThat(locator).hasAccessibleDescription("Upload a photo"); -
assertThat(locator).hasRole() 检查元素是否具有指定的 ARIA 角色:
¥assertThat(locator).hasRole() checks if the element has the specified ARIA role:
Locator locator = page.getByTestId("save-button");
assertThat(locator).hasRole(AriaRole.BUTTON);
定位器处理程序
¥Locator handler
-
执行使用 Page.addLocatorHandler() 添加的处理程序后,Playwright 现在将等待,直到触发处理程序的覆盖不再可见。你可以使用新的
setNoWaitAfter
选项选择退出此行为。¥After executing the handler added with Page.addLocatorHandler(), Playwright will now wait until the overlay that triggered the handler is not visible anymore. You can opt-out of this behavior with the new
setNoWaitAfter
option. -
你可以在 Page.addLocatorHandler() 中使用新的
setTimes
选项来指定处理程序应运行的最大次数。¥You can use new
setTimes
option in Page.addLocatorHandler() to specify maximum number of times the handler should be run. -
Page.addLocatorHandler() 中的处理程序现在接受定位器作为参数。
¥The handler in Page.addLocatorHandler() now accepts the locator as argument.
-
用于删除先前添加的定位器处理程序的新 Page.removeLocatorHandler() 方法。
¥New Page.removeLocatorHandler() method for removing previously added locator handlers.
Locator locator = page.getByText("This interstitial covers the button");
page.addLocatorHandler(locator, overlay -> {
overlay.locator("#close").click();
}, new Page.AddLocatorHandlerOptions().setTimes(3).setNoWaitAfter(true));
// Run your tests that can be interrupted by the overlay.
// ...
page.removeLocatorHandler(locator);
其他选项
¥Miscellaneous options
-
新方法 FormData.append() 允许在
RequestOptions
中的setMultipart
选项中指定同名的重复字段:¥New method FormData.append() allows to specify repeating fields with the same name in
setMultipart
option inRequestOptions
:FormData formData = FormData.create();
formData.append("file", new FilePayload("f1.js", "text/javascript",
"var x = 2024;".getBytes(StandardCharsets.UTF_8)));
formData.append("file", new FilePayload("f2.txt", "text/plain",
"hello".getBytes(StandardCharsets.UTF_8)));
APIResponse response = context.request().post("https://example.com/uploadFile", RequestOptions.create().setMultipart(formData));
expect(page).toHaveURL(url)
now supportssetIgnoreCase
option.
浏览器版本
¥Browser Versions
-
Chromium 125.0.6422.14
-
Mozilla Firefox 125.0.1
-
网络工具包 17.4
¥WebKit 17.4
该版本还针对以下稳定渠道进行了测试:
¥This version was also tested against the following stable channels:
-
Google Chrome 124
-
Microsoft Edge 124
1.43 版本
¥Version 1.43
新 API
¥New APIs
-
方法 BrowserContext.clearCookies() 现在支持过滤器以仅删除一些 cookie。
¥Method BrowserContext.clearCookies() now supports filters to remove only some cookies.
// Clear all cookies.
context.clearCookies();
// New: clear cookies with a particular name.
context.clearCookies(new BrowserContext.ClearCookiesOptions().setName("session-id"));
// New: clear cookies for a particular domain.
context.clearCookies(new BrowserContext.ClearCookiesOptions().setDomain("my-origin.com")); -
新方法 Locator.contentFrame() 将 Locator 对象转换为 FrameLocator。当你在某处获得了 Locator 对象,并且稍后想要与框架内的内容进行交互时,这会很有用。
¥New method Locator.contentFrame() converts a Locator object to a FrameLocator. This can be useful when you have a Locator object obtained somewhere, and later on would like to interact with the content inside the frame.
Locator locator = page.locator("iframe[name='embedded']");
// ...
FrameLocator frameLocator = locator.contentFrame();
frameLocator.getByRole(AriaRole.BUTTON).click(); -
新方法 FrameLocator.owner() 将 FrameLocator 对象转换为 Locator。当你在某处获得了 FrameLocator 对象,并且稍后想要与
iframe
元素进行交互时,这会很有用。¥New method FrameLocator.owner() converts a FrameLocator object to a Locator. This can be useful when you have a FrameLocator object obtained somewhere, and later on would like to interact with the
iframe
element.FrameLocator frameLocator = page.frameLocator("iframe[name='embedded']");
// ...
Locator locator = frameLocator.owner();
assertThat(locator).isVisible();
浏览器版本
¥Browser Versions
-
Chromium 124.0.6367.8
-
火狐浏览器 124.0
¥Mozilla Firefox 124.0
-
网络工具包 17.4
¥WebKit 17.4
该版本还针对以下稳定渠道进行了测试:
¥This version was also tested against the following stable channels:
-
谷歌浏览器 123
¥Google Chrome 123
-
微软边缘 123
¥Microsoft Edge 123
1.42 版本
¥Version 1.42
实验性 JUnit 集成
¥Experimental JUnit integration
在测试类中添加新的 @UsePlaywright
注解,以便在测试方法中开始使用 Playwright 的 Page、BrowserContext、Browser、APIRequestContext 和 Playwright 夹具。
¥Add new @UsePlaywright
annotation to your test classes to start using Playwright fixtures for Page, BrowserContext, Browser, APIRequestContext and Playwright in the test methods.
package org.example;
import com.microsoft.playwright.Page;
import com.microsoft.playwright.junit.UsePlaywright;
import org.junit.jupiter.api.Test;
import static com.microsoft.playwright.assertions.PlaywrightAssertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
@UsePlaywright
public class TestExample {
void shouldNavigateToInstallationGuide(Page page) {
page.navigate("https://playwright.nodejs.cn/java/");
page.getByRole(AriaRole.LINK, new Page.GetByRoleOptions().setName("Docs")).click();
assertThat(page.getByRole(AriaRole.HEADING, new Page.GetByRoleOptions().setName("Installation"))).isVisible();
}
@Test
void shouldCheckTheBox(Page page) {
page.setContent("<input id='checkbox' type='checkbox'></input>");
page.locator("input").check();
assertEquals(true, page.evaluate("window['checkbox'].checked"));
}
@Test
void shouldSearchWiki(Page page) {
page.navigate("https://www.wikipedia.org/");
page.locator("input[name=\"search\"]").click();
page.locator("input[name=\"search\"]").fill("playwright");
page.locator("input[name=\"search\"]").press("Enter");
assertThat(page).hasURL("https://en.wikipedia.org/wiki/Playwright");
}
}
在上面的示例中,所有三个测试方法都使用同一个 Browser。每个测试使用自己的 BrowserContext 和 Page。
¥In the example above, all three test methods use the same Browser. Each test uses its own BrowserContext and Page.
自定义选项
¥Custom options
实现你自己的 OptionsFactory
实例,并使用自定义配置初始化 Fixture。
¥Implement your own OptionsFactory
to initialize the fixtures with custom configuration.
import com.microsoft.playwright.junit.Options;
import com.microsoft.playwright.junit.OptionsFactory;
import com.microsoft.playwright.junit.UsePlaywright;
@UsePlaywright(MyTest.CustomOptions.class)
public class MyTest {
public static class CustomOptions implements OptionsFactory {
@Override
public Options getOptions() {
return new Options()
.setHeadless(false)
.setContextOption(new Browser.NewContextOptions()
.setBaseURL("https://github.com"))
.setApiRequestOptions(new APIRequest.NewContextOptions()
.setBaseURL("https://playwright.nodejs.cn"));
}
}
@Test
public void testWithCustomOptions(Page page, APIRequestContext request) {
page.navigate("/");
assertThat(page).hasURL(Pattern.compile("github"));
APIResponse response = request.get("/");
assertTrue(response.text().contains("Playwright"));
}
}
了解更多关于我们 JUnit 指南 中的 Fixture 的信息。
¥Learn more about the fixtures in our JUnit guide.
新的定位器处理程序
¥New Locator Handler
新方法 Page.addLocatorHandler() 注册一个回调,当指定元素变得可见时将调用该回调,并且可能会阻止 Playwright 操作。回调可以摆脱覆盖。以下是一个在 Cookie 对话框出现时关闭该对话框的示例。
¥New method Page.addLocatorHandler() registers a callback that will be invoked when specified element becomes visible and may block Playwright actions. The callback can get rid of the overlay. Here is an example that closes a cookie dialog when it appears.
// Setup the handler.
page.addLocatorHandler(
page.getByRole(AriaRole.BUTTON, new Page.GetByRoleOptions().setName("Hej! You are in control of your cookies.")),
() -> {
page.getByRole(AriaRole.BUTTON, new Page.GetByRoleOptions().setName("Accept all")).click();
});
// Write the test as usual.
page.navigate("https://www.ikea.com/");
page.getByRole(AriaRole.LINK, new Page.GetByRoleOptions().setName("Collection of blue and white")).click();
assertThat(page.getByRole(AriaRole.HEADING, new Page.GetByRoleOptions().setName("Light and easy"))).isVisible();
新 API
¥New APIs
-
Page.pdf() 接受两个新选项 setTagged 和 setOutline。
¥Page.pdf() accepts two new options setTagged and setOutline.
公告
¥Announcements
-
⚠️ Ubuntu 18 不再受支持。
¥⚠️ Ubuntu 18 is not supported anymore.
浏览器版本
¥Browser Versions
-
Chromium 123.0.6312.4
-
火狐浏览器 123.0
¥Mozilla Firefox 123.0
-
网络工具包 17.4
¥WebKit 17.4
该版本还针对以下稳定渠道进行了测试:
¥This version was also tested against the following stable channels:
-
谷歌浏览器 122
¥Google Chrome 122
-
微软边缘 123
¥Microsoft Edge 123
1.41 版本
¥Version 1.41
新 API
¥New APIs
-
新方法 Page.unrouteAll() 删除了 Page.route() 和 Page.routeFromHAR() 注册的所有路由。
¥New method Page.unrouteAll() removes all routes registered by Page.route() and Page.routeFromHAR().
-
新方法 BrowserContext.unrouteAll() 删除了 BrowserContext.route() 和 BrowserContext.routeFromHAR() 注册的所有路由。
¥New method BrowserContext.unrouteAll() removes all routes registered by BrowserContext.route() and BrowserContext.routeFromHAR().
-
Page.screenshot() 中的新选项 setStyle 和 Locator.screenshot() 中的新选项 setStyle 可在截屏前将自定义 CSS 添加到页面。
¥New options setStyle in Page.screenshot() and setStyle in Locator.screenshot() to add custom CSS to the page before taking a screenshot.
浏览器版本
¥Browser Versions
-
Chromium 121.0.6167.57
-
火狐浏览器 121.0
¥Mozilla Firefox 121.0
-
网络工具包 17.4
¥WebKit 17.4
该版本还针对以下稳定渠道进行了测试:
¥This version was also tested against the following stable channels:
-
谷歌浏览器 120
¥Google Chrome 120
-
微软 Edge 120
¥Microsoft Edge 120
1.40 版本
¥Version 1.40
测试生成器更新
¥Test Generator Update
生成断言的新工具:
¥New tools to generate assertions:
-
"断言可见性" 工具生成 assertThat(locator).isVisible()。
¥"Assert visibility" tool generates assertThat(locator).isVisible().
-
"断言值" 工具生成 assertThat(locator).hasValue()。
¥"Assert value" tool generates assertThat(locator).hasValue().
-
"断言文本" 工具生成 assertThat(locator).containsText()。
¥"Assert text" tool generates assertThat(locator).containsText().
以下是带有断言的生成测试的示例:
¥Here is an example of a generated test with assertions:
page.navigate("https://playwright.nodejs.cn/");
page.getByRole(AriaRole.LINK, new Page.GetByRoleOptions().setName("Get started")).click();
assertThat(page.getByLabel("Breadcrumbs").getByRole(AriaRole.LIST)).containsText("Installation");
assertThat(page.getByLabel("Search")).isVisible();
page.getByLabel("Search").click();
page.getByPlaceholder("Search docs").fill("locator");
assertThat(page.getByPlaceholder("Search docs")).hasValue("locator");
新 API
¥New APIs
-
Page.close() 中的选项 setReason、BrowserContext.close() 中的选项 setReason 和 Browser.close() 中的选项 setReason。报告因关闭而中断的所有操作的关闭原因。
¥Options setReason in Page.close(), setReason in BrowserContext.close() and setReason in Browser.close(). Close reason is reported for all operations interrupted by the closure.
-
BrowserType.launchPersistentContext() 中的选项 setFirefoxUserPrefs。
¥Option setFirefoxUserPrefs in BrowserType.launchPersistentContext().
其他变更
¥Other Changes
-
方法 Download.path() 和 Download.createReadStream() 会因下载失败和取消而引发错误。
¥Methods Download.path() and Download.createReadStream() throw an error for failed and cancelled downloads.
浏览器版本
¥Browser Versions
-
Chromium 120.0.6099.28
-
火狐浏览器 119.0
¥Mozilla Firefox 119.0
-
网络工具包 17.4
¥WebKit 17.4
该版本还针对以下稳定渠道进行了测试:
¥This version was also tested against the following stable channels:
-
谷歌浏览器 119
¥Google Chrome 119
-
微软 Edge 119
¥Microsoft Edge 119
1.39 版本
¥Version 1.39
常青浏览器更新。
¥Evergreen browsers update.
浏览器版本
¥Browser Versions
-
Chromium 119.0.6045.9
-
火狐浏览器 118.0.1
¥Mozilla Firefox 118.0.1
-
网络工具包 17.4
¥WebKit 17.4
该版本还针对以下稳定渠道进行了测试:
¥This version was also tested against the following stable channels:
-
谷歌浏览器 118
¥Google Chrome 118
-
微软 Edge 118
¥Microsoft Edge 118
1.38 版本
¥Version 1.38
跟踪查看器更新
¥Trace Viewer Updates
-
放大时间范围。
¥Zoom into time range.
-
网络面板重新设计。
¥Network panel redesign.
新 API
¥New APIs
弃用
¥Deprecations
-
以下方法已被弃用:Page.type()、Frame.type()、Locator.type() 和 ElementHandle.type()。请使用 Locator.fill(),这样速度更快。仅当页面上有特殊键盘处理并且需要逐个按键时才使用 Locator.pressSequentially()。
¥The following methods were deprecated: Page.type(), Frame.type(), Locator.type() and ElementHandle.type(). Please use Locator.fill() instead which is much faster. Use Locator.pressSequentially() only if there is a special keyboard handling on the page, and you need to press keys one-by-one.
浏览器版本
¥Browser Versions
-
Chromium 117.0.5938.62
-
火狐浏览器 117.0
¥Mozilla Firefox 117.0
-
网络工具包 17.0
¥WebKit 17.0
该版本还针对以下稳定渠道进行了测试:
¥This version was also tested against the following stable channels:
-
谷歌浏览器 116
¥Google Chrome 116
-
微软 Edge 116
¥Microsoft Edge 116
1.37 版本
¥Version 1.37
新 API
¥New APIs
-
新方法 BrowserContext.newCDPSession() 和 Browser.newBrowserCDPSession() 分别为页面和浏览器创建 Chrome 开发者工具协议 会话。
¥New methods BrowserContext.newCDPSession() and Browser.newBrowserCDPSession() create a Chrome DevTools Protocol session for the page and browser respectively.
CDPSession cdpSession = page.context().newCDPSession(page);
cdpSession.send("Runtime.enable");
JsonObject params = new JsonObject();
params.addProperty("expression", "window.foo = 'bar'");
cdpSession.send("Runtime.evaluate", params);
Object foo = page.evaluate("window['foo']");
assertEquals("bar", foo);
📚 Debian 12 Bookworm 支持
¥📚 Debian 12 Bookworm Support
Playwright 现在支持在 x86_64 和 arm64 上运行 Chromium、Firefox 和 WebKit 的 Debian 12 Bookworm。如果你遇到任何问题,请告诉我们!
¥Playwright now supports Debian 12 Bookworm on both x86_64 and arm64 for Chromium, Firefox and WebKit. Let us know if you encounter any issues!
Linux 支持如下所示:
¥Linux support looks like this:
乌班图 20.04 | 乌班图 22.04 | Debian 11 | Debian 12 | |
---|---|---|---|---|
Chromium | ✅ | ✅ | ✅ | ✅ |
WebKit | ✅ | ✅ | ✅ | ✅ |
火狐浏览器 | ✅ | ✅ | ✅ | ✅ |
浏览器版本
¥Browser Versions
-
Chromium 116.0.5845.82
-
火狐浏览器 115.0
¥Mozilla Firefox 115.0
-
网络工具包 17.0
¥WebKit 17.0
该版本还针对以下稳定渠道进行了测试:
¥This version was also tested against the following stable channels:
-
谷歌浏览器 115
¥Google Chrome 115
-
微软 Edge 115
¥Microsoft Edge 115
1.36 版本
¥Version 1.36
🏝️ 夏季维护版本。
¥🏝️ Summer maintenance release.
浏览器版本
¥Browser Versions
-
Chromium 115.0.5790.75
-
火狐浏览器 115.0
¥Mozilla Firefox 115.0
-
网络工具包 17.0
¥WebKit 17.0
该版本还针对以下稳定渠道进行了测试:
¥This version was also tested against the following stable channels:
-
谷歌浏览器 114
¥Google Chrome 114
-
微软 Edge 114
¥Microsoft Edge 114
1.35 版本
¥Version 1.35
高亮
¥Highlights
-
方法 Page.screenshot() 和 Locator.screenshot() 新增选项
maskColor
,用于更改默认遮罩颜色。¥New option
maskColor
for methods Page.screenshot() and Locator.screenshot() to change default masking color. -
用于卸载浏览器二进制文件的新
uninstall
CLI 命令:¥New
uninstall
CLI command to uninstall browser binaries:$ mvn exec:java -e -D exec.mainClass=com.microsoft.playwright.CLI -D exec.args="uninstall" # remove browsers installed by this installation
$ mvn exec:java -e -D exec.mainClass=com.microsoft.playwright.CLI -D exec.args="uninstall --all" # remove all ever-install Playwright browsers
浏览器版本
¥Browser Versions
-
Chromium 115.0.5790.13
-
火狐浏览器 113.0
¥Mozilla Firefox 113.0
-
网络工具包 16.4
¥WebKit 16.4
该版本还针对以下稳定渠道进行了测试:
¥This version was also tested against the following stable channels:
-
谷歌浏览器 114
¥Google Chrome 114
-
微软 Edge 114
¥Microsoft Edge 114
1.34 版本
¥Version 1.34
高亮
¥Highlights
-
新建 Locator.and() 以创建与两个定位器匹配的定位器。
¥New Locator.and() to create a locator that matches both locators.
Locator button = page.getByRole(AriaRole.BUTTON).and(page.getByTitle("Subscribe"));
-
新事件 BrowserContext.onConsoleMessage(handler) 和 BrowserContext.onDialog(handler) 用于订阅给定浏览器上下文中任何页面的任何对话框和控制台消息。使用新方法 ConsoleMessage.page() 和 Dialog.page() 来查明事件源。
¥New events BrowserContext.onConsoleMessage(handler) and BrowserContext.onDialog(handler) to subscribe to any dialogs and console messages from any page from the given browser context. Use the new methods ConsoleMessage.page() and Dialog.page() to pin-point event source.
浏览器版本
¥Browser Versions
-
Chromium 114.0.5735.26
-
火狐浏览器 113.0
¥Mozilla Firefox 113.0
-
网络工具包 16.4
¥WebKit 16.4
该版本还针对以下稳定渠道进行了测试:
¥This version was also tested against the following stable channels:
-
谷歌浏览器 113
¥Google Chrome 113
-
微软 Edge 113
¥Microsoft Edge 113
1.33 版本
¥Version 1.33
定位器更新
¥Locators Update
-
使用 Locator.or() 创建与两个定位器之一匹配的定位器。考虑这样一个场景:你想要单击 "新邮件" 按钮,但有时会显示安全设置对话框。在这种情况下,你可以等待 "新邮件" 按钮或对话框并进行相应操作:
¥Use Locator.or() to create a locator that matches either of the two locators. Consider a scenario where you'd like to click on a "New email" button, but sometimes a security settings dialog shows up instead. In this case, you can wait for either a "New email" button, or a dialog and act accordingly:
Locator newEmail = page.getByRole(AriaRole.BUTTON, new Page.GetByRoleOptions().setName("New email"));
Locator dialog = page.getByText("Confirm security settings");
assertThat(newEmail.or(dialog)).isVisible();
if (dialog.isVisible())
page.getByRole(AriaRole.BUTTON, new Page.GetByRoleOptions().setName("Dismiss")).click();
newEmail.click(); -
使用 Locator.filter() 中的新选项 setHasNot 和 setHasNotText 来查找不符合某些条件的元素。
¥Use new options setHasNot and setHasNotText in Locator.filter() to find elements that do not match certain conditions.
Locator rowLocator = page.locator("tr");
rowLocator
.filter(new Locator.FilterOptions().setHasNotText("text in column 1"))
.filter(new Locator.FilterOptions().setHasNot(
page.getByRole(AriaRole.BUTTON,
new Page.GetByRoleOptions().setName("column 2 button" ))))
.screenshot(); -
使用新的 Web 优先断言 assertThat(locator).isAttached() 确保该元素存在于页面的 DOM 中。不要与确保该元素既附加又可见的 assertThat(locator).isVisible() 混淆。
¥Use new web-first assertion assertThat(locator).isAttached() to ensure that the element is present in the page's DOM. Do not confuse with the assertThat(locator).isVisible() that ensures that element is both attached & visible.
新 API
¥New APIs
-
Locator.filter() 中的新选项 setHasNot
¥New option setHasNot in Locator.filter()
-
Locator.filter() 中的新选项 setHasNotText
¥New option setHasNotText in Locator.filter()
-
Route.fetch() 中的新选项 setTimeout
¥New option setTimeout in Route.fetch()
其他亮点
¥Other highlights
-
原生支持 Apple Silicon - Playwright 现在无需 Rosetta 即可运行
¥Native support for Apple Silicon - Playwright now runs without Rosetta
-
添加了 Ubuntu 22.04 (Jammy) Docker 镜像
¥Added Ubuntu 22.04 (Jammy) Docker image
⚠️ 重大改变
¥⚠️ Breaking change
-
mcr.microsoft.com/playwright/java:v1.33.0
现在提供基于 Ubuntu Jammy 的 Playwright 映像。要使用基于焦点的图片,请改用mcr.microsoft.com/playwright/java:v1.33.0-focal
。¥The
mcr.microsoft.com/playwright/java:v1.33.0
now serves a Playwright image based on Ubuntu Jammy. To use the focal-based image, please usemcr.microsoft.com/playwright/java:v1.33.0-focal
instead.
浏览器版本
¥Browser Versions
-
Chromium 113.0.5672.53
-
火狐浏览器 112.0
¥Mozilla Firefox 112.0
-
网络工具包 16.4
¥WebKit 16.4
该版本还针对以下稳定渠道进行了测试:
¥This version was also tested against the following stable channels:
-
谷歌浏览器 112
¥Google Chrome 112
-
微软 Edge 112
¥Microsoft Edge 112
1.32 版本
¥Version 1.32
新 API
¥New APIs
-
Page.routeFromHAR() 和 BrowserContext.routeFromHAR() 中的新选项 setUpdateMode 和 setUpdateContent。
¥New options setUpdateMode and setUpdateContent in Page.routeFromHAR() and BrowserContext.routeFromHAR().
-
链接现有定位器对象,请参阅 定位器文档 了解详细信息。
¥Chaining existing locator objects, see locator docs for details.
-
方法 Tracing.startChunk() 中的新选项 setName。
¥New option setName in method Tracing.startChunk().
浏览器版本
¥Browser Versions
-
Chromium 112.0.5615.29
-
火狐浏览器 111.0
¥Mozilla Firefox 111.0
-
网络工具包 16.4
¥WebKit 16.4
该版本还针对以下稳定渠道进行了测试:
¥This version was also tested against the following stable channels:
-
谷歌浏览器 111
¥Google Chrome 111
-
微软 Edge 111
¥Microsoft Edge 111
1.31 版本
¥Version 1.31
新 API
¥New APIs
-
根据 交集监视器 API,新断言 assertThat(locator).isInViewport() 确保定位器指向与视口相交的元素。
¥New assertion assertThat(locator).isInViewport() ensures that locator points to an element that intersects viewport, according to the intersection observer API.
Locator locator = page.getByRole(AriaRole.BUTTON);
// Make sure at least some part of element intersects viewport.
assertThat(locator).isInViewport();
// Make sure element is fully outside of viewport.
assertThat(locator).not().isInViewport();
// Make sure that at least half of the element intersects viewport.
assertThat(locator).isInViewport(new LocatorAssertions.IsInViewportOptions().setRatio(0.5));
杂项
¥Miscellaneous
-
现在可以在单独的窗口中打开跟踪查看器中的 DOM 快照。
¥DOM snapshots in trace viewer can be now opened in a separate window.
-
方法 Route.fetch() 的新选项 setMaxRedirects。
¥New option setMaxRedirects for method Route.fetch().
-
Playwright 现在支持 Debian 11 arm64。
¥Playwright now supports Debian 11 arm64.
-
官方 docker 图片 现在包括 Node 18,而不是 Node 16。
¥Official docker images now include Node 18 instead of Node 16.
浏览器版本
¥Browser Versions
-
Chromium 111.0.5563.19
-
火狐浏览器 109.0
¥Mozilla Firefox 109.0
-
网络工具包 16.4
¥WebKit 16.4
该版本还针对以下稳定渠道进行了测试:
¥This version was also tested against the following stable channels:
-
谷歌浏览器 110
¥Google Chrome 110
-
微软 Edge 110
¥Microsoft Edge 110
1.30 版本
¥Version 1.30
浏览器版本
¥Browser Versions
-
Chromium 110.0.5481.38
-
火狐浏览器 108.0.2
¥Mozilla Firefox 108.0.2
-
网络工具包 16.4
¥WebKit 16.4
该版本还针对以下稳定渠道进行了测试:
¥This version was also tested against the following stable channels:
-
谷歌浏览器 109
¥Google Chrome 109
-
微软 Edge 109
¥Microsoft Edge 109
1.29 版本
¥Version 1.29
新 API
¥New APIs
-
新方法 Route.fetch():
¥New method Route.fetch():
page.route("**/api/settings", route -> {
// Fetch original settings.
APIResponse response = route.fetch();
// Force settings theme to a predefined value.
String body = response.text().replace("\"theme\":\"default\"",
"\"theme\":\"Solorized\"");
// Fulfill with modified data.
route.fulfill(new Route.FulfillOptions().setResponse(response).setBody(body));
}); -
用于迭代所有匹配元素的新方法 Locator.all():
¥New method Locator.all() to iterate over all matching elements:
// Check all checkboxes!
Locator checkboxes = page.getByRole(AriaRole.CHECKBOX);
for (Locator checkbox : checkboxes.all())
checkbox.check(); -
Locator.selectOption() 现在按值或标签匹配:
¥Locator.selectOption() matches now by value or label:
<select multiple>
<option value="red">Red</option>
<option value="green">Green</option>
<option value="blue">Blue</option>
</select>element.selectOption("Red");
浏览器版本
¥Browser Versions
-
Chromium 109.0.5414.46
-
火狐浏览器 107.0
¥Mozilla Firefox 107.0
-
网络工具包 16.4
¥WebKit 16.4
该版本还针对以下稳定渠道进行了测试:
¥This version was also tested against the following stable channels:
-
谷歌浏览器 108
¥Google Chrome 108
-
微软 Edge 108
¥Microsoft Edge 108
1.28 版本
¥Version 1.28
Playwright 工具
¥Playwright Tools
-
CodeGen 中的实时定位器。使用 "探索" 工具为页面上的任何元素生成定位器。
¥Live Locators in CodeGen. Generate a locator for any element on the page using "Explore" tool.
新 API
¥New APIs
浏览器版本
¥Browser Versions
-
Chromium 108.0.5359.29
-
火狐浏览器 106.0
¥Mozilla Firefox 106.0
-
网络工具包 16.4
¥WebKit 16.4
该版本还针对以下稳定渠道进行了测试:
¥This version was also tested against the following stable channels:
-
谷歌浏览器 107
¥Google Chrome 107
-
微软 Edge 107
¥Microsoft Edge 107
1.27 版本
¥Version 1.27
定位器
¥Locators
使用这些新的 API 编写定位器是一种乐趣:
¥With these new APIs writing locators is a joy:
-
Page.getByText() 按文本内容定位。
¥Page.getByText() to locate by text content.
-
Page.getByRole() 通过 ARIA 角色、ARIA 属性 和 可访问的名称 定位。
¥Page.getByRole() to locate by ARIA role, ARIA attributes and accessible name.
-
Page.getByLabel() 通过关联标签的文本定位表单控件。
¥Page.getByLabel() to locate a form control by associated label's text.
-
Page.getByTestId() 根据
data-testid
属性定位元素(可以配置其他属性)。¥Page.getByTestId() to locate an element based on its
data-testid
attribute (other attribute can be configured). -
Page.getByPlaceholder() 通过占位符定位输入。
¥Page.getByPlaceholder() to locate an input by placeholder.
-
Page.getByAltText() 通过文本替代来定位元素(通常是图片)。
¥Page.getByAltText() to locate an element, usually image, by its text alternative.
-
Page.getByTitle() 通过标题定位元素。
¥Page.getByTitle() to locate an element by its title.
page.getByLabel("User Name").fill("John");
page.getByLabel("Password").fill("secret-password");
page.getByRole(AriaRole.BUTTON, new Page.GetByRoleOptions().setName("Sign in")).click();
assertThat(page.getByText("Welcome, John!")).isVisible();
所有相同的方法也可用于 Locator、FrameLocator 和 Frame 类。
¥All the same methods are also available on Locator, FrameLocator and Frame classes.
其他亮点
¥Other highlights
-
正如 v1.25 中所宣布的,自 2022 年 12 月起将不再支持 Ubuntu 18。除此之外,从下一个 Playwright 版本开始,Ubuntu 18 将不再有 WebKit 更新。
¥As announced in v1.25, Ubuntu 18 will not be supported as of Dec 2022. In addition to that, there will be no WebKit updates on Ubuntu 18 starting from the next Playwright release.
行为改变
¥Behavior Changes
-
具有空值的 assertThat(locator).hasAttribute() 不再与缺失的属性匹配。例如,当
button
没有disabled
属性时,以下代码片段将会成功。¥assertThat(locator).hasAttribute() with an empty value does not match missing attribute anymore. For example, the following snippet will succeed when
button
does not have adisabled
attribute.assertThat(page.getByRole(AriaRole.BUTTON)).hasAttribute("disabled", "");
浏览器版本
¥Browser Versions
-
Chromium 107.0.5304.18
-
火狐浏览器 105.0.1
¥Mozilla Firefox 105.0.1
-
网络工具包 16.0
¥WebKit 16.0
该版本还针对以下稳定渠道进行了测试:
¥This version was also tested against the following stable channels:
-
谷歌浏览器 106
¥Google Chrome 106
-
微软 Edge 106
¥Microsoft Edge 106
1.26 版本
¥Version 1.26
断言
¥Assertions
-
assertThat(locator).isEnabled() 的新选项
enabled
。¥New option
enabled
for assertThat(locator).isEnabled(). -
assertThat(locator).hasText() 现在刺穿开放的暗影根部。
¥assertThat(locator).hasText() now pierces open shadow roots.
-
assertThat(locator).isEditable() 的新选项
editable
。¥New option
editable
for assertThat(locator).isEditable(). -
assertThat(locator).isVisible() 的新选项
visible
。¥New option
visible
for assertThat(locator).isVisible().
其他亮点
¥Other highlights
-
用于 APIRequestContext.get() 和其他限制重定向计数的新选项
setMaxRedirects
。¥New option
setMaxRedirects
for APIRequestContext.get() and others to limit redirect count. -
Docker 镜像现在使用 OpenJDK 17。
¥Docker images are now using OpenJDK 17.
行为改变
¥Behavior Change
许多 Playwright API 已经支持 setWaitUntil(WaitUntilState.DOMCONTENTLOADED)
选项。例如:
¥A bunch of Playwright APIs already support the setWaitUntil(WaitUntilState.DOMCONTENTLOADED)
option. For example:
page.navigate("https://playwright.nodejs.cn", new Page.NavigateOptions().setWaitUntil(WaitUntilState.DOMCONTENTLOADED));
在 1.26 之前,这将等待所有 iframe 触发 DOMContentLoaded
事件。
¥Prior to 1.26, this would wait for all iframes to fire the DOMContentLoaded
event.
为了与 Web 规范保持一致,WaitUntilState.DOMCONTENTLOADED
值仅等待目标框架触发 'DOMContentLoaded'
事件。使用 setWaitUntil(WaitUntilState.LOAD)
等待所有 iframe。
¥To align with web specification, the WaitUntilState.DOMCONTENTLOADED
value only waits for the target frame to fire the 'DOMContentLoaded'
event. Use setWaitUntil(WaitUntilState.LOAD)
to wait for all iframes.
浏览器版本
¥Browser Versions
-
Chromium 106.0.5249.30
-
火狐浏览器 104.0
¥Mozilla Firefox 104.0
-
网络工具包 16.0
¥WebKit 16.0
该版本还针对以下稳定渠道进行了测试:
¥This version was also tested against the following stable channels:
-
谷歌浏览器 105
¥Google Chrome 105
-
微软 Edge 105
¥Microsoft Edge 105
1.25 版本
¥Version 1.25
新的 API 和更改
¥New APIs & changes
-
现在可以使用
setDefaultAssertionTimeout
更改默认断言超时。¥Default assertions timeout now can be changed with
setDefaultAssertionTimeout
.
公告
¥Announcements
-
🪦 这是支持 macOS 10.15 的最后一个版本(自 1.21 起已弃用)。
¥🪦 This is the last release with macOS 10.15 support (deprecated as of 1.21).
-
⚠️ Ubuntu 18 现已弃用,自 2022 年 12 月起将不再受支持。
¥⚠️ Ubuntu 18 is now deprecated and will not be supported as of Dec 2022.
浏览器版本
¥Browser Versions
-
Chromium 105.0.5195.19
-
火狐浏览器 103.0
¥Mozilla Firefox 103.0
-
网络工具包 16.0
¥WebKit 16.0
该版本还针对以下稳定渠道进行了测试:
¥This version was also tested against the following stable channels:
-
谷歌浏览器 104
¥Google Chrome 104
-
微软 Edge 104
¥Microsoft Edge 104
1.24 版本
¥Version 1.24
🐂 Debian 11 Bullseye 支持
¥🐂 Debian 11 Bullseye Support
Playwright 现在支持 x86_64 上的 Debian 11 Bullseye,适用于 Chromium、Firefox 和 WebKit。如果你遇到任何问题,请告诉我们!
¥Playwright now supports Debian 11 Bullseye on x86_64 for Chromium, Firefox and WebKit. Let us know if you encounter any issues!
Linux 支持如下所示:
¥Linux support looks like this:
| | 乌班图 20.04 | 乌班图 22.04 | Debian 11 | :--- | :---:| :---:| :---:| :---:| | Chromium | ✅ | ✅ | ✅ | | WebKit | ✅ | ✅ | ✅ | | Firefox | ✅ | ✅ | ✅ |
¥ | Ubuntu 20.04 | Ubuntu 22.04 | Debian 11 | |
---|---|---|---|---|
Chromium | ✅ | ✅ | ✅ | |
WebKit | ✅ | ✅ | ✅ | |
Firefox | ✅ | ✅ | ✅ |
1.23 版本
¥Version 1.23
网络重播
¥Network Replay
现在,你可以将网络流量记录到 HAR 文件中,并在测试中重复使用此流量。
¥Now you can record network traffic into a HAR file and re-use this traffic in your tests.
将网络记录到 HAR 文件中:
¥To record network into HAR file:
mvn exec:java -e -D exec.mainClass=com.microsoft.playwright.CLI -D exec.args="open --save-har=example.har --save-har-glob='**/api/**' https://example.com"
或者,你可以通过编程方式记录 HAR:
¥Alternatively, you can record HAR programmatically:
BrowserContext context = browser.newContext(new Browser.NewContextOptions()
.setRecordHarPath(Paths.get("example.har"))
.setRecordHarUrlFilter("**/api/**"));
// ... Perform actions ...
// Close context to ensure HAR is saved to disk.
context.close();
使用新方法 Page.routeFromHAR() 或 BrowserContext.routeFromHAR() 提供来自 HAR 文件的匹配响应:
¥Use the new methods Page.routeFromHAR() or BrowserContext.routeFromHAR() to serve matching responses from the HAR file:
context.routeFromHAR(Paths.get("example.har"));
阅读 我们的文档 中的更多内容。
¥Read more in our documentation.
高级路由
¥Advanced Routing
你现在可以使用 Route.fallback() 推迟路由到其他处理程序。
¥You can now use Route.fallback() to defer routing to other handlers.
考虑以下示例:
¥Consider the following example:
// Remove a header from all requests.
page.route("**/*", route -> {
Map<String, String> headers = new HashMap<>(route.request().headers());
headers.remove("X-Secret");
route.resume(new Route.ResumeOptions().setHeaders(headers));
});
// Abort all images.
page.route("**/*", route -> {
if ("image".equals(route.request().resourceType()))
route.abort();
else
route.fallback();
});
请注意,新方法 Page.routeFromHAR() 和 BrowserContext.routeFromHAR() 也参与路由并且可以推迟。
¥Note that the new methods Page.routeFromHAR() and BrowserContext.routeFromHAR() also participate in routing and could be deferred to.
Web 优先断言更新
¥Web-First Assertions Update
-
新方法 assertThat(locator).hasValues() 断言
<select multiple>
元素的所有选定值。¥New method assertThat(locator).hasValues() that asserts all selected values of
<select multiple>
element. -
方法 assertThat(locator).containsText() 和 assertThat(locator).hasText() 现在接受
ignoreCase
选项。¥Methods assertThat(locator).containsText() and assertThat(locator).hasText() now accept
ignoreCase
option.
杂项
¥Miscellaneous
-
如果有一个 Service Worker 挡住了你的路,你现在可以使用新的上下文选项
serviceWorkers
轻松禁用它:¥If there's a service worker that's in your way, you can now easily disable it with a new context option
serviceWorkers
:BrowserContext context = browser.newContext(new Browser.NewContextOptions()
.setServiceWorkers(ServiceWorkerPolicy.BLOCK)); -
使用
.zip
路径作为recordHar
上下文选项会自动压缩生成的 HAR:¥Using
.zip
path forrecordHar
context option automatically zips the resulting HAR:BrowserContext context = browser.newContext(new Browser.NewContextOptions()
.setRecordHarPath(Paths.get("example.har.zip"))); -
如果你打算手动编辑 HAR,请考虑使用
"minimal"
HAR 记录模式,该模式仅记录重放所必需的信息:¥If you intend to edit HAR by hand, consider using the
"minimal"
HAR recording mode that only records information that is essential for replaying:BrowserContext context = browser.newContext(new Browser.NewContextOptions()
.setRecordHarPath(Paths.get("example.har"))
.setRecordHarMode(HarMode.MINIMAL)); -
Playwright 现在可以在 Ubuntu 22 amd64 和 Ubuntu 22 arm64 上运行。
¥Playwright now runs on Ubuntu 22 amd64 and Ubuntu 22 arm64.
1.22 版本
¥Version 1.22
高亮
¥Highlights
-
允许按 ARIA 角色、ARIA 属性 和 可访问的名称 选择元素的角色选择器。
¥Role selectors that allow selecting elements by their ARIA role, ARIA attributes and accessible name.
// Click a button with accessible name "log in"
page.locator("role=button[name='log in']").click();阅读 我们的文档 中的更多内容。
¥Read more in our documentation.
-
用于过滤现有定位器的新 Locator.filter() API
¥New Locator.filter() API to filter an existing locator
Locator buttonsLocator = page.locator("role=button");
// ...
Locator submitButton = buttonsLocator.filter(new Locator.FilterOptions().setHasText("Submit"));
submitButton.click(); -
Playwright for Java 现已支持 Ubuntu 20.04 ARM64 和 Apple M1。你现在可以在 Apple M1、Apple M1 上的 Docker 内部以及 Raspberry Pi 上运行 Playwright for Java 测试。
¥Playwright for Java now supports Ubuntu 20.04 ARM64 and Apple M1. You can now run Playwright for Java tests on Apple M1, inside Docker on Apple M1, and on Raspberry Pi.
1.21 版本
¥Version 1.21
高亮
¥Highlights
-
新的角色选择器允许通过 ARIA 角色、ARIA 属性 和 可访问的名称 选择元素。
¥New role selectors that allow selecting elements by their ARIA role, ARIA attributes and accessible name.
// Click a button with accessible name "log in"
page.locator("role=button[name='log in']").click();阅读 我们的文档 中的更多内容。
¥Read more in our documentation.
-
Page.screenshot() 中的新
scale
选项适用于较小尺寸的屏幕截图。¥New
scale
option in Page.screenshot() for smaller sized screenshots. -
Page.screenshot() 中的新
caret
选项用于控制文本插入符。默认为"hide"
。¥New
caret
option in Page.screenshot() to control text caret. Defaults to"hide"
.
行为改变
¥Behavior Changes
-
Playwright 现在支持通过 Locator.setInputFiles() API 上传大文件(数百 MB)。
¥Playwright now supports large file uploads (100s of MBs) via Locator.setInputFiles() API.
浏览器版本
¥Browser Versions
-
Chromium 101.0.4951.26
-
火狐浏览器 98.0.2
¥Mozilla Firefox 98.0.2
-
网络工具包 15.4
¥WebKit 15.4
该版本还针对以下稳定渠道进行了测试:
¥This version was also tested against the following stable channels:
-
谷歌浏览器 100
¥Google Chrome 100
-
微软 Edge 100
¥Microsoft Edge 100
1.20 版本
¥Version 1.20
高亮
¥Highlights
-
方法 Page.screenshot()、Locator.screenshot() 和 ElementHandle.screenshot() 的新选项:
¥New options for methods Page.screenshot(), Locator.screenshot() and ElementHandle.screenshot():
-
选项
ScreenshotAnimations.DISABLED
倒回所有 CSS 动画并转换到一致的状态¥Option
ScreenshotAnimations.DISABLED
rewinds all CSS animations and transitions to a consistent state -
选项
mask: Locator[]
屏蔽给定元素,用粉红色#FF00FF
框覆盖它们。¥Option
mask: Locator[]
masks given elements, overlaying them with pink#FF00FF
boxes.
-
-
¥Trace Viewer now shows API testing requests.
-
Locator.highlight() 直观地显示元素以便于调试。
¥Locator.highlight() visually reveals element(s) for easier debugging.
公告
¥Announcements
-
v1.20 是接收 macOS 10.15 Catalina WebKit 更新的最后一个版本。请更新 macOS 以继续使用最新和最好的 WebKit!
¥v1.20 is the last release to receive WebKit update for macOS 10.15 Catalina. Please update macOS to keep using latest & greatest WebKit!
浏览器版本
¥Browser Versions
-
Chromium 101.0.4921.0
-
火狐浏览器 97.0.1
¥Mozilla Firefox 97.0.1
-
网络工具包 15.4
¥WebKit 15.4
该版本还针对以下稳定渠道进行了测试:
¥This version was also tested against the following stable channels:
-
谷歌浏览器 99
¥Google Chrome 99
-
微软 Edge 99
¥Microsoft Edge 99
1.19 版本
¥Version 1.19
高亮
¥Highlights
-
定位器现在支持
has
选项,确保它内部包含另一个定位器:¥Locator now supports a
has
option that makes sure it contains another locator inside:page.locator("article", new Page.LocatorOptions().setHas(page.locator(".highlight"))).click();
阅读 定位器文档 的更多内容
¥Read more in locator documentation
-
¥New Locator.page()
-
Page.screenshot() 和 Locator.screenshot() 现在自动隐藏闪烁的插入符号
¥Page.screenshot() and Locator.screenshot() now automatically hide blinking caret
-
Playwright Codegen 现在生成定位器和帧定位器
¥Playwright Codegen now generates locators and frame locators
浏览器版本
¥Browser Versions
-
Chromium 100.0.4863.0
-
火狐浏览器 96.0.1
¥Mozilla Firefox 96.0.1
-
网络工具包 15.4
¥WebKit 15.4
该版本还针对以下稳定渠道进行了测试:
¥This version was also tested against the following stable channels:
-
谷歌浏览器 98
¥Google Chrome 98
-
微软 Edge 98
¥Microsoft Edge 98
1.18 版本
¥Version 1.18
API 测试
¥API Testing
Playwright for Java 1.18 引入了新的 API 测试,可让你直接从 Java 向服务器发送请求!现在你可以:
¥Playwright for Java 1.18 introduces new API Testing that lets you send requests to the server directly from Java! Now you can:
-
测试你的服务器 API
¥test your server API
-
在测试中访问 Web 应用之前准备服务器端状态
¥prepare server side state before visiting the web application in a test
-
在浏览器中运行某些操作后验证服务器端后置条件
¥validate server side post-conditions after running some actions in the browser
要代表 Playwright 页面触发请求,请使用新的 Page.request() API:
¥To do a request on behalf of Playwright's Page, use new Page.request() API:
// Do a GET request on behalf of page
APIResponse res = page.request().get("http://example.com/foo.json");
在我们的 API 测试指南 中了解更多相关信息。
¥Read more about it in our API testing guide.
Web 优先断言
¥Web-First Assertions
Playwright for Java 1.18 引入了 Web 优先断言。
¥Playwright for Java 1.18 introduces Web-First Assertions.
考虑以下示例:
¥Consider the following example:
import static com.microsoft.playwright.assertions.PlaywrightAssertions.assertThat;
public class TestExample {
@Test
void statusBecomesSubmitted() {
// ...
page.locator("#submit-button").click();
assertThat(page.locator(".status")).hasText("Submitted");
}
}
Playwright 将使用选择器 .status
重新测试节点,直到获取的节点具有 "Submitted"
文本。它将重新获取节点并一遍又一遍地检查它,直到满足条件或达到超时。你可以将此超时作为一个选项传递。
¥Playwright will be re-testing the node with the selector .status
until fetched Node has the "Submitted"
text. It will be re-fetching the node and checking it over and over, until the condition is met or until the timeout is reached. You can pass this timeout as an option.
阅读 我们的文档 中的更多内容。
¥Read more in our documentation.
定位器改进
¥Locator Improvements
-
现在可以选择通过每个定位器包含的文本来过滤每个定位器:
¥Each locator can now be optionally filtered by the text it contains:
page.locator("li", new Page.LocatorOptions().setHasText("my item"))
.locator("button").click();阅读 定位器文档 的更多内容
¥Read more in locator documentation
跟踪改进
¥Tracing Improvements
Tracing 现在可以使用新的 setSources
选项将 Java 源代码嵌入到记录的跟踪中。
¥Tracing now can embed Java sources to recorded traces, using new setSources
option.
新的 API 和更改
¥New APIs & changes
-
acceptDownloads
选项现在默认为true
。¥
acceptDownloads
option now defaults totrue
.
浏览器版本
¥Browser Versions
-
Chromium 99.0.4812.0
-
火狐浏览器 95.0
¥Mozilla Firefox 95.0
-
网络工具包 15.4
¥WebKit 15.4
该版本还针对以下稳定渠道进行了测试:
¥This version was also tested against the following stable channels:
-
谷歌浏览器 97
¥Google Chrome 97
-
微软 Edge 97
¥Microsoft Edge 97
1.17 版本
¥Version 1.17
框架定位器
¥Frame Locators
Playwright 1.17 引入 帧定位器 - 页面上 iframe 的定位器。帧定位器捕获足以检索 iframe
的逻辑,然后定位该 iframe 中的元素。默认情况下,帧定位器是严格的,将等待 iframe
出现,并且可以在 Web 优先断言中使用。
¥Playwright 1.17 introduces frame locators - a locator to the iframe on the page. Frame locators capture the logic sufficient to retrieve the iframe
and then locate elements in that iframe. Frame locators are strict by default, will wait for iframe
to appear and can be used in Web-First assertions.
帧定位器可以使用 Page.frameLocator() 或 Locator.frameLocator() 方法创建。
¥Frame locators can be created with either Page.frameLocator() or Locator.frameLocator() method.
Locator locator = page.frameLocator("#my-frame").locator("text=Submit");
locator.click();
欲了解更多内容,请访问 我们的文档。
¥Read more at our documentation.
跟踪查看器更新
¥Trace Viewer Update
Playwright Trace Viewer 现已在 https://trace.playwright.dev 在线提供!只需拖放 trace.zip
文件即可检查其内容。
¥Playwright Trace Viewer is now available online at https://trace.playwright.dev! Just drag-and-drop your trace.zip
file to inspect its contents.
注意:跟踪文件没有上传到任何地方;trace.playwright.dev 是本地处理跟踪的 渐进式网络应用。
¥NOTE: trace files are not uploaded anywhere; trace.playwright.dev is a progressive web application that processes traces locally.
-
Playwright 测试跟踪现在默认包括源(可以使用跟踪选项关闭这些源)
¥Playwright Test traces now include sources by default (these could be turned off with tracing option)
-
跟踪查看器现在显示测试名称
¥Trace Viewer now shows test name
-
包含浏览器详细信息的新跟踪元数据选项卡
¥New trace metadata tab with browser details
-
快照现在有 URL 栏
¥Snapshots now have URL bar
HTML 报告更新
¥HTML Report Update
-
HTML 报告现在支持动态过滤
¥HTML report now supports dynamic filtering
-
报告现在是一个静态 HTML 文件,可以通过电子邮件或作为 Slack 附件发送。
¥Report is now a single static HTML file that could be sent by e-mail or as a slack attachment.
Ubuntu ARM64 支持及更多
¥Ubuntu ARM64 support + more
-
Playwright 现在支持 Ubuntu 20.04 ARM64。现在,你可以在 Apple M1 和 Raspberry Pi 上的 Docker 内运行 Playwright 测试。
¥Playwright now supports Ubuntu 20.04 ARM64. You can now run Playwright tests inside Docker on Apple M1 and on Raspberry Pi.
-
你现在可以使用 Playwright 在 Linux 上安装稳定版本的 Edge:
¥You can now use Playwright to install stable version of Edge on Linux:
mvn exec:java -e -D exec.mainClass=com.microsoft.playwright.CLI -D exec.args="install msedge"
新 API
¥New APIs
-
跟踪现在支持
'title'
选项¥Tracing now supports a
'title'
option -
页面导航支持新的
'commit'
等待选项¥Page navigations support a new
'commit'
waiting option
1.16 版本
¥Version 1.16
🎭 Playwright 库
¥🎭 Playwright Library
Locator.waitFor
等待定位器解析为具有给定状态的单个元素。默认为 state: 'visible'
。
¥Wait for a locator to resolve to a single element with a given state. Defaults to the state: 'visible'
.
Locator orderSent = page.locator("#order-sent");
orderSent.waitFor();
了解有关 Locator.waitFor() 的更多信息。
¥Read more about Locator.waitFor().
🎭 Playwright 踪迹查看器
¥🎭 Playwright Trace Viewer
-
使用
mvn exec:java -e -D exec.mainClass=com.microsoft.playwright.CLI -D exec.args="show-trace"
运行跟踪查看器并将跟踪文件拖放到跟踪查看器 PWA¥run trace viewer with
mvn exec:java -e -D exec.mainClass=com.microsoft.playwright.CLI -D exec.args="show-trace"
and drop trace files to the trace viewer PWA -
更好地对行动目标进行视觉归因
¥better visual attribution of action targets
了解有关 跟踪查看器 的更多信息。
¥Read more about Trace Viewer.
浏览器版本
¥Browser Versions
-
Chromium 97.0.4666.0
-
火狐浏览器 93.0
¥Mozilla Firefox 93.0
-
网络工具包 15.4
¥WebKit 15.4
此版本的 Playwright 还针对以下稳定通道进行了测试:
¥This version of Playwright was also tested against the following stable channels:
-
谷歌浏览器 94
¥Google Chrome 94
-
微软 Edge 94
¥Microsoft Edge 94
1.15 版本
¥Version 1.15
🖱️ 鼠标滚轮
¥🖱️ Mouse Wheel
通过使用 Mouse.wheel(),你现在可以垂直或水平滚动。
¥By using Mouse.wheel() you are now able to scroll vertically or horizontally.
📜 新的标头 API
¥📜 New Headers API
以前不可能获取响应的多个标头值。现在可以实现这一点,并且提供了其他辅助函数:
¥Previously it was not possible to get multiple header values of a response. This is now possible and additional helper functions are available:
🌈 强制颜色模拟
¥🌈 Forced-Colors emulation
现在可以通过在 Browser.newContext() 中传递或调用 Page.emulateMedia() 来模拟 forced-colors
CSS 媒体功能。
¥Its now possible to emulate the forced-colors
CSS media feature by passing it in the Browser.newContext() or calling Page.emulateMedia().
新 API
¥New APIs
-
Page.route() 接受新的
times
选项来指定该路由应匹配的次数。¥Page.route() accepts new
times
option to specify how many times this route should be matched. -
引入 Page.setChecked() 和 Locator.setChecked() 是为了设置复选框的选中状态。
¥Page.setChecked() and Locator.setChecked() were introduced to set the checked state of a checkbox.
-
Request.sizes() 返回给定 http 请求的资源大小信息。
¥Request.sizes() Returns resource size information for given http request.
-
Tracing.startChunk() - 开始一个新的跟踪块。
¥Tracing.startChunk() - Start a new trace chunk.
-
Tracing.stopChunk() - 停止新的跟踪块。
¥Tracing.stopChunk() - Stops a new trace chunk.
浏览器版本
¥Browser Versions
-
Chromium 96.0.4641.0
-
火狐浏览器 92.0
¥Mozilla Firefox 92.0
-
网络工具包 15.0
¥WebKit 15.0
1.14 版本
¥Version 1.14
⚡ 新 "strict" 模式
¥⚡️ New "strict" mode
选择器歧义是自动化测试中的一个常见问题。"strict" 模式确保你的选择器指向单个元素,否则抛出异常。
¥Selector ambiguity is a common problem in automation testing. "strict" mode ensures that your selector points to a single element and throws otherwise.
在你的操作调用中设置 setStrict(true)
以选择加入。
¥Set setStrict(true)
in your action calls to opt in.
// This will throw if you have more than one button!
page.click("button", new Page.ClickOptions().setStrict(true));
📍 新 定位器 API
¥📍 New Locators API
定位器表示页面上元素的视图。它捕获足以在任何给定时刻检索元素的逻辑。
¥Locator represents a view to the element(s) on the page. It captures the logic sufficient to retrieve the element at any given moment.
Locator 和 ElementHandle 之间的区别在于后者指向特定元素,而 Locator 捕获如何检索该元素的逻辑。
¥The difference between the Locator and ElementHandle is that the latter points to a particular element, while Locator captures the logic of how to retrieve that element.
另外,定位器默认为 "strict"!
¥Also, locators are "strict" by default!
Locator locator = page.locator("button");
locator.click();
在 documentation 中了解更多信息。
¥Learn more in the documentation.
🧩 实验性 React 和 Vue 选择器引擎
¥🧩 Experimental React and Vue selector engines
React 和 Vue 选择器允许通过组件名称和/或属性值来选择元素。语法与 属性选择器 非常相似,并且支持所有属性选择器运算符。
¥React and Vue selectors allow selecting elements by its component name and/or property values. The syntax is very similar to attribute selectors and supports all attribute selector operators.
page.locator("_react=SubmitButton[enabled=true]").click();
page.locator("_vue=submit-button[enabled=true]").click();
在 反应选择器文档 和 vue 选择器文档 中了解更多信息。
¥Learn more in the react selectors documentation and the vue selectors documentation.
✨ 新的 nth
和 visible
选择器引擎
¥✨ New nth
and visible
selector engines
-
nth
选择器引擎相当于:nth-match
伪类,但可以与其他选择器引擎组合。¥
nth
selector engine is equivalent to the:nth-match
pseudo class, but could be combined with other selector engines. -
visible
选择器引擎相当于:visible
伪类,但可以与其他选择器引擎组合。¥
visible
selector engine is equivalent to the:visible
pseudo class, but could be combined with other selector engines.
// select the first button among all buttons
button.click("button >> nth=0");
// or if you are using locators, you can use first(), nth() and last()
page.locator("button").first().click();
// click a visible button
button.click("button >> visible=true");
浏览器版本
¥Browser Versions
-
Chromium 94.0.4595.0
-
火狐浏览器 91.0
¥Mozilla Firefox 91.0
-
网络工具包 15.0
¥WebKit 15.0
1.13 版本
¥Version 1.13
Playwright
-
🖖 通过 Page.dragAndDrop() API 提供编程式拖放支持。
¥🖖 Programmatic drag-and-drop support via the Page.dragAndDrop() API.
-
🔎 增强型 HAR,具有请求和响应的正文大小。通过 Browser.newContext() 中的
recordHar
选项使用。¥🔎 Enhanced HAR with body sizes for requests and responses. Use via
recordHar
option in Browser.newContext().
工具
¥Tools
-
Playwright Trace Viewer 现在显示参数、返回值和
console.log()
调用。¥Playwright Trace Viewer now shows parameters, returned values and
console.log()
calls.
新的和大修的指南
¥New and Overhauled Guides
浏览器版本
¥Browser Versions
-
Chromium 93.0.4576.0
-
火狐浏览器 90.0
¥Mozilla Firefox 90.0
-
网络工具包 14.2
¥WebKit 14.2
新 Playwright API
¥New Playwright APIs
-
Browser.newContext() 和 Browser.newPage() 中的新
baseURL
选项¥new
baseURL
option in Browser.newContext() and Browser.newPage() -
Page.inputValue()、Frame.inputValue() 和 ElementHandle.inputValue()
¥Page.inputValue(), Frame.inputValue() and ElementHandle.inputValue()
-
Page.fill()、Frame.fill() 和 ElementHandle.fill() 中的新
force
选项¥new
force
option in Page.fill(), Frame.fill(), and ElementHandle.fill() -
Page.selectOption()、Frame.selectOption() 和 ElementHandle.selectOption() 中的新
force
选项¥new
force
option in Page.selectOption(), Frame.selectOption(), and ElementHandle.selectOption()
1.12 版本
¥Version 1.12
🧟♂️ Playwright 跟踪查看器简介
¥🧟♂️ Introducing Playwright Trace Viewer
Playwright 踪迹查看器 是一个新的 GUI 工具,可帮助探索脚本运行后记录的 Playwright 痕迹。Playwright 的踪迹让你可以检查:
¥Playwright Trace Viewer is a new GUI tool that helps exploring recorded Playwright traces after the script ran. Playwright traces let you examine:
-
每个 Playwright 操作之前和之后的页面 DOM
¥page DOM before and after each Playwright action
-
每个 Playwright 操作之前和之后的页面渲染
¥page rendering before and after each Playwright action
-
脚本执行期间的浏览器网络
¥browser network during script execution
使用新的 BrowserContext.tracing() API 记录跟踪:
¥Traces are recorded using the new BrowserContext.tracing() API:
Browser browser = playwright.chromium().launch();
BrowserContext context = browser.newContext();
// Start tracing before creating / navigating a page.
context.tracing().start(new Tracing.StartOptions()
.setScreenshots(true)
.setSnapshots(true));
Page page = context.newPage();
page.navigate("https://playwright.nodejs.cn");
// Stop tracing and export it into a zip archive.
context.tracing().stop(new Tracing.StopOptions()
.setPath(Paths.get("trace.zip")));
稍后使用 Playwright CLI 检查痕迹:
¥Traces are examined later with the Playwright CLI:
mvn exec:java -e -D exec.mainClass=com.microsoft.playwright.CLI -D exec.args="show-trace trace.zip"
这将打开以下 GUI:
¥That will open the following GUI:
👉 阅读 跟踪查看器文档 中的更多内容。
¥👉 Read more in trace viewer documentation.
浏览器版本
¥Browser Versions
-
Chromium 93.0.4530.0
-
火狐浏览器 89.0
¥Mozilla Firefox 89.0
-
网络工具包 14.2
¥WebKit 14.2
此版本的 Playwright 还针对以下稳定通道进行了测试:
¥This version of Playwright was also tested against the following stable channels:
-
谷歌浏览器 91
¥Google Chrome 91
-
微软 Edge 91
¥Microsoft Edge 91
新 API
¥New APIs
-
Page.emulateMedia()、BrowserType.launchPersistentContext()、Browser.newContext() 和 Browser.newPage() 中的
reducedMotion
选项¥
reducedMotion
option in Page.emulateMedia(), BrowserType.launchPersistentContext(), Browser.newContext() and Browser.newPage() -
BrowserType.launch() 和 BrowserType.launchPersistentContext() 中的
tracesDir
选项¥
tracesDir
option in BrowserType.launch() and BrowserType.launchPersistentContext() -
新的 BrowserContext.tracing() API 命名空间
¥new BrowserContext.tracing() API namespace
-
新 Download.page() 方法
¥new Download.page() method
1.11 版本
¥Version 1.11
🎥 新视频:Playwright:现代 Web 的新测试自动化框架(slides)
¥🎥 New video: Playwright: A New Test Automation Framework for the Modern Web (slides)
-
我们谈论了 Playwright
¥We talked about Playwright
-
展示了幕后的工程工作
¥Showed engineering work behind the scenes
-
进行了新功能的现场演示 ✨
¥Did live demos with new features ✨
-
特别感谢 applitools 主办这次活动并邀请我们!
¥Special thanks to applitools for hosting the event and inviting us!
浏览器版本
¥Browser Versions
-
Chromium 92.0.4498.0
-
火狐浏览器 89.0b6
¥Mozilla Firefox 89.0b6
-
网络工具包 14.2
¥WebKit 14.2
新 API
¥New APIs
-
支持 Page.waitForRequest() 等方法中跨 API 的异步谓词
¥support for async predicates across the API in methods such as Page.waitForRequest() and others
-
新的模拟设备:Galaxy S8、Galaxy S9+、Galaxy Tab S4、Pixel 3、Pixel 4
¥new emulation devices: Galaxy S8, Galaxy S9+, Galaxy Tab S4, Pixel 3, Pixel 4
-
新方法:
¥new methods:
-
Page.waitForURL() 等待导航到 URL
¥Page.waitForURL() to await navigations to URL
-
Video.delete() 和 Video.saveAs() 管理屏幕录制
¥Video.delete() and Video.saveAs() to manage screen recording
-
-
新选项:
¥new options:
-
Browser.newContext() 方法中的
screen
选项可模拟window.screen
尺寸¥
screen
option in the Browser.newContext() method to emulatewindow.screen
dimensions -
Page.check() 和 Page.uncheck() 方法中的
position
选项¥
position
option in Page.check() and Page.uncheck() methods -
trial
选项可用于 Page.check()、Page.uncheck()、Page.click()、Page.dblclick()、Page.hover() 和 Page.tap() 中的空运行操作¥
trial
option to dry-run actions in Page.check(), Page.uncheck(), Page.click(), Page.dblclick(), Page.hover() and Page.tap()
-
1.10 版本
¥Version 1.10
-
Playwright for Java v1.10 现已稳定!
¥Playwright for Java v1.10 is now stable!
-
使用 新渠道 API 在 Google Chrome 和 Microsoft Edge 稳定通道上运行 Playwright。
¥Run Playwright against Google Chrome and Microsoft Edge stable channels with the new channels API.
-
Chromium 屏幕截图在 Mac 和 Windows 上速度很快。
¥Chromium screenshots are fast on Mac & Windows.
打包的浏览器版本
¥Bundled Browser Versions
-
Chromium 90.0.4430.0
-
火狐浏览器 87.0b10
¥Mozilla Firefox 87.0b10
-
网络工具包 14.2
¥WebKit 14.2
此版本的 Playwright 还针对以下稳定通道进行了测试:
¥This version of Playwright was also tested against the following stable channels:
-
谷歌浏览器 89
¥Google Chrome 89
-
微软 Edge 89
¥Microsoft Edge 89
新 API
¥New APIs
-
BrowserType.launch() 现在接受新的
'channel'
选项。阅读 我们的文档 中的更多内容。¥BrowserType.launch() now accepts the new
'channel'
option. Read more in our documentation.
1.9 版本
¥Version 1.9
-
Playwright 检查器 是一个新的 GUI 工具,用于编写和调试测试。
¥Playwright Inspector is a new GUI tool to author and debug your tests.
-
对 Playwright 脚本进行逐行调试,包括播放、暂停和单步执行。
¥Line-by-line debugging of your Playwright scripts, with play, pause and step-through.
-
通过记录用户操作来编写新脚本。
¥Author new scripts by recording user actions.
-
通过将鼠标悬停在元素上来为脚本生成元素选择器。
¥Generate element selectors for your script by hovering over elements.
-
设置
PWDEBUG=1
环境变量以启动 Inspector¥Set the
PWDEBUG=1
environment variable to launch the Inspector
-
-
在标题模式下使用 Page.pause() 暂停脚本执行。暂停页面会启动 Playwright 检查器 进行调试。
¥Pause script execution with Page.pause() in headed mode. Pausing the page launches Playwright Inspector for debugging.
-
CSS 选择器的新 has-text 伪类。
:has-text("example")
匹配内部某处包含"example"
的任何元素,可能在子元素或后代元素中。参见 更多例子。¥New has-text pseudo-class for CSS selectors.
:has-text("example")
matches any element containing"example"
somewhere inside, possibly in a child or a descendant element. See more examples. -
除非配置了
dialog
事件的监听器,否则页面对话框现在会在执行期间自动关闭。了解更多 关于这个。¥Page dialogs are now auto-dismissed during execution, unless a listener for
dialog
event is configured. Learn more about this. -
Python Playwright 现已稳定,具有惯用的蛇形案例 API 和预构建的 Docker 镜像 以在 CI/CD 中运行测试。
¥Playwright for Python is now stable with an idiomatic snake case API and pre-built Docker image to run tests in CI/CD.
浏览器版本
¥Browser Versions
-
Chromium 90.0.4421.0
-
火狐浏览器 86.0b10
¥Mozilla Firefox 86.0b10
-
网络工具包 14.1
¥WebKit 14.1
新 API
¥New APIs
1.8 版本
¥Version 1.8
-
根据布局选择元素 与
:left-of()
、:right-of()
、:above()
和:below()
。¥Selecting elements based on layout with
:left-of()
,:right-of()
,:above()
and:below()
. -
Playwright 现在包含命令行接口,即以前的 playwright-cli。
¥Playwright now includes command line interface, former playwright-cli.
mvn exec:java -e -D exec.mainClass=com.microsoft.playwright.CLI -D exec.args="--help"
-
Page.selectOption() 现在等待选项出现。
¥Page.selectOption() now waits for the options to be present.
-
断言元素状态 的新方法与 Page.isEditable() 类似。
¥New methods to assert element state like Page.isEditable().
新 API
¥New APIs
-
ElementHandle.waitForElementState() 中的新选项
'editable'
。¥New option
'editable'
in ElementHandle.waitForElementState().
浏览器版本
¥Browser Versions
-
Chromium 90.0.4392.0
-
火狐浏览器 85.0b5
¥Mozilla Firefox 85.0b5
-
网络工具包 14.1
¥WebKit 14.1
1.7 版本
¥Version 1.7
-
新的 Java SDK:Java Playwright 现在与 JavaScript、Python 和 .NET 绑定 同等。
¥New Java SDK: Playwright for Java is now on par with JavaScript, Python and .NET bindings.
-
浏览器存储 API:新的便利 API 用于保存和加载浏览器存储状态(cookie、本地存储),以通过身份验证简化自动化场景。
¥Browser storage API: New convenience APIs to save and load browser storage state (cookies, local storage) to simplify automation scenarios with authentication.
-
新的 CSS 选择器:我们听取了你对更灵活的选择器的反馈,并改进了选择器的实现。Playwright 1.7 引入了 新的 CSS 扩展,并且即将推出更多内容。
¥New CSS selectors: We heard your feedback for more flexible selectors and have revamped the selectors implementation. Playwright 1.7 introduces new CSS extensions and there's more coming soon.
-
新网站:playwright.dev 的文档网站已更新,现在使用 Docusaurus 构建。
¥New website: The docs website at playwright.dev has been updated and is now built with Docusaurus.
-
对苹果芯片的支持:适用于 WebKit 和 Chromium 的 Playwright 浏览器二进制文件现已针对 Apple Silicon 构建。
¥Support for Apple Silicon: Playwright browser binaries for WebKit and Chromium are now built for Apple Silicon.
新 API
¥New APIs
-
BrowserContext.storageState() 获取当前状态以供以后重用。
¥BrowserContext.storageState() to get current state for later reuse.
-
Browser.newContext() 和 Browser.newPage() 中的
storageState
选项用于设置浏览器上下文状态。¥
storageState
option in Browser.newContext() and Browser.newPage() to setup browser context state.
浏览器版本
¥Browser Versions
-
Chromium 89.0.4344.0
-
火狐浏览器 84.0b9
¥Mozilla Firefox 84.0b9
-
网络工具包 14.1
¥WebKit 14.1