Skip to main content

發行說明

版本 1.55

錄製測試

  • 自動 isVisible() 斷言:Codegen 現在可以為常見的 UI 互動產生自動 isVisible() 斷言。此功能可以在 Codegen 設定 UI 中啟用。

重大變更

  • ⚠️ 已移除對 Chromium 擴充功能 manifest v2 的支援。

其他

  • 新增對 Debian 13 "Trixie" 的支援。

瀏覽器版本

  • Chromium 140.0.7339.16
  • Mozilla Firefox 141.0
  • WebKit 26.0

此版本也對以下穩定頻道進行了測試:

  • Google Chrome 139
  • Microsoft Edge 139

版本 1.54

重點

  • BrowserContext.cookies()BrowserContext.addCookies() 中新增 cookie 屬性 partitionKey。此屬性允許儲存和還原分割的 cookies。請參閱 CHIPS MDN 文章 以取得更多資訊。請注意,瀏覽器對 cookie 分割有不同的支援和預設值。

  • 在多個指令中新增選項 --user-data-dir。您可以指定相同的使用者資料目錄來重複使用瀏覽狀態,例如驗證,在不同的會話之間。

    mvn exec:java -e -D exec.mainClass=com.microsoft.playwright.CLI -D exec.args="codegen --user-data-dir=./user-data"
  • mvn exec:java -e -D exec.mainClass=com.microsoft.playwright.CLI -D exec.args=open 指令不再開啟測試錄製器。請改用 mvn exec:java -e -D exec.mainClass=com.microsoft.playwright.CLI -D exec.args=codegen

瀏覽器版本

  • Chromium 139.0.7258.5
  • Mozilla Firefox 140.0.2
  • WebKit 26.0

此版本也對以下穩定頻道進行了測試:

  • Google Chrome 140
  • Microsoft Edge 140

版本 1.53

追蹤檢視器和 HTML 報告器更新

  • 追蹤檢視器中的新步驟:新的追蹤檢視器步驟

  • 新方法 Locator.describe() 用於描述定位器,在追蹤檢視器中使用。

    Locator button = page.getByTestId("btn-sub").describe("Subscribe button");
    button.click();
  • mvn exec:java -e -D exec.mainClass=com.microsoft.playwright.CLI -D exec.args="install --list" 現在會列出所有已安裝的瀏覽器、版本和位置。

瀏覽器版本

  • Chromium 138.0.7204.4
  • Mozilla Firefox 139.0
  • WebKit 18.5

此版本也對以下穩定頻道進行了測試:

  • Google Chrome 137
  • Microsoft Edge 137

版本 1.52

重點

  • 新方法 assertThat(locator).containsClass() 用於人體工學地斷言元素上的個別類別名稱。

      assertThat(page.getByRole(AriaRole.LISTITEM, new Page.GetByRoleOptions().setName("Ship v1.52"))).containsClass("done");
  • Aria Snapshots 獲得兩個新屬性:/children 用於嚴格匹配和 /url 用於連結。

    assertThat(locator).toMatchAriaSnapshot("""
    - list
    - /children: equal
    - listitem: Feature A
    - listitem:
    - link "Feature B":
    - /url: "https://playwright.dev"
    """);

其他

重大變更

  • Page.route() 這樣的方法中的全域 URL 模式不再支援 ?[]。我們建議改用正規表達式。
  • 方法 Route.resume() 不再允許覆寫 Cookie 標頭。如果提供了 Cookie 標頭,它將被忽略,cookie 將從瀏覽器的 cookie 儲存區載入。若要設定自訂 cookie,請使用 BrowserContext.addCookies()
  • macOS 13 現已棄用,將不再接收 WebKit 更新。請升級到更新的 macOS 版本以繼續受益於最新的 WebKit 改進。

瀏覽器版本

  • Chromium 136.0.7103.25
  • Mozilla Firefox 137.0
  • WebKit 18.4

此版本也對以下穩定頻道進行了測試:

  • Google Chrome 135
  • Microsoft Edge 135

版本 1.51

重點

  • 新選項 setIndexedDB 用於 BrowserContext.storageState() 允許儲存和還原 IndexedDB 內容。當您的應用程式使用 IndexedDB API 儲存身份驗證權杖(如 Firebase Authentication)時很有用。

    以下是遵循身份驗證指南的範例:

    // 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")));
  • 新選項 setVisible 用於 Locator.filter() 允許只匹配可見元素。

    // 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);
  • 新選項 setContrast 用於方法 Page.emulateMedia()Browser.newContext() 允許模擬 prefers-contrast 媒體功能。

  • 新選項 setFailOnStatusCode 讓所有透過 APIRequestContext 發出的 fetch 請求在回應代碼不是 2xx 和 3xx 時拋出例外。

