A high-level API to automate web browsers across Chromium, Firefox and WebKit with both synchronous and asynchronous execution models.
—
Emulate mobile devices, tablets, and various screen configurations with pre-configured device profiles, touch interaction, and mobile-specific features.
Emulate mobile devices using predefined device profiles or custom configurations.
class Playwright:
devices: Dict[str, Dict[str, Any]]
"""Pre-configured device profiles (iPhone, iPad, Android, etc.)"""
class BrowserContext:
def set_geolocation(self, geolocation: Optional[Geolocation]) -> None:
"""
Set device geolocation.
Args:
geolocation: GPS coordinates (None to clear)
"""
def grant_permissions(
self,
permissions: List[str],
origin: Optional[str] = None
) -> None:
"""
Grant device permissions.
Args:
permissions: Permission names (geolocation, camera, microphone, etc.)
origin: Origin to grant permissions for
"""
def clear_permissions(self) -> None:
"""Clear all granted permissions."""
def set_offline(self, offline: bool) -> None:
"""
Simulate offline mode.
Args:
offline: True for offline mode
"""Touch-based interaction for mobile interfaces.
class Page:
touchscreen: Touchscreen
class Touchscreen:
def tap(self, x: float, y: float) -> None:
"""
Tap at coordinates.
Args:
x: X coordinate
y: Y coordinate
"""Browser context options for mobile emulation.
class Browser:
def new_context(
self,
viewport: Optional[ViewportSize] = None,
device_scale_factor: Optional[float] = None,
is_mobile: Optional[bool] = None,
has_touch: Optional[bool] = None,
user_agent: Optional[str] = None,
geolocation: Optional[Geolocation] = None,
permissions: Optional[List[str]] = None,
color_scheme: Optional[str] = None,
reduced_motion: Optional[str] = None,
**kwargs
) -> BrowserContext:
"""
Create mobile-configured browser context.
Mobile-specific args:
viewport: Screen size (width, height)
device_scale_factor: Device pixel ratio
is_mobile: Enable mobile viewport
has_touch: Enable touch events
user_agent: Mobile user agent string
geolocation: GPS coordinates
permissions: Mobile permissions
color_scheme: 'light', 'dark', 'no-preference'
reduced_motion: 'reduce', 'no-preference'
"""from playwright.sync_api import sync_playwright
with sync_playwright() as p:
# Use predefined iPhone device
iphone = p.devices["iPhone 12"]
browser = p.webkit.launch()
context = browser.new_context(**iphone)
page = context.new_page()
page.goto("https://example.com")
# Touch interactions
page.touchscreen.tap(100, 200)
# Mobile-specific assertions
viewport = page.viewport_size
assert viewport["width"] == 390 # iPhone 12 width
browser.close()with sync_playwright() as p:
browser = p.chromium.launch()
# Custom mobile setup
context = browser.new_context(
viewport={"width": 412, "height": 915},
device_scale_factor=2.625,
is_mobile=True,
has_touch=True,
user_agent="Mozilla/5.0 (Linux; Android 10) Mobile",
geolocation={"latitude": 37.7749, "longitude": -122.4194},
permissions=["geolocation"]
)
page = context.new_page()
page.goto("https://maps.google.com")
# Verify mobile viewport
is_mobile = page.evaluate("window.innerWidth < 768")
assert is_mobile
browser.close()with sync_playwright() as p:
browser = p.chromium.launch()
context = browser.new_context(**p.devices["Pixel 5"])
# Set location to New York
context.set_geolocation({
"latitude": 40.7128,
"longitude": -74.0060
})
context.grant_permissions(["geolocation"])
page = context.new_page()
page.goto("https://weather.com")
# Wait for location-based content
page.wait_for_selector("[data-testid='location-name']")
location = page.locator("[data-testid='location-name']").text_content()
assert "New York" in location
browser.close()with sync_playwright() as p:
browser = p.chromium.launch()
# Test multiple devices
devices_to_test = [
"iPhone 12",
"iPad Pro",
"Pixel 5",
"Galaxy S9+"
]
for device_name in devices_to_test:
device = p.devices[device_name]
context = browser.new_context(**device)
page = context.new_page()
page.goto("https://responsive-site.com")
# Take device-specific screenshot
page.screenshot(path=f"screenshot-{device_name.replace(' ', '-')}.png")
# Test device-specific features
if device.get("is_mobile"):
# Mobile-specific tests
hamburger_menu = page.locator(".mobile-menu-toggle")
assert hamburger_menu.is_visible()
else:
# Tablet/desktop tests
desktop_nav = page.locator(".desktop-navigation")
assert desktop_nav.is_visible()
context.close()
browser.close()with sync_playwright() as p:
browser = p.chromium.launch()
context = browser.new_context(**p.devices["iPhone 12"])
page = context.new_page()
# Load page while online
page.goto("https://pwa-app.com")
# Verify online functionality
online_indicator = page.locator(".online-status")
assert online_indicator.text_content() == "Online"
# Go offline
context.set_offline(True)
# Test offline functionality
page.reload()
offline_indicator = page.locator(".offline-status")
assert offline_indicator.is_visible()
# Test cached content still works
cached_content = page.locator(".cached-article")
assert cached_content.is_visible()
browser.close()Install with Tessl CLI
npx tessl i tessl/pypi-playwright