跳到主要內容

編寫測試

簡介🔗

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 使用 定位器 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/")

下一步🔗