瀏覽器版本

  • Chromium 134.0.6998.35
  • Mozilla Firefox 135.0
  • WebKit 18.4

此版本也對以下穩定頻道進行了測試:

  • Google Chrome 133
  • Microsoft Edge 133

版本 1.50

其他

UI 更新

  • Codegen 中用於選取元素以產生 aria 快照的新按鈕。
  • 其他詳細資訊(如按下的按鍵)現在會與追蹤中的動作 API 呼叫一起顯示。
  • 在追蹤中顯示 canvas 內容容易出錯。顯示功能現在預設停用,可透過 Display canvas content UI 設定啟用。
  • CallNetwork 面板現在顯示其他時間資訊。

重大變更

瀏覽器版本

  • Chromium 133.0.6943.16
  • Mozilla Firefox 134.0
  • WebKit 18.2

此版本也對以下穩定頻道進行了測試:

  • Google Chrome 132
  • Microsoft Edge 132

版本 1.49

Aria 快照

新斷言 assertThat(locator).matchesAriaSnapshot() 透過與預期的無障礙樹狀結構(以 YAML 表示)進行比較來驗證頁面結構。

page.navigate("https://playwright.dev");
assertThat(page.locator("body")).matchesAriaSnapshot("""
- banner:
- heading /Playwright enables reliable/ [level=1]
- link "開始使用"
- link "Star microsoft/playwright on GitHub"
- main:
- img "Browsers (Chromium, Firefox, WebKit)"
- heading "Any browser • Any platform • One API"
""");

您可以使用測試產生器或呼叫 Locator.ariaSnapshot() 產生此斷言。

aria 快照指南 中了解更多。

追蹤群組

新方法 Tracing.group() 允許您在追蹤檢視器中視覺化地群組動作。

// 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.dev/");
page.getByRole(AriaRole.LINK, new Page.GetByRoleOptions().setName("API")).click();
page.context().tracing().groupEnd();

Breaking: chromemsedge 頻道切換到新的無頭模式

如果您在 playwright.config.ts 中使用以下任一頻道,此變更會影響您:

  • chrome, chrome-dev, chrome-beta, or chrome-canary
  • msedge, msedge-dev, msedge-beta, or msedge-canary

更新到 Playwright v1.49 後,執行您的測試套件。如果仍然通過,您就可以繼續。如果沒有,您可能需要更新快照,並調整一些關於 PDF 檢視器和擴充功能的測試程式碼。請參閱 issue #33566 以取得更多詳細資訊。

嘗試新的 Chromium 無頭模式

您可以透過使用 'chromium' 頻道選擇新的無頭模式。如 官方 Chrome 文件所述

另一方面,新的無頭模式是真正的 Chrome 瀏覽器,因此更真實、可靠,並提供更多功能。這使其更適合高精度的端到端網頁應用程式測試或瀏覽器擴充功能測試。

請參閱...以取得更多詳細資訊.

其他

  • 方法 Locator.setInputFiles() 現在支援為 <input type=file webkitdirectory> 元素上傳目錄。

    page.getByLabel("Upload directory").setInputFiles(Paths.get("mydir"));
  • 多個方法(如 Locator.click() or Locator.press() 現在支援a ControlOrMeta modifier key. 此鍵 對應到 Meta on macOS and 對應到 Control on Windows and Linux.

    // Press the common keyboard shortcut Control+S or Meta+S to trigger a "Save" operation.
    page.keyboard.press("ControlOrMeta+S");
  • 新屬性 httpCredentials.sendAPIRequest.newContext() 中,允許永遠傳送 Authorization 標頭或僅在回應 401 Unauthorized 時傳送。

  • Playwright 現在支援在 Ubuntu 24.04 上的 Chromium、Firefox 和 WebKit。

  • v1.45 是最後一個接收 macOS 12 Monterey WebKit 更新的版本。請更新 macOS 以繼續使用最新的 WebKit。

瀏覽器版本

  • Chromium 127.0.6533.5
  • Mozilla Firefox 127.0
  • WebKit 17.4

此版本也對以下穩定頻道進行了測試:

  • Google Chrome 126
  • Microsoft Edge 126

版本 1.44

新的 API

無障礙斷言

  • assertThat(locator).hasAccessibleName() 檢查元素是否具有指定的無障礙名稱:

    Locator locator = page.getByRole(AriaRole.BUTTON);
    assertThat(locator).hasAccessibleName("Submit");
  • assertThat(locator).hasAccessibleDescription() 檢查元素是否具有指定的無障礙描述:

    Locator locator = page.getByRole(AriaRole.BUTTON);
    assertThat(locator).hasAccessibleDescription("Upload a photo");
  • assertThat(locator).hasRole() 檢查元素是否具有指定的 ARIA 角色:

    Locator locator = page.getByTestId("save-button");
    assertThat(locator).hasRole(AriaRole.BUTTON);

定位器處理器

Locator locator = page.getByText("This interstitial covers the button");
page.addLocatorHandler(locator, overlay -> {
overlay.locator("#close").click();
}, new Page.AddLocatorHandlerOptions().setTimes(3).setNoWaitAfter(true));
// 執行可能被覆蓋層中斷的測試。
// ...
page.removeLocatorHandler(locator);

其他選項

  • 新方法 FormData.append() 允許在 RequestOptionssetMultipart 選項中指定相同名稱的重複欄位:

    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) 現在支援 setIgnoreCase 選項.

