模擬
簡介
使用 Playwright,你可以在任何瀏覽器上測試你的應用程式,並模擬真實設備,例如手機或平板電腦。只需配置你想要模擬的設備,Playwright 就會模擬瀏覽器行為,例如 "userAgent"
、"screenSize"
、"viewport"
以及是否啟用 "hasTouch"
。你還可以模擬 "geolocation"
、"locale"
和 "timezone"
,對所有測試或特定測試進行設定,並設置 "permissions"
來顯示通知或更改 "colorScheme"
。
裝置
Playwright 附帶一個設備參數註冊表,使用 playwright.devices 為選定的桌面、平板和移動設備。它可以用來模擬特定設備的瀏覽器行為,例如使用者代理、螢幕大小、視窗大小以及是否啟用了觸控。所有測試將使用指定的設備參數進行。
- Sync
- Async
from playwright.sync_api import sync_playwright, Playwright
def run(playwright: Playwright):
iphone_13 = playwright.devices['iPhone 13']
browser = playwright.webkit.launch(headless=False)
context = browser.new_context(
**iphone_13,
)
with sync_playwright() as playwright:
run(playwright)
import asyncio
from playwright.async_api import async_playwright, Playwright
async def run(playwright: Playwright):
iphone_13 = playwright.devices['iPhone 13']
browser = await playwright.webkit.launch(headless=False)
context = await browser.new_context(
**iphone_13,
)
async def main():
async with async_playwright() as playwright:
await run(playwright)
asyncio.run(main())
視口 (Viewport)
視口包含在設備中,但您可以在某些測試中使用 page.set_viewport_size() 來覆蓋它。
測試檔案:
在測試文件內部也是一樣的。
- Sync
- Async
# Create context with given viewport
context = browser.new_context(
viewport={ 'width': 1280, 'height': 1024 }
)
# Resize viewport for individual page
page.set_viewport_size({"width": 1600, "height": 1200})
# Emulate high-DPI
context = browser.new_context(
viewport={ 'width': 2560, 'height': 1440 },
device_scale_factor=2,
)
# Create context with given viewport
context = await browser.new_context(
viewport={ 'width': 1280, 'height': 1024 }
)
# Resize viewport for individual page
await page.set_viewport_size({"width": 1600, "height": 1200})
# Emulate high-DPI
context = await browser.new_context(
viewport={ 'width': 2560, 'height': 1440 },
device_scale_factor=2,
)
isMobile
無論是否考慮 meta viewport 標籤並啟用觸控事件。
- Sync
- Async
context = browser.new_context(
isMobile=false
)
context = await browser.new_context(
isMobile=false
)
Locale & Timezone
模擬使用者的區域和時區,可以在配置中全域設定所有測試,然後為特定測試覆蓋設定。
- Sync
- Async
context = browser.new_context(
locale='de-DE',
timezone_id='Europe/Berlin',
)
context = await browser.new_context(
locale='de-DE',
timezone_id='Europe/Berlin',
)
權限
允許應用程式顯示 系統通知。
- Sync
- Async
context = browser.new_context(
permissions=['notifications'],
)
context = await browser.new_context(
permissions=['notifications'],
)
允許特定域的通知。
- Sync
- Async
context.grant_permissions(['notifications'], origin='https://skype.com')
await context.grant_permissions(['notifications'], origin='https://skype.com')
撤銷所有權限,請使用 browser_context.clear_permissions()。
- Sync
- Async
context.clear_permissions()
await context.clear_permissions()
地理位置
授予 "geolocation" 權限並將 geolocation 設定到特定區域。
- Sync
- Async
context = browser.new_context(
geolocation={"longitude": 41.890221, "latitude": 12.492348},
permissions=["geolocation"]
)
context = await browser.new_context(
geolocation={"longitude": 41.890221, "latitude": 12.492348},
permissions=["geolocation"]
)
稍後更改位置:
- Sync
- Async
context.set_geolocation({"longitude": 48.858455, "latitude": 2.294474})
await context.set_geolocation({"longitude": 48.858455, "latitude": 2.294474})
注意 您只能更改所有頁面的地理位置。
色彩方案和媒體
模擬用戶的“colorScheme”。支持的值為 'light'、'dark'、'no-preference'。你也可以使用 page.emulate_media() 模擬媒體類型。
- Sync
- Async
# Create context with dark mode
context = browser.new_context(
color_scheme='dark' # or 'light'
)
# Create page with dark mode
page = browser.new_page(
color_scheme='dark' # or 'light'
)
# Change color scheme for the page
page.emulate_media(color_scheme='dark')
# Change media for page
page.emulate_media(media='print')
# Create context with dark mode
context = await browser.new_context(
color_scheme='dark' # or 'light'
)
# Create page with dark mode
page = await browser.new_page(
color_scheme='dark' # or 'light'
)
# Change color scheme for the page
await page.emulate_media(color_scheme='dark')
# Change media for page
await page.emulate_media(media='print')
使用者代理
使用者代理包含在裝置中,因此您很少需要更改它。但是,如果您需要測試不同的使用者代理,您可以使用 userAgent
屬性來覆寫它。
- Sync
- Async
context = browser.new_context(
user_agent='My user agent'
)
context = await browser.new_context(
user_agent='My user agent'
)
離線
模擬網路離線。
- Sync
- Async
context = browser.new_context(
offline=True
)
context = await browser.new_context(
offline=True
)
啟用 JavaScript
模擬使用者在 JavaScript 被停用的情境。
- Sync
- Async
context = browser.new_context(
java_script_enabled=False
)
context = await browser.new_context(
java_script_enabled=False
)