Emulate a Mobile Device in Chrome and Edge
Run a test at phone size with a mobile user agent, device pixel ratio and touch support using Chrome's mobileEmulation option, plus a Firefox fallback.
Problem
The mobile layout has a hamburger menu, a bottom navigation bar and different checkout steps. Resizing the desktop window to 390 px wide is not enough: the app also sniffs the user agent, checks window.devicePixelRatio, and only enables swipe gestures when touch is available.
Solution
Use Chromium’s mobileEmulation option. A named device sets viewport, pixel ratio, user agent and touch in one go; custom metrics let you define any device.
// Named device (names match the DevTools device list)ChromeOptions options = new ChromeOptions();options.setExperimentalOption("mobileEmulation", Map.of("deviceName", "Pixel 7"));WebDriver driver = new ChromeDriver(options);
// Custom deviceChromeOptions custom = new ChromeOptions();custom.setExperimentalOption("mobileEmulation", Map.of( "deviceMetrics", Map.of("width", 390, "height", 844, "pixelRatio", 3.0, "touch", true), "userAgent", "Mozilla/5.0 (iPhone; CPU iPhone OS 18_0 like Mac OS X) AppleWebKit/605.1.15 " + "(KHTML, like Gecko) Version/18.0 Mobile/15E148 Safari/604.1"));WebDriver phone = new ChromeDriver(custom);
phone.get("https://app.example.com");assertTrue(phone.findElement(By.cssSelector("[aria-label='Open menu']")).isDisplayed());# Named deviceoptions = webdriver.ChromeOptions()options.add_experimental_option("mobileEmulation", {"deviceName": "Pixel 7"})driver = webdriver.Chrome(options=options)
# Custom devicecustom = webdriver.ChromeOptions()custom.add_experimental_option("mobileEmulation", { "deviceMetrics": {"width": 390, "height": 844, "pixelRatio": 3.0, "touch": True}, "userAgent": ("Mozilla/5.0 (iPhone; CPU iPhone OS 18_0 like Mac OS X) AppleWebKit/605.1.15 " "(KHTML, like Gecko) Version/18.0 Mobile/15E148 Safari/604.1"),})phone = webdriver.Chrome(options=custom)
phone.get("https://app.example.com")assert phone.find_element(By.CSS_SELECTOR, "[aria-label='Open menu']").is_displayed()const chrome = require('selenium-webdriver/chrome');
// Named deviceconst options = new chrome.Options().setMobileEmulation({ deviceName: 'Pixel 7' });const driver = await new Builder().forBrowser('chrome').setChromeOptions(options).build();
// Custom deviceconst custom = new chrome.Options().setMobileEmulation({deviceMetrics: { width: 390, height: 844, pixelRatio: 3.0, touch: true },userAgent: 'Mozilla/5.0 (iPhone; CPU iPhone OS 18_0 like Mac OS X) AppleWebKit/605.1.15 ' + '(KHTML, like Gecko) Version/18.0 Mobile/15E148 Safari/604.1',});const phone = await new Builder().forBrowser('chrome').setChromeOptions(custom).build();// Named devicevar options = new ChromeOptions();options.EnableMobileEmulation("Pixel 7");var driver = new ChromeDriver(options);
// Custom devicevar custom = new ChromeOptions();custom.EnableMobileEmulation(new ChromiumMobileEmulationDeviceSettings{ Width = 390, Height = 844, PixelRatio = 3.0, EnableTouchEvents = true, UserAgent = "Mozilla/5.0 (iPhone; CPU iPhone OS 18_0 like Mac OS X) AppleWebKit/605.1.15 " + "(KHTML, like Gecko) Version/18.0 Mobile/15E148 Safari/604.1",});var phone = new ChromeDriver(custom);The same options work for Edge (EdgeOptions), since it is Chromium.
Why It Works
mobileEmulation is handled by chromedriver at session start using the same machinery as the DevTools device toolbar. The page sees a mobile viewport, a high devicePixelRatio, the mobile user agent, and ontouchstart in window, so CSS media queries, UA sniffing and touch feature detection all take the mobile path. No CDP calls are needed, so it also works through Selenium Grid.
Firefox and Other Browsers
Firefox has no equivalent option. The practical fallback is to set the window size and override the user agent through a preference:
options = webdriver.FirefoxOptions()options.set_preference("general.useragent.override", "Mozilla/5.0 (Android 15; Mobile; rv:140.0) Gecko/140.0 Firefox/140.0")driver = webdriver.Firefox(options=options)driver.set_window_size(390, 844)This gets you the responsive layout and UA-based logic, but not pixel ratio or touch. For viewport and pixel ratio across browsers, the BiDi browsingContext.setViewport command is the standards-based route.
Gotchas
- Emulation is not a real device. Rendering engine, fonts, virtual keyboard, Safari quirks and real touch latency are all absent. Use a device cloud for a few critical journeys on real hardware. See Cloud Testing Platforms.
- Device names change. Chrome renames and removes entries in its device list over time. If a name stops working, switch to explicit
deviceMetricsso the test does not depend on Chrome’s list. - Window size vs viewport. Do not call
setSize/maximizeafter starting with mobile emulation; it can fight the emulated viewport. - Touch actions. Emulation enables touch events, but
click()still sends mouse-style input. For swipe gestures, use the Actions API with a touch pointer.
Learn the theory
- Browser Options & Configuration Learn how to configure browser options in Selenium WebDriver, including headless mode, window size, and custom arguments.
- Chrome DevTools Protocol (CDP) What CDP gives Selenium on Chromium browsers, how to use it for network throttling, device emulation and performance metrics, why it is version-locked, and when to choose BiDi instead.