瀏覽器版本

  • Chromium 125.0.6422.14
  • Mozilla Firefox 125.0.1
  • WebKit 17.4

此版本也對以下穩定頻道進行了測試:

  • Google Chrome 124
  • Microsoft Edge 124

版本 1.43

新的 API

  • 方法 BrowserContext.clearCookies() 現在支援篩選器 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 物件,稍後想要與框架內的內容互動時,這會很有用。

    Locator locator = page.locator("iframe[name='embedded']");
    // ...
    FrameLocator frameLocator = locator.contentFrame();
    frameLocator.getByRole(AriaRole.BUTTON).click();
  • 新方法 FrameLocator.owner()FrameLocator 物件轉換為 Locator。當您在某處獲得了 FrameLocator 物件,稍後想要與 iframe 元素互動時,這會很有用。

    FrameLocator frameLocator = page.frameLocator("iframe[name='embedded']");
    // ...
    Locator locator = frameLocator.owner();
    assertThat(locator).isVisible();

瀏覽器版本

  • Chromium 124.0.6367.8
  • Mozilla Firefox 124.0
  • WebKit 17.4

此版本也對以下穩定頻道進行了測試:

  • Google Chrome 123
  • Microsoft Edge 123

版本 1.42

實驗性 JUnit 整合

新增 @UsePlaywright 註解至您的測試類別,以開始在測試方法中使用 Playwright 固件,包括 PageBrowserContextBrowserAPIRequestContextPlaywright

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.dev/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。每個測試都使用自己的 BrowserContextPage

自訂選項

實作您自己的 OptionsFactory 以使用自訂配置初始化佈置。

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() {
回傳 new Options()
.setHeadless(false)
.setContextOption(new Browser.NewContextOptions()
.setBaseURL("https://github.com"))
.setApiRequestOptions(new APIRequest.NewContextOptions()
.setBaseURL("https://playwright.dev"));
}
}

@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 指南中了解更多關於佈置的資訊。

新的定位器處理器

新方法 Page.addLocatorHandler() 註冊一個回呼函數,當指定元素變為可見且可能阻擋 Playwright 動作時會被調用。回呼函數可以移除覆蓋層。以下是一個當 cookie 對話框出現時關閉它的範例。

// 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

公告

  • ⚠️ 不再支援Ubuntu 18。

瀏覽器版本

  • Chromium 123.0.6312.4
  • Mozilla Firefox 123.0
  • WebKit 17.4

此版本也對以下穩定頻道進行了測試:

  • Google Chrome 122
  • Microsoft Edge 123

版本 1.41

新的 API

瀏覽器版本

  • Chromium 121.0.6167.57
  • Mozilla Firefox 121.0
  • WebKit 17.4

此版本也對以下穩定頻道進行了測試:

  • Google Chrome 120
  • Microsoft Edge 120

版本 1.40

測試產生器更新

Playwright Test Generator

新的產生斷言工具:

以下是產生的測試與斷言範例:

page.navigate("https://playwright.dev/");
page.getByRole(AriaRole.LINK, new Page.GetByRoleOptions().setName("開始使用")).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

其他變更

瀏覽器版本

  • Chromium 120.0.6099.28
  • Mozilla Firefox 119.0
  • WebKit 17.4

