This guide shows you how to configure Playwright to emulate the viewport of a Samsung Galaxy phone, so you can test your web app’s mobile layout without needing a physical device. You’ll learn to set the exact screen dimensions, device pixel ratio, and user agent, and how to handle the browser’s mobile emulation mode. This is useful for catching layout issues, touch target problems, and responsive design bugs before your users do.
What You’ll Need
- A Node.js project with Playwright installed (
npm init playwright@latest). - A basic understanding of writing Playwright tests or scripts.
- Optional: a Samsung device or access to the Samsung website to verify the exact viewport values if you need a specific model.
Step-by-Step
- Identify the Samsung device you want to emulate. The most common is the Galaxy S series. For example, the Galaxy S23 has a viewport of 360×780 pixels with a device pixel ratio (DPR) of 3. The Galaxy S24 is similar. If you need a specific model, check the device’s spec sheet or use Playwright’s built-in device descriptors (see step 4).
- Create a Playwright test or script. For a test file (e.g.,
samsung.spec.js), you’ll usetest.use()to apply the emulation. For a standalone script, you’ll pass options tobrowser.newContext(). - Set the viewport size. Use the
viewportoption to set the width and height in CSS pixels. For a Galaxy S23, that’s{ width: 360, height: 780 }. This is the visible area, not the physical screen resolution. - Set the device scale factor (DPR). Use
deviceScaleFactorto simulate the pixel density. For most Samsung phones, this is3. This affects how CSS pixels map to physical pixels, which is critical for testing high-res images and media queries. - Set the user agent. This is important for sites that serve different content based on the browser. You can use a real Samsung user agent string, or use Playwright’s
deviceslist to get a pre-defined one. For example:const { devices } = require('playwright'); const samsung = devices['Galaxy S9+']; // or 'Galaxy S8', etc.Then spread that into your context options. - Enable mobile emulation. Set
isMobile: trueandhasTouch: true. This makes the browser behave like a mobile browser, affecting things like viewport meta tag handling and touch events. Without these, some mobile-specific CSS won’t apply. - Put it all together in a test. Here’s a complete example for a Playwright test file:
const { test, devices } = require('@playwright/test'); const samsung = devices['Galaxy S9+']; // or use your own values test.use({ ...samsung }); test('Samsung viewport test', async ({ page }) => { await page.goto('https://example.com'); // Your assertions here });If you’re writing a script, do:
const { chromium, devices } = require('playwright'); const browser = await chromium.launch(); const context = await browser.newContext({ ...devices['Galaxy S9+'] }); const page = await context.newPage(); await page.goto('https://example.com'); - Run your test. Execute
npx playwright testor run your script. The browser will open with the Samsung viewport, and you can inspect the layout, take screenshots, or run assertions. - Take a screenshot to verify. Add
await page.screenshot({ path: 'samsung.png' })to see the visual result. This is a quick way to confirm the emulation is working.
Common Mistakes
- Using the physical resolution instead of CSS pixels. The Galaxy S23 has a physical resolution of 1080×2340, but the viewport is 360×780. Using the physical resolution will make the page render tiny.
- Forgetting to set
isMobileandhasTouch. Without these, the browser won’t apply mobile-specific behaviors like the viewport meta tag, and touch events won’t fire. - Not setting the device scale factor. If you omit it, the DPR defaults to 1, which will make CSS pixels larger and may trigger desktop media queries.
- Using a generic mobile user agent. Some sites sniff the user agent to serve specific content. A generic iPhone UA might not trigger the Samsung-specific layout.
Tips
- Use Playwright’s built-in device descriptors. They include accurate viewport, DPR, user agent, and mobile flags for many Samsung models. Check the list in the Playwright docs.
- Test multiple Samsung models. The Galaxy S and Note series have different aspect ratios. Use a loop to test a few key models.
- Combine with responsive design testing. Use Playwright’s
page.setViewportSize()to test different widths within the same test, from 360px up to 768px. - Use the
--deviceflag in the CLI. When running tests, you can usenpx playwright test --device="Galaxy S9+"to quickly emulate a device without writing code.
Troubleshooting
- The page looks like a desktop version. Check that
isMobileistrueand that the viewport width is less than 768px. Also, make sure the user agent is a mobile one. - The layout is too wide. You might be using the physical resolution. Set the viewport to 360×780, not 1080×2340.
- Images are blurry. Set
deviceScaleFactorto 3. If it’s 1, the browser will render at a lower resolution. - Touch events don’t work. Ensure
hasTouchistrue. Also, if you’re usingpage.tap(), it requires touch emulation. - The user agent is being overridden. If you’re using a device descriptor, it includes a user agent. If you want to override it, set
userAgentafter spreading the device.
Frequently Asked Questions
Can I emulate any Samsung phone?
Yes, as long as you know the viewport size, DPR, and user agent. Playwright provides descriptors for many models, but you can also manually set values for any device.
What is the difference between viewport and screen size?
The viewport is the visible area in CSS pixels, while the screen size is the physical resolution. Playwright uses the viewport for rendering, so you should set the CSS pixel dimensions.
Do I need a real Samsung device for accurate testing?
No, emulation is generally accurate for layout and functionality. However, for performance testing or testing specific hardware features (like the camera), you’ll need a real device.
How do I test in landscape mode?
Set the viewport to have a width greater than height, e.g., { width: 780, height: 360 }. You can also use page.setViewportSize() to switch orientation during a test.
Why does my media query not trigger?
Media queries often rely on viewport width and DPR. Make sure you’ve set the viewport width to a value that matches your breakpoint (e.g., 360px for mobile). Also, ensure isMobile is true, as some queries check for pointer type.
Can I use this for a Samsung tablet?
Yes, but tablets have different viewport sizes. For example, the Galaxy Tab S7 has a viewport of 800×1280. Set the viewport accordingly.
Related
General steps for current mainstream versions; menus can differ by device and version. See the disclaimer or report an error.
More in this bed
- Fix Error 0x800703e6: 5 Working Methods
- Install Android Apps on SD Card: Full Guide
- Why Is My Standby Red on iPhone? Fixes
- How to Use Multiple Desktops on Mac (Spaces Guide)
- How to Alter Galaxy Note 8 Text Message App
- Set iPhone Media Volume Limit: Step-by-Step