發行說明
版本 1.55
錄製測試
- 自動
to_be_visible()
斷言:Codegen 現在可以為常見的 UI 互動產生自動to_be_visible()
斷言。此功能可以在 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
重點
-
在 browser_context.cookies() 和 browser_context.add_cookies() 中新增 cookie 屬性
partition_key
。此屬性允許儲存和還原分割的 cookies。請參閱 CHIPS MDN 文章 以取得更多資訊。請注意,瀏覽器對 cookie 分割有不同的支援和預設值。 -
在多個指令中新增選項
--user-data-dir
。您可以指定相同的使用者資料目錄來重複使用瀏覽狀態,例如驗證,在不同的會話之間。playwright codegen --user-data-dir=./user-data
-
playwright open
不再開啟測試錄製器。請改用playwright codegen
。
瀏覽器版本
- Chromium 139.0.7258.5
- Mozilla Firefox 140.0.2
- WebKit 26.0
此版本也對以下穩定頻道進行了測試:
- Google Chrome 140
- Microsoft Edge 140
版本 1.53
Trace Viewer and HTML Reporter Updates
-
追蹤檢視器中的新步驟:
-
新方法 locator.describe() 用於描述定位器,在追蹤檢視器中使用。
button = page.get_by_test_id("btn-sub").describe("Subscribe button")
button.click() -
python -m playwright install --list
現在會列出所有已安裝的瀏覽器、版本和位置。
瀏覽器版本
- Chromium 138.0.7204.4
- Mozilla Firefox 139.0
- WebKit 18.5
此版本也對以下穩定頻道進行了測試:
- Google Chrome 137
- Microsoft Edge 137
版本 1.52
重點
-
新方法 expect(locator).to_contain_class() 用於人體工學地斷言元素上的個別類別名稱。
expect(page.get_by_role('listitem', name='Ship v1.52')).to_contain_class('done')
-
Aria 快照 獲得了兩個新屬性:
/children
用於嚴格比對,以及/url
用於連結。expect(locator).to_match_aria_snapshot("""
- list
- /children: equal
- listitem: Feature A
- listitem:
- link "Feature B":
- /url: "https://playwright.dev"
""")
其他
- 新選項 max_redirects 在 api_request.new_context() 中控制重定向的最大次數。
重大變更
- page.route() 等方法中的 Glob URL 模式不再支援
?
和[]
。我們建議改用正規表示式。 - 方法 route.continue_() 不再允許覆寫
Cookie
標頭。如果提供了Cookie
標頭,它將被忽略,而 cookie 將從瀏覽器的 cookie 儲存中載入。要設定自訂 cookie,請使用 browser_context.add_cookies()。 - 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
重點
-
新選項 indexed_db for browser_context.storage_state() 允許save and restore IndexedDB contents. Useful when your application uses IndexedDB API to store authentication tokens, like Firebase Authentication.
以下是遵循身份驗證指南的範例:
# Save storage state into the file. Make sure to include IndexedDB.
storage = await context.storage_state(path="state.json", indexed_db=True)
# Create a new context with the saved storage state.
context = await browser.new_context(storage_state="state.json") -
新選項 visible 用於 locator.filter() 允許只匹配可見元素。
# Ignore invisible todo items.
todo_items = page.get_by_test_id("todo-item").filter(visible=True)
# Check there are exactly 3 visible ones.
await expect(todo_items).to_have_count(3) -
新選項
contrast
用於方法page.emulate_media() 和 browser.new_context() 允許emulate theprefers-contrast
media feature. -
新選項 fail_on_status_code 讓所有 fetch requests made through the APIRequestContext throw on response codes other than 2xx and 3xx.
瀏覽器版本
- Chromium 134.0.6998.35
- Mozilla Firefox 135.0
- WebKit 18.4
此版本也對以下穩定頻道進行了測試:
- Google Chrome 133
- Microsoft Edge 133
版本 1.50
非同步 Pytest 外掛
- Playwright's Pytest plugin now has support for Async Fixtures.
其他
- Added 方法 expect(locator).to_have_accessible_error_message() to assert the Locator points to an element with a given aria errormessage.
UI 更新
- 錄製測試中用於挑選元素以產生 aria 快照的新按鈕。
- 其他詳細資訊(如按下的按鍵)現在會與追蹤中的動作 API 呼叫一起顯示。
- 在追蹤中顯示
canvas
內容容易出錯。顯示功能現在預設停用,可透過Display canvas content
UI 設定啟用。 Call
和Network
面板現在顯示額外的時間資訊。
重大變更
- expect(locator).to_be_editable() 和 locator.is_editable() 現在在目標元素不是
<input>
、<select>
或其他許多可編輯元素時會拋出例外。
瀏覽器版本
- Chromium 133.0.6943.16
- Mozilla Firefox 134.0
- WebKit 18.2
此版本也對以下穩定頻道進行了測試:
- Google Chrome 132
- Microsoft Edge 132
版本 1.49
Aria 快照
新斷言 expect(locator).to_match_aria_snapshot() 透過與預期的無障礙樹狀結構(以 YAML 表示)進行比較來驗證頁面結構。
page.goto("https://playwright.dev")
expect(page.locator('body')).to_match_aria_snapshot('''
- 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.aria_snapshot() 產生此斷言。
在 aria 快照指南 中了解更多。
追蹤群組
新方法 tracing.group() 允許您在追蹤檢視器中視覺化地群組動作。
# group 和 group_end 之間的所有動作
# 將在追蹤檢視器中顯示為一個群組。
page.context.tracing.group("Open Playwright.dev > API")
page.goto("https://playwright.dev/")
page.get_by_role("link", name="API").click()
page.context.tracing.group_end()
Breaking: chrome
和 msedge
頻道切換到新的無頭模式
如果您在 playwright.config.ts
中使用以下任一頻道,此變更會影響您:
chrome
,chrome-dev
,chrome-beta
, orchrome-canary
msedge
,msedge-dev
,msedge-beta
, ormsedge-canary
更新到 Playwright v1.49 後,執行您的測試套件。如果仍然通過,您就可以繼續。如果沒有,您可能需要更新快照,並調整一些關於 PDF 檢視器和擴充功能的測試程式碼。請參閱 issue #33566 以取得更多詳細資訊。
嘗試新的 Chromium 無頭模式
您可以透過使用 'chromium'
頻道選擇新的無頭模式。如 官方 Chrome 文件所述:
另一方面,新的無頭模式是真正的 Chrome 瀏覽器,因此更真實、可靠,並提供更多功能。這使其更適合高精度的端到端網頁應用程式測試或瀏覽器擴充功能測試。
請參閱...以取得更多詳細資訊.
其他
-
方法 locator.set_input_files() 現在支援為
<input type=file webkitdirectory>
元素上傳目錄。page.get_by_label("Upload directory").set_input_files('mydir')
-
多個方法(如 locator.click() or locator.press() 現在支援a
ControlOrMeta
modifier key. 此鍵 對應到Meta
on macOS and 對應到Control
on Windows and Linux.# 按下常見的鍵盤快捷鍵 Control+S 或 Meta+S 來觸發「儲存」操作。
page.keyboard.press("ControlOrMeta+S") -
新屬性
httpCredentials.send
在 api_request.new_context() 中,允許永遠傳送Authorization
標頭或僅在回應401 Unauthorized
時傳送。 -
Playwright 現在支援在 Ubuntu 24.04 上的 Chromium、Firefox 和 WebKit。
-
v1.45 是最後一個接收 WebKit 更新的 macOS 12 Monterey 版本。請更新 macOS 以繼續使用最新的 WebKit。
瀏覽器版本
- Chromium 127.0.6533.5
- Mozilla Firefox 127.0
- WebKit 17.4
此版本也對以下穩定頻道進行了測試:
- Google Chrome 126
- Microsoft Edge 126
版本 1.44
新的 API
無障礙斷言
-
expect(locator).to_have_accessible_name() 檢查元素是否具有指定的無障礙名稱:
locator = page.get_by_role("button")
expect(locator).to_have_accessible_name("Submit") -
expect(locator).to_have_accessible_description() 檢查元素是否具有指定的無障礙描述:
locator = page.get_by_role("button")
expect(locator).to_have_accessible_description("Upload a photo") -
expect(locator).to_have_role() 檢查元素是否具有指定的 ARIA 角色:
locator = page.get_by_test_id("save-button")
expect(locator).to_have_role("button")
定位器處理器
- 執行透過 page.add_locator_handler() 新增的處理器後,Playwright 現在會等待直到觸發處理器的覆蓋層不再可見。您可以使用新的
no_wait_after
選項來選擇退出此行為。 - 您可以在 page.add_locator_handler() 中使用新的
times
選項來指定處理器應執行的最大次數。 - page.add_locator_handler() 中的處理器現在接受定位器作為參數。
- 新方法 page.remove_locator_handler() 用於移除先前新增的定位器處理器。
locator = page.get_by_text("此插頁覆蓋了按鈕")
page.add_locator_handler(locator, lambda overlay: overlay.locator("#close").click(), times=3, no_wait_after=True)
# 執行可能被覆蓋層中斷的測試。
# ...
page.remove_locator_handler(locator)
其他選項
- expect(page).to_have_url() 現在支援
ignore_case
選項.
瀏覽器版本
- Chromium 125.0.6422.14
- Mozilla Firefox 125.0.1
- WebKit 17.4
此版本也對以下穩定頻道進行了測試:
- Google Chrome 124
- Microsoft Edge 124
版本 1.43
新的 API
-
方法 browser_context.clear_cookies() 現在支援篩選器 to remove only some cookies.
# Clear all cookies.
context.clear_cookies()
# New: clear cookies with a particular name.
context.clear_cookies(name="session-id")
# New: clear cookies for a particular domain.
context.clear_cookies(domain="my-origin.com") -
新方法 locator.content_frame 將 Locator 物件轉換為 FrameLocator。當您在某處獲得了 Locator 物件,稍後想要與框架內的內容互動時,這會很有用。
locator = page.locator("iframe[name='embedded']")
# ...
frame_locator = locator.content_frame
frame_locator.getByRole("button").click() -
新方法 frame_locator.owner 將 FrameLocator 物件轉換為 Locator。當您在某處獲得了 FrameLocator 物件,稍後想要與
iframe
元素互動時,這會很有用。frame_locator = page.frame_locator("iframe[name='embedded']")
# ...
locator = frame_locator.owner
expect(locator).to_be_visible() -
現在發佈用於 macOS-arm64 和 Linux-arm64 的 Conda 建置。
瀏覽器版本
- Chromium 124.0.6367.8
- Mozilla Firefox 124.0
- WebKit 17.4
此版本也對以下穩定頻道進行了測試:
- Google Chrome 123
- Microsoft Edge 123
版本 1.42
新的定位器處理器
新方法 page.add_locator_handler() 註冊一個回呼函數,當指定元素變為可見且可能阻擋 Playwright 動作時會被調用。回呼函數可以移除覆蓋層。以下是一個當 cookie 對話框出現時關閉它的範例。
# Setup the handler.
page.add_locator_handler(
page.get_by_role("heading", name="Hej! You are in control of your cookies."),
lambda: page.get_by_role("button", name="Accept all").click(),
)
# Write the test as usual.
page.goto("https://www.ikea.com/")
page.get_by_role("link", name="Collection of blue and white").click()
expect(page.get_by_role("heading", name="Light and easy")).to_be_visible()
新的 API
- page.pdf() 接受兩個新選項 tagged 和 outline。
公告
- ⚠️ 不再支援Ubuntu 18。
瀏覽器版本
- Chromium 123.0.6312.4
- Mozilla Firefox 123.0
- WebKit 17.4
此版本也對以下穩定頻道進行了測試:
- Google Chrome 122
- Microsoft Edge 123
版本 1.41
新的 API
- 新方法 page.unroute_all() 移除由 page.route() 和 page.route_from_har() 註冊的所有路由。可選擇等待進行中的路由完成,或忽略來自它們的任何錯誤。
- 新方法 browser_context.unroute_all() 移除由 browser_context.route() 和 browser_context.route_from_har() 註冊的所有路由。可選擇等待進行中的路由完成,或忽略來自它們的任何錯誤。
- 新選項s style in page.screenshot() 和 style in locator.screenshot()以新增custom CSS to the page在執行...之前a screenshot.
瀏覽器版本
- Chromium 121.0.6167.57
- Mozilla Firefox 121.0
- WebKit 17.4
此版本也對以下穩定頻道進行了測試:
- Google Chrome 120
- Microsoft Edge 120
版本 1.40
測試產生器更新
新的產生斷言工具:
- "Assert visibility" 工具產生 expect(locator).to_be_visible()。
- "Assert value" 工具產生 expect(locator).to_have_value()。
- "Assert text" 工具產生 expect(locator).to_contain_text()。
以下是產生的測試與斷言範例:
from playwright.sync_api import Page, expect
def test_example(page: Page) -> None:
page.goto("https://playwright.dev/")
page.get_by_role("link", name="開始使用").click()
expect(page.get_by_label("Breadcrumbs").get_by_role("list")).to_contain_text("Installation")
expect(page.get_by_label("Search")).to_be_visible()
page.get_by_label("Search").click()
page.get_by_placeholder("Search docs").fill("locator")
expect(page.get_by_placeholder("Search docs")).to_have_value("locator");
新的 API
- page.close() 中的選項 reason、browser_context.close() 中的選項 reason 和 browser.close() 中的選項 reason。關閉原因會回報給所有因關閉而中斷的操作。
- 選項 firefox_user_prefs in browser_type.launch_persistent_context().
其他變更
- 方法 download.path()對於...會拋出錯誤failed and cancelled downloads.
瀏覽器版本
- 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
追蹤檢視器更新
- 放大到時間範圍。
- 網路面板重新設計。
新的 API
棄用
- 以下方法已棄用:page.type()、frame.type()、locator.type() 和 element_handle.type()。請改用速度更快的 locator.fill()。只有在頁面有特殊的鍵盤處理需求,需要逐一按鍵時,才使用 locator.press_sequentially()。
瀏覽器版本
- Chromium 117.0.5938.62
- Mozilla Firefox 117.0
- WebKit 17.0
此版本也對以下穩定頻道進行了測試:
- Google Chrome 116
- Microsoft Edge 116
版本 1.37
重點
- 新的 --full-page-screenshot 命令列標誌允許在失敗時擷取全頁面截圖。
- 現在可以透過使用 browser_context_args 標記來覆寫單個測試的上下文選項。
pytest-playwright
現在也發佈在 Anaconda 上
📚 Debian 12 Bookworm 支援
Playwright 現在支援 Debian 12 Bookworm 在 x86_64 和 arm64 上的 Chromium、Firefox 和 WebKit。如果您遇到任何問題,請告訴我們!
Linux 支援如下:
Ubuntu 20.04 | Ubuntu 22.04 | Debian 11 | Debian 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
重點
-
新選項
mask_color
用於方法page.screenshot() 和 locator.screenshot() 以變更預設遮罩顏色。 -
新的
uninstall
CLI 命令用於解除安裝瀏覽器二進位檔:$ playwright uninstall # remove browsers installed by this installation
$ playwright 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
重點
-
新的 locator.and_() 用於建立匹配兩個定位器的定位器。
button = page.get_by_role("button").and_(page.get_by_title("Subscribe"))
-
新事件 browser_context.on("console") 和 browser_context.on("dialog") 用於訂閱來自指定瀏覽器上下文中任何頁面的對話框和控制台訊息。使用新方法 console_message.page 和 dialog.page 來精確定位事件來源。
瀏覽器版本
- 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」按鈕或對話框,並相應地採取行動:
new_email = page.get_by_role("button", name="New email")
dialog = page.get_by_text("Confirm security settings")
expect(new_email.or_(dialog)).is_visible()
if (dialog.is_visible()):
page.get_by_role("button", name="Dismiss").click()
new_email.click() -
使用新選項 has_not 和 has_not_text 在 locator.filter() 中查找不匹配特定條件的元素。
row_locator = page.locator("tr")
row_locator.filter(has_not_text="text in column 1").filter(
has_not=page.get_by_role("button", name="column 2 button")
).screenshot() -
使用新的網頁優先斷言 expect(locator).to_be_attached() 來確保元素存在於頁面的 DOM 中。不要與 expect(locator).to_be_visible() 混淆,後者確保元素既被附加又可見。
新的 API
- locator.or_()
- 新選項 has_not in locator.filter()
- 新選項 has_not_text in locator.filter()
- expect(locator).to_be_attached()
- 新選項 timeout in route.fetch()
⚠️ 重大變更
mcr.microsoft.com/playwright/python:v1.33.0
現在提供基於 Ubuntu Jammy 的 Playwright 映像。若要使用基於 focal 的映像,請改用mcr.microsoft.com/playwright/python: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
- 自訂預期訊息,請參閱測試斷言文件。
- 新選項s update_mode 和 update_content in page.route_from_har() 和 browser_context.route_from_har().
- Chaining existing locator objects, see locator docs for details.
- 新選項 name in 方法 tracing.start_chunk().
瀏覽器版本
- Chromium 112.0.5615.29
- Mozilla Firefox 111.0
- WebKit 16.4
此版本也對以下穩定頻道進行了測試:
- Google Chrome 111
- Microsoft Edge 111
版本 1.31
新的 API
-
新的斷言 expect(locator).to_be_in_viewport() 確保定位器指向根據交集觀察器 API 與檢視區相交的元素。
from playwright.sync_api import expect
locator = page.get_by_role("button")
# Make sure at least some part of element intersects viewport.
expect(locator).to_be_in_viewport()
# Make sure element is fully outside of viewport.
expect(locator).not_to_be_in_viewport()
# Make sure that at least half of the element intersects viewport.
expect(locator).to_be_in_viewport(ratio=0.5)
其他
- 追蹤檢視器中的 DOM 快照現在可以在單獨的視窗中開啟。
- 新選項 max_redirects 用於方法 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() 和新選項
json
用於route.fulfill():def handle_route(route: Route):
# Fetch original settings.
response = route.fetch()
# Force settings theme to a predefined value.
json = response.json()
json["theme"] = "Solorized"
# Fulfill with modified data.
route.fulfill(json=json)
page.route("**/api/settings", handle_route) -
新方法 locator.all() to iterate over all matching elements:
# Check all checkboxes!
checkboxes = page.get_by_role("checkbox")
for checkbox in checkboxes.all():
checkbox.check() -
locator.select_option() matches現在by value or label:
<select multiple>
<選項 value="red">Red</選項>
<選項 value="green">Green</選項>
<選項 value="blue">Blue</選項>
</select>element.select_option("Red")
其他
- 選項
postData
in 方法 route.continue_() 現在支援 Serializable values.
瀏覽器版本
- Chromium 109.0.5414.46
- Mozilla Firefox 107.0
- WebKit 16.4
此版本也對以下穩定頻道進行了測試:
- Google Chrome 108
- Microsoft Edge 108
版本 1.28
Playwright 工具
- CodeGen 中的即時定位器。 使用「探索」工具為頁面上的任何元素產生定位器。
新的 API
瀏覽器版本
- Chromium 108.0.5359.29
- Mozilla Firefox 106.0
- WebKit 16.4
此版本也對以下穩定頻道進行了測試:
- Google Chrome 107
- Microsoft Edge 107
版本 1.27
定位器
使用這些新 API 編寫定位器是一種樂趣:
- page.get_by_text() 透過文字內容定位。
- page.get_by_role() 透過 ARIA role、ARIA attributes 和 accessible name 定位。
- page.get_by_label()以定位a form control by associated label's text.
- page.get_by_test_id() 以定位基於其
data-testid
屬性的元素(可以配置其他屬性)。 - page.get_by_placeholder()以定位an input by placeholder.
- page.get_by_alt_text()以定位an element, usually image, by its text alternative.
- page.get_by_title()以定位an element by its title.
page.get_by_label("User Name").fill("John")
page.get_by_label("Password").fill("secret-password")
page.get_by_role("button", name="Sign in").click()
expect(page.get_by_text("Welcome, John!")).to_be_visible()
Locator、FrameLocator 和 Frame 類別也都提供相同的方法。
其他重點
- 如 v1.25 中所宣布,Ubuntu 18 將在 2022 年 12 月起不再受支援。除此之外,從下一個 Playwright 版本開始,Ubuntu 18 上將不會有 WebKit 更新。
行為變更
-
expect(locator).to_have_attribute() 使用空值不再比對缺少的屬性。例如,當
button
沒有disabled
屬性時,以下程式碼片段將會成功。expect(page.get_by_role("button")).to_have_attribute("disabled", "")
瀏覽器版本
- Chromium 107.0.5304.18
- Mozilla Firefox 105.0.1
- WebKit 16.0
此版本也對以下穩定頻道進行了測試:
- Google Chrome 106
- Microsoft Edge 106
版本 1.26
斷言
- 新選項
enabled
for expect(locator).to_be_enabled(). - expect(locator).to_have_text()現在pierces open shadow roots.
- 新選項
editable
for expect(locator).to_be_editable(). - 新選項
visible
for expect(locator).to_be_visible().
其他重點
- 新選項
max_redirects
for api_request_context.get()和其他方法以limit redirect count. - 現在支援Python 3.11。
行為變更
許多 Playwright API 已經支援 wait_until: "domcontentloaded"
選項。例如:
page.goto("https://playwright.dev", wait_until="domcontentloaded")
在 1.26 之前,這將等待所有 iframe 觸發 DOMContentLoaded
事件。
為了與網頁規範保持一致,'domcontentloaded'
值只會等待目標框架觸發 'DOMContentLoaded'
事件。使用 wait_until="load"
來等待所有 iframe。
瀏覽器版本
- Chromium 106.0.5249.30
- Mozilla Firefox 104.0
- WebKit 16.0
此版本也對以下穩定頻道進行了測試:
- Google Chrome 105
- Microsoft Edge 105
版本 1.25
公告
- 🎁 我們現在發佈 Ubuntu 22.04 Jammy Jellyfish docker 映像:
mcr.microsoft.com/playwright/python:v1.34.0-jammy
。 - 🪦 這是最後一個支援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 | ✅ | ✅ | ✅ |
新的介紹文件
我們重新撰寫了入門文件,更專注於端對端測試。請在 playwright.dev 查看這些文件。
版本 1.23
網路重播
現在您可以將網路流量錄製到 HAR 檔案中,並在測試中重複使用此流量。
將網路錄製到 HAR 檔案:
npx playwright open --save-har=github.har.zip https://github.com/microsoft
或者,您可以透過程式方式錄製 HAR:
- Sync
- Async
context = browser.new_context(record_har_path="github.har.zip")
# ... do stuff ...
context.close()
context = await browser.new_context(record_har_path="github.har.zip")
# ... do stuff ...
await context.close()
使用新方法 page.route_from_har() 或 browser_context.route_from_har() 來從 HAR 檔案提供匹配的回應:
- Sync
- Async
context.route_from_har("github.har.zip")
await context.route_from_har("github.har.zip")
在我們的文件中閱讀更多。
進階路由
您現在可以使用 route.fallback() 將路由移交給其他處理器。
請考慮以下範例:
- Sync
- Async
# Remove a header from all requests
def remove_header_handler(route: Route) -> None:
headers = route.request.all_headers()
if "if-none-match" in headers:
del headers["if-none-match"]
route.fallback(headers=headers)
page.route("**/*", remove_header_handler)
# Abort all images
def abort_images_handler(route: Route) -> None:
if route.request.resource_type == "image":
route.abort()
else:
route.fallback()
page.route("**/*", abort_images_handler)
# Remove a header from all requests
async def remove_header_handler(route: Route) -> None:
headers = await route.request.all_headers()
if "if-none-match" in headers:
del headers["if-none-match"]
await route.fallback(headers=headers)
await page.route("**/*", remove_header_handler)
# Abort all images
async def abort_images_handler(route: Route) -> None:
if route.request.resource_type == "image":
await route.abort()
else:
await route.fallback()
await page.route("**/*", abort_images_handler)
請注意新方法 page.route_from_har() 和 browser_context.route_from_har() 也參與路由並可以被延遲執行。
Web-First 斷言更新
- 新方法 expect(locator).to_have_values() 用於斷言
<select multiple>
元素的所有選定值。 - Methods expect(locator).to_contain_text() 和 expect(locator).to_have_text() 現在接受
ignore_case
選項。
其他
-
如果有阻礙您的 Service Worker,您現在可以透過新的上下文選項
service_workers
輕鬆停用它:- Sync
- Async
context = browser.new_context(service_workers="block")
page = context.new_page()context = await browser.new_context(service_workers="block")
page = await context.new_page() -
Using
.zip
path forrecordHar
context 選項 自動壓縮 the resulting HAR:- Sync
- Async
context = browser.new_context(record_har_path="github.har.zip")
context = await browser.new_context(record_har_path="github.har.zip")
-
如果您打算手動編輯 HAR,請考慮使用僅記錄重播所需基本資訊的
"minimal"
HAR 記錄模式:- Sync
- Async
context = browser.new_context(record_har_mode="minimal", record_har_path="har.har")
context = await browser.new_context(record_har_mode="minimal", record_har_path="har.har")
-
Playwright 現在在 Ubuntu 22 amd64 和 Ubuntu 22 arm64 上執行。
版本 1.22
重點
-
角色選擇器,允許依據 ARIA role、ARIA attributes 和 accessible 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
buttons = page.locator("role=button")
# ...
submit_button = buttons.filter(has_text="Submit")
submit_button.click() -
Codegen 現在支援generating Pytest Tests
版本 1.21
重點
-
新的角色選擇器,允許依據 ARIA role、ARIA attributes 和 accessible name 選擇元素。
- Sync
- Async
# Click a button with accessible name "log in"
page.locator("role=button[name='log in']").click()# Click a button with accessible name "log in"
await page.locator("role=button[name='log in']").click()在我們的文件中閱讀更多。
-
New
scale
選項 in page.screenshot() for smaller sized screenshots. -
New
caret
選項 in page.screenshot() 控制 text caret. Defaults to"hide"
.
行為變更
- The
mcr.microsoft.com/playwright
docker image no longer contains Python. Please usemcr.microsoft.com/playwright/python
as a Playwright-ready docker image with pre-installed Python. - Playwright 現在支援large file uploads (100s of MBs) via locator.set_input_files() API.
瀏覽器版本
- Chromium 101.0.4951.26
- Mozilla Firefox 98.0.2
- WebKit 15.4
此版本也對以下穩定頻道進行了測試:
- Google Chrome 100
- Microsoft Edge 100
版本 1.20
重點
- 新選項s用於方法page.screenshot(), locator.screenshot() 和 element_handle.screenshot():
- 選項
animations: "disabled"
將所有 CSS 動畫和轉場重置為一致狀態 - 選項
mask: Locator[]
遮罩指定的元素,在其上方覆蓋粉色#FF00FF
方框。
- 選項
- Trace Viewer現在shows API testing requests.
- locator.highlight()視覺化地顯示element(s)以便更輕鬆地debugging.
公告
- 我們現在發佈專用的 Python docker 映像
mcr.microsoft.com/playwright/python
。如果您使用 Python,請切換到該映像。這是最後一個在我們的 javascriptmcr.microsoft.com/playwright
docker 映像中包含 Python 的版本。 - v1.20 是最後一個為 macOS 10.15 Catalina 接收 WebKit 更新的版本。請更新 macOS 以繼續使用最新的 WebKit!
瀏覽器版本
- Chromium 101.0.4921.0
- Mozilla Firefox 97.0.1
- WebKit 15.4
此版本也對以下穩定頻道進行了測試:
- Google Chrome 99
- Microsoft Edge 99
版本 1.19
重點
-
Locator 現在支援
has
選項,確保其包含另一個內部定位器:- Sync
- Async
page.locator("article", has=page.locator(".highlight")).click()
await page.locator("article", has=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 Python 1.18 引入新的 API 測試,讓您可以直接從 Python 向伺服器發送請求!現在您可以:
- 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
要代表 Playwright 的 Page 執行請求,請使用新的 page.request API:
- Sync
- Async
# Do a GET request on behalf of page
res = page.request.get("http://example.com/foo.json")
# Do a GET request on behalf of page
res = await page.request.get("http://example.com/foo.json")
在我們的文件中閱讀更多。
Web-First 斷言
Playwright for Python 1.18 引入了 Web-First 斷言。
請考慮以下範例:
- Sync
- Async
from playwright.sync_api import Page, expect
def test_status_becomes_submitted(page: Page) -> None:
# ..
page.locator("#submit-button").click()
expect(page.locator(".status")).to_have_text("Submitted")
from playwright.async_api import Page, expect
async def test_status_becomes_submitted(page: Page) -> None:
# ..
await page.locator("#submit-button").click()
await expect(page.locator(".status")).to_have_text("Submitted")
Playwright 會使用選擇器 .status
重複測試節點,直到獲取的節點具有 "Submitted"
文字。它會重複獲取節點並反覆檢查,直到滿足條件或達到逾時為止。您可以將此逾時作為選項傳遞。
在我們的文件中閱讀更多。
定位器改進
-
每個定位器現在可以選擇性地依其包含的文字進行篩選:
- Sync
- Async
page.locator("li", has_text="my item").locator("button").click()
await page.locator("li", has_text="my item").locator("button").click()
在定位器文件中閱讀更多
新的 API 與變更
accept_downloads
選項現在預設為True
。sources
選項 to embed sources into traces.
瀏覽器版本
- 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 斷言。
框架定位器可以使用 page.frame_locator() 或 locator.frame_locator() 方法建立。
locator = page.frame_locator("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 列
HTML Report Update
- HTML 報告現在支援動態篩選
- 報告現在是單一靜態 HTML 檔案,可以透過電子郵件或作為 slack 附件傳送。
Ubuntu ARM64 支援與更多功能
-
Playwright 現在支援 Ubuntu 20.04 ARM64。您現在可以在 Apple M1 的 Docker 內和 Raspberry Pi 上執行 Playwright 測試。
-
您現在可以使用 Playwright 在 Linux 上安裝穩定版本的 Edge:
npx playwright install msedge
新的 API
版本 1.16
🎭 Playwright Library
locator.wait_for
等待定位器解析為具有給定狀態的單一元素。預設為 state: 'visible'
。
在處理清單時特別有用:
order_sent = page.locator("#order-sent")
order_sent.wait_for()
Docker support for Arm64
Playwright Docker 映像現在已發佈 Arm64 版本,因此可以在 Apple Silicon 上使用。
🎭 Playwright Trace Viewer
- run trace viewer with
npx playwright show-trace
and drop trace files to the trace viewer PWA - better visual attribution of action targets
瀏覽器版本
- 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
以前無法取得回應的多個標頭值。現在這是可能的,並且有額外的輔助函數可用:
- request.all_headers()
- request.headers_array()
- request.header_value()
- response.all_headers()
- response.headers_array()
- response.header_value()
- response.header_values()
🌈 Forced-Colors emulation
現在可以透過在 browser.new_context() 中傳遞它或呼叫 page.emulate_media() 來模擬 forced-colors
CSS 媒體功能。
新的 API
- page.route() 接受新的
times
選項,以指定此路由應匹配多少次。 - page.set_checked() 和 locator.set_checked() 被引入以設定the checked state of a checkbox.
- request.sizes() 回傳 resource size information for given http request.
- tracing.start_chunk() - Start a new trace chunk.
- tracing.stop_chunk() - Stops a new trace chunk.
瀏覽器版本
- Chromium 96.0.4641.0
- Mozilla Firefox 92.0
- WebKit 15.0
版本 1.14
⚡️ New "strict" mode
選擇器歧義是自動化測試中的常見問題。「嚴格」模式確保您的選擇器指向單一元素,否則會拋出例外。
在您的動作呼叫中傳遞 strict=true
以啟用。
# This will throw if you have more than one button!
page.click("button", strict=True)
📍 新的 Locators API
Locator 代表頁面上元素的檢視。它捕獲在任何時刻擷取元素所需的邏輯。
Locator 和 ElementHandle 之間的差異在於,後者指向特定元素,而 Locator 捕獲如何擷取該元素的邏輯。
Also, locators are "strict" by default!
locator = page.locator("button")
locator.click()
在文件中了解更多。
🧩 Experimental React 和 Vue selector engines
React 和 Vue 選擇器允許按組件名稱和/或屬性值選擇元素。語法與屬性選擇器非常相似,並支援所有屬性選擇器運算子。
page.locator("_react=SubmitButton[enabled=true]").click()
page.locator("_vue=submit-button[enabled=true]").click()
在 react 選擇器文件 和 vue 選擇器文件 中了解更多。
✨ New nth
和 visible
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
- 🖖 Programmatic drag-and-drop support via the page.drag_and_drop() API.
- 🔎 Enhanced HAR具有請求和回應的主體大小. 透過
recordHar
選項 in browser.new_context(). 使用
工具
- 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
- new
baseURL
選項 in browser.new_context() 和 browser.new_page() - response.security_details() 和 response.server_addr()
- page.drag_and_drop() 和 frame.drag_and_drop()
- download.cancel()
- page.input_value(), frame.input_value() 和 element_handle.input_value()
- new
force
選項 in page.fill(), frame.fill(), 和 element_handle.fill() - new
force
選項 in page.select_option(), frame.select_option(), 和 element_handle.select_option()
版本 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 browser_context.tracing API:
browser = chromium.launch()
context = browser.new_context()
# Start tracing before creating / navigating a page.
context.tracing.start(screenshots=True, snapshots=True)
page.goto("https://playwright.dev")
# Stop tracing and export it into a zip archive.
context.tracing.stop(path = "trace.zip")
Traces are examined later with the Playwright CLI:
playwright show-trace trace.zip
That will open the following GUI:
👉 在追蹤檢視器文件中閱讀更多。
瀏覽器版本
- Chromium 93.0.4530.0
- Mozilla Firefox 89.0
- WebKit 14.2
此版本也對以下穩定頻道進行了測試:
- Google Chrome 91
- Microsoft Edge 91
新的 API
reducedMotion
選項 in page.emulate_media(), browser_type.launch_persistent_context(), browser.new_context() 和 browser.new_page()- browser_context.on("request")
- browser_context.on("requestfailed")
- browser_context.on("requestfinished")
- browser_context.on("response")
tracesDir
選項 in browser_type.launch() 和 browser_type.launch_persistent_context()- new browser_context.tracing API namespace
- new download.page 方法
版本 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
- support for async predicates across the API in methods such as page.expect_request() and others
- new emulation devices: Galaxy S8, Galaxy S9+, Galaxy Tab S4, Pixel 3, Pixel 4
- new methods:
- page.wait_for_url() to await navigations to URL
- video.delete() 和 video.save_as() to manage screen recording
- new options:
screen
選項 in the browser.new_context() 方法 to emulatewindow.screen
dimensionsposition
選項 in page.check() 和 page.uncheck() methodstrial
選項 to dry-run actions in page.check(), page.uncheck(), page.click(), page.dblclick(), page.hover() 和 page.tap()
版本 1.10
- Playwright for Java v1.10 is now stable!
- Run Playwright against Google Chrome and Microsoft Edge stable channels with the new channels API.
- Chromium 截圖在 Mac 和 Windows 上快速。
Bundled Browser Versions
- Chromium 90.0.4430.0
- Mozilla Firefox 87.0b10
- WebKit 14.2
此版本也對以下穩定頻道進行了測試:
- Google Chrome 89
- Microsoft Edge 89
新的 API
- browser_type.launch() 現在接受新的
'channel'
選項。在我們的文件中閱讀更多。
版本 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
-
基於佈局選擇元素,使用
:left-of()
、:right-of()
、:above()
和:below()
。 -
Playwright現在includes command line interface, former playwright-cli.
playwright --help
-
page.select_option() 現在會等待選項出現。
-
新方法s to assert element state like page.is_editable().
新的 API
- element_handle.is_checked().
- element_handle.is_disabled().
- element_handle.is_editable().
- element_handle.is_enabled().
- element_handle.is_hidden().
- element_handle.is_visible().
- page.is_checked().
- page.is_disabled().
- page.is_editable().
- page.is_enabled().
- page.is_hidden().
- page.is_visible().
- 新選項
'editable'
in element_handle.wait_for_element_state().
瀏覽器版本
- Chromium 90.0.4392.0
- Mozilla Firefox 85.0b5
- WebKit 14.1
版本 1.7
- 新 Java SDK:Playwright for Java 現在與 JavaScript、Python 和 .NET bindings 並駕齊驅。
- 瀏覽器儲存 API:新的便利 API 用於儲存和載入瀏覽器儲存狀態(cookies、本地儲存),以簡化驗證的自動化場景。
- 新的 CSS 選擇器:我們聽到了您對更靈活選擇器的回饋意見,並改良了選擇器實作。Playwright 1.7 引入了新的 CSS 擴充功能,更多功能即將推出。
- 新網站:playwright.dev 的文件網站已更新,現在使用 Docusaurus 建置。
- 支援Apple Silicon:WebKit 和 Chromium 的 Playwright 瀏覽器二進位檔現在為 Apple Silicon 建置。
新的 API
- browser_context.storage_state() 用於取得目前狀態以供稍後重複使用。
- browser.new_context() 和 browser.new_page() 中的
storageState
選項用於設定瀏覽器上下文狀態。
瀏覽器版本
- Chromium 89.0.4344.0
- Mozilla Firefox 84.0b9
- WebKit 14.1