此版本也對以下穩定頻道進行了測試:

  • Google Chrome 119
  • Microsoft Edge 119

版本 1.39

更新長青瀏覽器。

瀏覽器版本

  • Chromium 119.0.6045.9
  • Mozilla Firefox 118.0.1
  • WebKit 17.4

此版本也對以下穩定頻道進行了測試:

  • Google Chrome 118
  • Microsoft Edge 118

版本 1.38

追蹤檢視器更新

Playwright Trace Viewer

  1. 放大到時間範圍。
  2. 網路面板重新設計。

新的 API

棄用

瀏覽器版本

  • Chromium 117.0.5938.62
  • Mozilla Firefox 117.0
  • WebKit 17.0

此版本也對以下穩定頻道進行了測試:

  • Google Chrome 116
  • Microsoft Edge 116

版本 1.37

新的 API

  • 新方法s BrowserContext.newCDPSession()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 支援

Playwright 現在支援 Debian 12 Bookworm 在 x86_64 和 arm64 上的 Chromium、Firefox 和 WebKit。如果您遇到任何問題,請告訴我們!

Linux 支援如下:

Ubuntu 20.04Ubuntu 22.04Debian 11Debian 12
Chromium
WebKit
Firefox

瀏覽器版本

  • Chromium 116.0.5845.82
  • Mozilla Firefox 115.0
  • WebKit 17.0

此版本也對以下穩定頻道進行了測試:

  • Google Chrome 115
  • Microsoft Edge 115

版本 1.36

🏝️ 夏季維護版本。

瀏覽器版本

  • Chromium 115.0.5790.75
  • Mozilla Firefox 115.0
  • WebKit 17.0

此版本也對以下穩定頻道進行了測試:

  • Google Chrome 114
  • Microsoft Edge 114

版本 1.35

重點

  • 新選項 maskColor用於方法Page.screenshot()Locator.screenshot() 以變更預設遮罩顏色。

  • 新的 uninstall CLI 命令用於解除安裝瀏覽器二進位檔:

    $ 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

瀏覽器版本

  • Chromium 115.0.5790.13
  • Mozilla Firefox 113.0
  • WebKit 16.4

此版本也對以下穩定頻道進行了測試:

  • Google Chrome 114
  • Microsoft Edge 114

版本 1.34

重點

瀏覽器版本

  • Chromium 114.0.5735.26
  • Mozilla Firefox 113.0
  • WebKit 16.4

此版本也對以下穩定頻道進行了測試:

  • Google Chrome 113
  • Microsoft Edge 113

版本 1.33

定位器更新

  • 使用 Locator.or() 建立符合兩個定位器其中之一的定位器。考慮這樣的場景:您想要點選「New email」按鈕,但有時會出現安全設定對話框。在這種情況下,您可以等待「New email」按鈕或對話框,並相應地採取行動:

    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() 中使用新選項 setHasNotsetHasNotText 來尋找不符合特定條件的元素。

    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();
  • 使用新的網頁優先斷言 assertThat(locator).isAttached() 確保元素存在於頁面的 DOM 中。不要與 assertThat(locator).isVisible() 混淆,後者確保元素既附加又可見。

新的 API

其他重點

  • Apple Silicon 的原生支援 - Playwright 現在無需 Rosetta 即可執行
  • Added Ubuntu 22.04 (Jammy) Docker image

⚠️ 重大變更

  • mcr.microsoft.com/playwright/java:v1.33.0 現在提供基於 Ubuntu Jammy 的 Playwright 映像。若要使用基於 focal 的映像,請改用 mcr.microsoft.com/playwright/java:v1.33.0-focal

瀏覽器版本

  • Chromium 113.0.5672.53
  • Mozilla Firefox 112.0
  • WebKit 16.4

此版本也對以下穩定頻道進行了測試:

  • Google Chrome 112
  • Microsoft Edge 112

版本 1.32

新的 API

瀏覽器版本

  • Chromium 112.0.5615.29
  • Mozilla Firefox 111.0
  • WebKit 16.4

此版本也對以下穩定頻道進行了測試:

  • Google Chrome 111
  • Microsoft Edge 111

版本 1.31

新的 API

  • 新的斷言 assertThat(locator).isInViewport() 確保定位器指向與視埠相交的元素,根據交集觀察器 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));

其他

  • 追蹤檢視器中的 DOM 快照現在可以在單獨的視窗中開啟。
  • 新選項 setMaxRedirects 用於方法 Route.fetch().
  • Playwright 現在支援 Debian 11 arm64.
  • Official docker images 現在包含 Node 18 而非 Node 16。

