模拟
简介
使用 Playwright,您可以在任何浏览器上测试您的应用程序,并模拟真实设备,例如手机或平板电脑。 只需配置您想要模拟的设备,Playwright 将模拟浏览器行为,例如 "userAgent"
、"screenSize"
、"viewport"
以及是否启用 "hasTouch"
。 您还可以为所有测试或特定测试模拟 "geolocation"
、"locale"
和 "timezone"
,以及设置 "permissions"
以显示通知或更改 "colorScheme"
。
设备
Playwright 附带一个设备参数注册表,使用 playwright.devices 用于选定的桌面、平板电脑和移动设备。 它可以用于模拟特定设备的浏览器行为,例如用户代理、屏幕尺寸、视口以及是否启用了触摸。 所有测试都将使用指定的设备参数运行。
- 同步
- 异步
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())

视口
视口包含在设备中,但您可以使用 page.set_viewport_size() 为某些测试覆盖它。
测试文件
在测试文件中也适用。
- 同步
- 异步
# 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,
)
是否移动设备
是否考虑 meta 视口标签并启用触摸事件。
- 同步
- 异步
context = browser.new_context(
isMobile=false
)
context = await browser.new_context(
isMobile=false
)
区域设置 & 时区
模拟用户区域设置和时区,可以在配置中全局设置为所有测试,然后为特定测试覆盖。
- 同步
- 异步
context = browser.new_context(
locale='de-DE',
timezone_id='Europe/Berlin',
)
context = await browser.new_context(
locale='de-DE',
timezone_id='Europe/Berlin',
)

权限
允许应用程序显示系统通知。
- 同步
- 异步
context = browser.new_context(
permissions=['notifications'],
)
context = await browser.new_context(
permissions=['notifications'],
)
允许特定域的通知。
- 同步
- 异步
context.grant_permissions(['notifications'], origin='https://skype.com')
await context.grant_permissions(['notifications'], origin='https://skype.com')
使用 browser_context.clear_permissions() 撤销所有权限。
- 同步
- 异步
context.clear_permissions()
await context.clear_permissions()
地理位置
授予 "geolocation"
权限并将地理位置设置为特定区域。
- 同步
- 异步
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"]
)

稍后更改位置
- 同步
- 异步
context.set_geolocation({"longitude": 48.858455, "latitude": 2.294474})
await context.set_geolocation({"longitude": 48.858455, "latitude": 2.294474})
注意 您只能更改上下文中所有页面的地理位置。
颜色方案和媒体
模拟用户的 "colorScheme"
。 支持的值为 'light' 和 'dark'。 您还可以使用 page.emulate_media() 模拟媒体类型。
- 同步
- 异步
# 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
属性覆盖它。
- 同步
- 异步
context = browser.new_context(
user_agent='My user agent'
)
context = await browser.new_context(
user_agent='My user agent'
)
离线
模拟网络离线。
- 同步
- 异步
context = browser.new_context(
offline=True
)
context = await browser.new_context(
offline=True
)
启用 JavaScript
模拟 JavaScript 被禁用的用户场景。
- 同步
- 异步
context = browser.new_context(
java_script_enabled=False
)
context = await browser.new_context(
java_script_enabled=False
)