Playwright + Java · Lesson 37 of 38

0/38 done

Module 37: Playwright Configuration

37. Centralized Test Configuration & Device Emulation

16 minlesson

Your task

Implement configureDesktopViewport:

  1. Accept int width and int height.
  2. Return new Browser.NewContextOptions().setViewportSize(width, height).

Write it in the editor, then press Check answer (Ctrl + Enter).

Centralized Configuration in Java

java
public class PlaywrightConfig {
    public static Browser.NewContextOptions getStandardContextOptions() {
        return new Browser.NewContextOptions()
            .setBaseURL("https://staging.targetqa.com")
            .setViewportSize(1920, 1080)
            .setLocale("en-US")
            .setTimezoneId("America/New_York");
    }

    public static Browser.NewContextOptions getMobileDeviceOptions(Playwright playwright) {
        // Device emulation (e.g. iPhone 14):
        return new Browser.NewContextOptions()
            .setViewportSize(390, 844)
            .setUserAgent("Mozilla/5.0 (iPhone; CPU iPhone OS 16_0 like Mac OS X)...")
            .setIsMobile(true)
            .setHasTouch(true);
    }
}

Key Takeaways & Walkthrough Summary

A professional framework maintains configuration in a centralized location. In this lesson, you will configure viewports, mobile device emulation, global timeouts, and environment URLs for staging and production. Let's master Playwright configuration!

Part of the Playwright + Java course (Playwright with Java). Practice this concept live in the Playwright Online Compiler or explore our free automation practice form.

Common Doubts & Lesson Q&A

Can I test mobile responsive layouts in Playwright Java?

Yes! Playwright allows full mobile device emulation including screen size, user-agent, touch events, and device pixel ratio.

Ready? Solve the task in the editor

Use Check answer to mark it complete. Hints are there if you need them.

Java
Skip