瀏覽器版本

  • Chromium 111.0.5563.19
  • Mozilla Firefox 109.0
  • WebKit 16.4

此版本也對以下穩定頻道進行了測試:

  • Google Chrome 110
  • Microsoft Edge 110

版本 1.30

瀏覽器版本

  • Chromium 110.0.5481.38
  • Mozilla Firefox 108.0.2
  • WebKit 16.4

此版本也對以下穩定頻道進行了測試:

  • Google Chrome 109
  • Microsoft Edge 109

版本 1.29

新的 API

  • 新方法 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() to iterate over all matching elements:

    // Check all checkboxes!
    Locator checkboxes = page.getByRole(AriaRole.CHECKBOX);
    for (Locator checkbox : checkboxes.all())
    checkbox.check();
  • Locator.selectOption() matches現在by value or label:

    <select multiple>
    <選項 value="red">Red</選項>
    <選項 value="green">Green</選項>
    <選項 value="blue">Blue</選項>
    </select>
    element.selectOption("Red");

瀏覽器版本

  • Chromium 109.0.5414.46
  • Mozilla Firefox 107.0
  • WebKit 16.4

此版本也對以下穩定頻道進行了測試:

  • Google Chrome 108
  • Microsoft Edge 108

版本 1.28

Playwright 工具

  • CodeGen 中的即時定位器。 使用「探索」工具為頁面上的任何元素產生定位器。

Locator Explorer

新的 API

瀏覽器版本

  • Chromium 108.0.5359.29
  • Mozilla Firefox 106.0
  • WebKit 16.4

此版本也對以下穩定頻道進行了測試:

  • Google Chrome 107
  • Microsoft Edge 107

版本 1.27

定位器

使用這些新 API 撰寫定位器是一種樂趣:

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();

LocatorFrameLocatorFrame 類別也都提供相同的方法。

其他重點

  • 如 v1.25 中所宣布,Ubuntu 18 將在 2022 年 12 月起不再受支援。除此之外,從下一個 Playwright 版本開始,Ubuntu 18 上將不會有 WebKit 更新。

行為變更

  • assertThat(locator).hasAttribute() 使用空值不再比對缺少的屬性。例如,當 button 沒有 disabled 屬性時,以下程式碼片段將會成功。

    assertThat(page.getByRole(AriaRole.BUTTON)).hasAttribute("disabled", "");

瀏覽器版本

  • Chromium 107.0.5304.18
  • Mozilla Firefox 105.0.1
  • WebKit 16.0

此版本也對以下穩定頻道進行了測試:

  • Google Chrome 106
  • Microsoft Edge 106

版本 1.26

斷言

其他重點

  • 新選項 setMaxRedirects for APIRequestContext.get()和其他方法以limit redirect count.
  • Docker 映像現在使用 OpenJDK 17。

行為變更

許多 Playwright API 已經支援 setWaitUntil(WaitUntilState.DOMCONTENTLOADED) 選項。例如:

page.navigate("https://playwright.dev", new Page.NavigateOptions().setWaitUntil(WaitUntilState.DOMCONTENTLOADED));

在 1.26 之前,這將等待所有 iframe 觸發 DOMContentLoaded 事件。

為了與網頁規範保持一致,WaitUntilState.DOMCONTENTLOADED 值只會等待目標框架觸發 'DOMContentLoaded' 事件。使用 setWaitUntil(WaitUntilState.LOAD) 來等待所有 iframe。

瀏覽器版本

  • Chromium 106.0.5249.30
  • Mozilla Firefox 104.0
  • WebKit 16.0

此版本也對以下穩定頻道進行了測試:

  • Google Chrome 105
  • Microsoft Edge 105

版本 1.25

新的 API 與變更

公告

  • 🪦 這是最後一個支援macOS 10.15 的版本(自 1.21 起已棄用)。
  • ⚠️ Ubuntu 18 現已棄用,將從 2022 年 12 月起不再受支援。

瀏覽器版本

  • Chromium 105.0.5195.19
  • Mozilla Firefox 103.0
  • WebKit 16.0

此版本也對以下穩定頻道進行了測試:

  • Google Chrome 104
  • Microsoft Edge 104

版本 1.24

🐂 Debian 11 Bullseye Support

Playwright 現在支援 Debian 11 Bullseye 在 x86_64 上的 Chromium、Firefox 和 WebKit。如果您遇到任何問題,請告訴我們!

