跳到主要內容

撰寫測試

簡介

Playwright 測試很簡單,它們

  • 執行動作,以及
  • 斷言狀態 符合預期。

在執行動作之前,無需等待任何事物:Playwright 會自動等待各種可操作性檢查通過,然後再執行每個動作。

在執行檢查時,也無需處理競爭條件 - Playwright 斷言的設計方式是描述最終需要滿足的期望。

就是這樣! 這些設計選擇讓 Playwright 使用者可以完全忘記測試中不穩定的逾時和競爭性檢查。

你將學到

第一個測試

請看以下範例,了解如何撰寫測試。 請注意檔名如何遵循 test_ 前綴慣例以及每個測試名稱。

test_example.py
import re
from playwright.sync_api import Page, expect

def test_has_title(page: Page):
page.goto("https://playwright.dev.org.tw/")

# Expect a title "to contain" a substring.
expect(page).to_have_title(re.compile("Playwright"))

def test_get_started_link(page: Page):
page.goto("https://playwright.dev.org.tw/")

# Click the get started link.
page.get_by_role("link", name="Get started").click()

# Expects page to have a heading with the name of Installation.
expect(page.get_by_role("heading", name="Installation")).to_be_visible()

動作

大多數測試將從導航頁面到 URL 開始。 之後,測試將能夠與頁面元素互動。

page.goto("https://playwright.dev.org.tw/")

Playwright 將等待頁面達到載入狀態,然後再繼續。 了解更多關於 page.goto() 選項的資訊。

互動

執行動作從定位元素開始。 Playwright 使用 Locators API 來實現這一點。 定位器代表一種隨時在頁面上尋找元素的方式,了解更多關於不同類型的定位器。 Playwright 將等待元素可操作後再執行動作,因此無需等待它變得可用。

# Create a locator.
get_started = page.get_by_role("link", name="Get started")

# Click it.
get_started.click()

在大多數情況下,它會寫成一行

page.get_by_role("link", name="Get started").click()

基本動作

這是最受歡迎的 Playwright 動作列表。 請注意,還有更多動作,因此請務必查看 Locator API 章節以了解更多資訊。

動作描述
locator.check()勾選輸入核取方塊
locator.click()點擊元素
locator.uncheck()取消勾選輸入核取方塊
locator.hover()將滑鼠懸停在元素上
locator.fill()填寫表單欄位,輸入文字
locator.focus()聚焦元素
locator.press()按下單個按鍵
locator.set_input_files()選擇要上傳的檔案
locator.select_option()在下拉式選單中選擇選項

斷言

Playwright 包含斷言,它們將等待直到滿足預期條件。 使用這些斷言可以使測試變得更穩定且更具彈性。 例如,此程式碼將等待直到頁面標題包含 "Playwright"。

import re
from playwright.sync_api import expect

expect(page).to_have_title(re.compile("Playwright"))

這是最受歡迎的非同步斷言列表。 請注意,還有更多斷言需要熟悉。

斷言描述
expect(locator).to_be_checked()核取方塊已勾選
expect(locator).to_be_enabled()控制項已啟用
expect(locator).to_be_visible()元素可見
expect(locator).to_contain_text()元素包含文字
expect(locator).to_have_attribute()元素具有屬性
expect(locator).to_have_count()元素列表具有給定的長度
expect(locator).to_have_text()元素符合文字
expect(locator).to_have_value()輸入元素具有值
expect(page).to_have_title()頁面具有標題
expect(page).to_have_url()頁面具有 URL

測試隔離

Playwright Pytest 外掛程式基於測試夾具的概念,例如內建的頁面夾具,它會傳遞到您的測試中。 由於瀏覽器上下文,頁面在測試之間是隔離的,這相當於一個全新的瀏覽器設定檔,即使多個測試在單個瀏覽器中執行,每個測試都會獲得一個全新的環境。

test_example.py
from playwright.sync_api import Page

def test_example_test(page: Page):
pass
# "page" belongs to an isolated BrowserContext, created for this specific test.

def test_another_test(page: Page):
pass
# "page" in this second test is completely isolated from the first test.

使用夾具

您可以使用各種夾具在測試之前或之後執行程式碼,並在它們之間共享物件。 例如,具有 autouse 的 function 作用域夾具的行為類似於 beforeEach/afterEach。 而具有 autouse 的 module 作用域夾具的行為類似於 beforeAll/afterAll,它在所有測試之前和所有測試之後執行。

test_example.py
import pytest
from playwright.sync_api import Page, expect

@pytest.fixture(scope="function", autouse=True)
def before_each_after_each(page: Page):

print("before the test runs")

# Go to the starting url before each test.
page.goto("https://playwright.dev.org.tw/")
yield

print("after the test runs")

def test_main_navigation(page: Page):
# Assertions use the expect API.
expect(page).to_have_url("https://playwright.dev.org.tw/")

接下來是什麼