Linux 支援情況如下:

| | Ubuntu 20.04 | Ubuntu 22.04 | Debian 11 | :--- | :---: | :---: | :---: | :---: | | Chromium | ✅ | ✅ | ✅ | | WebKit | ✅ | ✅ | ✅ | | Firefox | ✅ | ✅ | ✅ |

版本 1.23

網路重播

現在您可以將網路流量錄製到 HAR 檔案中,並在測試中重複使用此流量。

將網路錄製到 HAR 檔案:

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:

BrowserContext context = browser.newContext(new Browser.NewContextOptions()
.setRecordHarPath(Paths.get("example.har"))
.setRecordHarUrlFilter("**/api/**"));

// ... Perform actions ...

// 關閉上下文以確保 HAR 儲存到磁碟。
context.close();

使用新方法 Page.routeFromHAR()BrowserContext.routeFromHAR()HAR 檔案提供符合的回應:

context.routeFromHAR(Paths.get("example.har"));

我們的文件中閱讀更多。

進階路由

您現在可以使用 Route.fallback() 將路由移交給其他處理器。

請考慮以下範例:

// 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() 也會參與路由處理,並且可以被延遲。

Web-First 斷言更新

其他

  • 如果有阻礙您的 Service Worker,您現在可以透過新的上下文選項 serviceWorkers 輕鬆停用它:

    BrowserContext context = browser.newContext(new Browser.NewContextOptions()
    .setServiceWorkers(ServiceWorkerPolicy.BLOCK));
  • Using .zip path for recordHar context 選項 自動壓縮 the resulting HAR:

    BrowserContext context = browser.newContext(new Browser.NewContextOptions()
    .setRecordHarPath(Paths.get("example.har.zip")));
  • 如果您打算手動編輯 HAR,請考慮使用僅記錄重播所需基本資訊的 "minimal" HAR 記錄模式:

    BrowserContext context = browser.newContext(new Browser.NewContextOptions()
    .setRecordHarPath(Paths.get("example.har"))
    .setRecordHarMode(HarMode.MINIMAL));
  • Playwright 現在在 Ubuntu 22 amd64 和 Ubuntu 22 arm64 上執行。

版本 1.22

重點

  • 角色選擇器,允許依據 ARIA roleARIA attributesaccessible name 選擇元素。

    // Click a button with accessible name "log in"
    page.locator("role=button[name='log in']").click();

    我們的文件中閱讀更多。

  • 新的 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 ARM64Apple M1。您現在可以在 Apple M1、Apple M1 的 Docker 內以及 Raspberry Pi 上執行 Playwright for Java 測試。

版本 1.21

重點

行為變更

瀏覽器版本

  • Chromium 101.0.4951.26
  • Mozilla Firefox 98.0.2
  • WebKit 15.4

此版本也對以下穩定頻道進行了測試:

  • Google Chrome 100
  • Microsoft Edge 100

版本 1.20

重點

公告

  • v1.20是最後一個接收receive WebKit update for macOS 10.15 Catalina. 請更新macOS以繼續使用latest & greatest WebKit!

瀏覽器版本

  • Chromium 101.0.4921.0
  • Mozilla Firefox 97.0.1
  • WebKit 15.4

此版本也對以下穩定頻道進行了測試:

  • Google Chrome 99
  • Microsoft Edge 99

版本 1.19

重點

  • Locator 現在支援 has 選項,確保它在內部包含另一個定位器:

    page.locator("article", new Page.LocatorOptions().setHas(page.locator(".highlight"))).click();

    定位器文件中閱讀更多

  • 新的 Locator.page()

  • Page.screenshot()Locator.screenshot() 現在會自動隐藏閃爍的游標

  • Playwright 錄製器現在會產生定位器和框架定位器

瀏覽器版本

  • Chromium 100.0.4863.0
  • Mozilla Firefox 96.0.1
  • WebKit 15.4

此版本也對以下穩定頻道進行了測試:

  • Google Chrome 98
  • Microsoft Edge 98

版本 1.18

API 測試

Playwright for Java 1.18 引入新的 API 測試,讓您可以直接從 Java 向伺服器發送請求!現在您可以:

  • test your server API
  • prepare server side state before visiting the web application in a test
  • validate server side post-conditions after running some actions in the browser

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 測試指南中閱讀更多。

Web-First 斷言

Playwright for Java 1.18 引入了 Web-First 斷言

請考慮以下範例:

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" 文字。它會反覆重新擷取節點並檢查,直到滿足條件或達到超時為止。您可以將此超時作為選項傳遞。

我們的文件中閱讀更多。

定位器改進

  • Locator.dragTo()

  • 每個定位器現在可以選擇性地依其包含的文字進行篩選:

    page.locator("li", new Page.LocatorOptions().setHasText("my item"))
    .locator("button").click();

    定位器文件中閱讀更多

追蹤改進

Tracing now can embed Java sources to recorded traces, using new setSources 選項.

tracing-java-sources

新的 API 與變更

瀏覽器版本

  • Chromium 99.0.4812.0
  • Mozilla Firefox 95.0
  • WebKit 15.4

此版本也對以下穩定頻道進行了測試:

  • Google Chrome 97
  • Microsoft Edge 97

版本 1.17

框架定位器

Playwright 1.17 引入了框架定位器 - 用於頁面上 iframe 的定位器。框架定位器捕獲足以擷取 iframe 然後在該 iframe 中定位元素的邏輯。框架定位器預設為嚴格模式,會等待 iframe 出現,並可用於 Web-First 斷言。

Graphics

框架定位器可以使用 Page.frameLocator()Locator.frameLocator() 方法建立。

Locator locator = page.frameLocator("#my-frame").locator("text=Submit");
locator.click();

我們的文件中閱讀更多。

追蹤檢視器更新

Playwright 追蹤檢視器現在可在線上使用,網址為 https://trace.playwright.dev!只需拖放您的 trace.zip 檔案即可檢查其內容。

NOTE: trace files are not uploaded anywhere; trace.playwright.dev is a progressive web application that processes traces locally.

  • Playwright 測試追蹤現在預設包含原始碼(這些可以透過追蹤選項關閉)
  • 追蹤檢視器現在顯示測試名稱
  • 新的追蹤中繼資料頁籤,包含瀏覽器詳細資訊
  • 快照現在有 URL 列

image

HTML Report Update

  • HTML report 現在支援dynamic filtering
  • 報告現在是單一靜態 HTML 檔案,可透過電子郵件傳送或作為 slack 附件。

image

Ubuntu ARM64 支援與更多功能

  • Playwright 現在支援 Ubuntu 20.04 ARM64。您現在可以在 Apple M1 的 Docker 內和 Raspberry Pi 上執行 Playwright 測試。

  • You 現在可以 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

  • Tracing 現在支援a 'title' 選項
  • Page navigations support a new 'commit' waiting 選項

版本 1.16

🎭 Playwright Library

Locator.waitFor

等待定位器解析為具有給定狀態的單一元素。預設為 state: 'visible'

Locator orderSent = page.locator("#order-sent");
orderSent.waitFor();

Locator.waitFor().

🎭 Playwright Trace Viewer

  • 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

Trace Viewer.

瀏覽器版本

  • Chromium 97.0.4666.0
  • Mozilla Firefox 93.0
  • WebKit 15.4

此版本也對以下穩定頻道進行了測試:

  • Google Chrome 94
  • Microsoft Edge 94

版本 1.15

🖱️ Mouse Wheel

透過使用 Mouse.wheel() 您現在可以垂直或水平捲動。

📜 新的 Headers API

以前無法取得回應的多個標頭值。現在這是可能的,並提供了額外的輔助函數:

🌈 Forced-Colors emulation

現在可以透過在 Browser.newContext() 中傳遞它或呼叫 Page.emulateMedia() 來模擬 forced-colors CSS 媒體功能。

新的 API

瀏覽器版本

  • Chromium 96.0.4641.0
  • Mozilla Firefox 92.0
  • WebKit 15.0

版本 1.14

⚡️ New "strict" mode

選擇器歧義是自動化測試中的常見問題。「嚴格」模式確保您的選擇器指向單一元素,否則會拋出例外。

在您的動作呼叫中設定 setStrict(true) 以啟用。

// This will throw if you have more than one button!
page.click("button", new Page.ClickOptions().setStrict(true));

📍 新的 Locators API

Locator 代表頁面上元素的檢視。它捕獲在任何時刻擷取元素所需的邏輯。

LocatorElementHandle 之間的差異在於,後者指向特定元素,而 Locator 捕獲如何擷取該元素的邏輯。

Also, locators are "strict" by default!

Locator locator = page.locator("button");
locator.click();

文件中了解更多。

🧩 Experimental ReactVue selector engines

React 和 Vue 選擇器允許按組件名稱和/或屬性值選擇元素。語法與屬性選擇器非常相似,並支援所有屬性選擇器運算子。

page.locator("_react=SubmitButton[enabled=true]").click();
page.locator("_vue=submit-button[enabled=true]").click();

react 選擇器文件vue 選擇器文件 中了解更多。

✨ New nthvisible selector engines

  • nth selector engine is equivalent to the :nth-match pseudo class, but could be combined with other selector engines.
  • 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");

瀏覽器版本

  • Chromium 94.0.4595.0
  • Mozilla Firefox 91.0
  • WebKit 15.0

版本 1.13

Playwright

工具

  • Playwright Trace Viewer現在shows parameters, returned values and console.log() calls.

新增與重新編寫的指南

瀏覽器版本

  • Chromium 93.0.4576.0
  • Mozilla Firefox 90.0
  • WebKit 14.2

新的 Playwright API

版本 1.12

🧟‍♂️ Introducing Playwright Trace Viewer

Playwright Trace Viewer 是一個新的 GUI 工具,可幫助探索在腳本執行後記錄的 Playwright 追蹤。Playwright 追蹤讓您檢查:

  • page DOM before and after each Playwright action
  • page rendering before and after each Playwright action
  • browser network during script execution

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.dev");

// Stop tracing and export it into a zip archive.
context.tracing().stop(new Tracing.StopOptions()
.setPath(Paths.get("trace.zip")));

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"

That will open the following GUI:

image

👉 在追蹤檢視器文件中閱讀更多。

瀏覽器版本

  • Chromium 93.0.4530.0
  • Mozilla Firefox 89.0
  • WebKit 14.2

此版本也對以下穩定頻道進行了測試:

  • Google Chrome 91
  • Microsoft Edge 91

新的 API

版本 1.11

🎥 New video: Playwright: A New Test Automation Framework for the Modern Web (slides)

  • We talked about Playwright
  • Showed engineering work behind the scenes
  • Did live demos with new features ✨
  • 特別感謝 applitools 主辦活動並邀請我們!

瀏覽器版本

  • Chromium 92.0.4498.0
  • Mozilla Firefox 89.0b6
  • WebKit 14.2

新的 API

版本 1.10

Bundled Browser Versions

  • Chromium 90.0.4430.0
  • Mozilla Firefox 87.0b10
  • WebKit 14.2

此版本也對以下穩定頻道進行了測試:

  • Google Chrome 89
  • Microsoft Edge 89

新的 API

版本 1.9

  • Playwright Inspector 是一個新的 GUI 工具,用於編寫和偵錯您的測試。
    • 逐行偵錯您的 Playwright 腳本,支援播放、暫停和逐步執行。
    • 透過錄製使用者操作編寫新腳本。
    • 透過滑鼠懸停在元素上為您的腳本產生元素選擇器
    • Set the PWDEBUG=1 environment variable to launch the Inspector
  • Pause script execution with Page.pause() in headed mode. Pausing the page launches Playwright Inspector for debugging.
  • 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 事件的監聽器,否則在執行過程中會自動關閉頁面對話框。了解更多相關資訊。
  • Playwright for Python 現在穩定,具有慣用的蛇形命名法 API 和預建的 Docker 映像 用於在 CI/CD 中執行測試。

瀏覽器版本

  • Chromium 90.0.4421.0
  • Mozilla Firefox 86.0b10
  • WebKit 14.1

新的 API

版本 1.8

新的 API

瀏覽器版本

  • Chromium 90.0.4392.0
  • Mozilla Firefox 85.0b5
  • WebKit 14.1

版本 1.7

  • New Java SDK: Playwright for Java is現在on par with JavaScript, Python.NET bindings.
  • 瀏覽器儲存 API:新的便利 API 用於儲存和載入瀏覽器儲存狀態(cookies、本地儲存),以簡化驗證的自動化場景。
  • 新的 CSS 選擇器:我們聽到了您對更靈活選擇器的回饋意見,並改良了選擇器實作。Playwright 1.7 引入了新的 CSS 擴充功能,更多功能即將推出。
  • 新網站playwright.dev 的文件網站已更新,現在使用 Docusaurus 建置。
  • 支援Apple Silicon:WebKit 和 Chromium 的 Playwright 瀏覽器二進位檔現在為 Apple Silicon 建置。

新的 API

瀏覽器版本

  • Chromium 89.0.4344.0
  • Mozilla Firefox 84.0b9
  • WebKit 14.1