java.lang.Object
net.serenitybdd.screenplay.playwright.interactions.EmulateDevice
All Implemented Interfaces:
net.serenitybdd.core.SkipNested, net.serenitybdd.screenplay.Performable

public class EmulateDevice extends Object implements net.serenitybdd.screenplay.Performable
Emulate different devices by setting viewport size, user agent, and device characteristics.

Sample usage:

     // Emulate predefined devices
     actor.attemptsTo(EmulateDevice.iPhone12());
     actor.attemptsTo(EmulateDevice.iPadPro());
     actor.attemptsTo(EmulateDevice.pixel5());
     actor.attemptsTo(EmulateDevice.galaxyS21());

     // Set custom viewport
     actor.attemptsTo(EmulateDevice.withViewport(1920, 1080));

     // Emulate mobile with touch
     actor.attemptsTo(
         EmulateDevice.withViewport(375, 667)
             .withDeviceScaleFactor(2)
             .asMobile()
     );

     // Desktop viewport
     actor.attemptsTo(EmulateDevice.desktop());
     actor.attemptsTo(EmulateDevice.desktopHD());
     actor.attemptsTo(EmulateDevice.desktop4K());
 

Note: For full device emulation including user agent and other properties, configure the browser context when creating the actor's ability.

  • Method Details

    • withViewport

      public static EmulateDevice withViewport(int width, int height)
      Set a custom viewport size.
      Parameters:
      width - Viewport width in pixels
      height - Viewport height in pixels
    • iPhone12

      public static EmulateDevice iPhone12()
      Emulate iPhone 12 / 12 Pro viewport.
    • iPhone14Pro

      public static EmulateDevice iPhone14Pro()
      Emulate iPhone 14 Pro viewport.
    • iPhoneSE

      public static EmulateDevice iPhoneSE()
      Emulate iPhone SE viewport.
    • iPad

      public static EmulateDevice iPad()
      Emulate iPad viewport.
    • iPadPro

      public static EmulateDevice iPadPro()
      Emulate iPad Pro 11" viewport.
    • pixel5

      public static EmulateDevice pixel5()
      Emulate Google Pixel 5 viewport.
    • pixel7

      public static EmulateDevice pixel7()
      Emulate Google Pixel 7 viewport.
    • galaxyS21

      public static EmulateDevice galaxyS21()
      Emulate Samsung Galaxy S21 viewport.
    • galaxyTabS7

      public static EmulateDevice galaxyTabS7()
      Emulate Samsung Galaxy Tab S7 viewport.
    • desktop

      public static EmulateDevice desktop()
      Standard desktop viewport (1280x720).
    • desktopHD

      public static EmulateDevice desktopHD()
      HD desktop viewport (1920x1080).
    • desktop4K

      public static EmulateDevice desktop4K()
      4K desktop viewport (3840x2160).
    • laptop

      public static EmulateDevice laptop()
      Laptop viewport (1366x768).
    • macBookPro14

      public static EmulateDevice macBookPro14()
      MacBook Pro 14" viewport (1512x982).
    • withDeviceScaleFactor

      public EmulateDevice withDeviceScaleFactor(double factor)
      Set the device scale factor (pixel density).
      Parameters:
      factor - The device pixel ratio (e.g., 2 for Retina displays)
    • asMobile

      public EmulateDevice asMobile()
      Mark as a mobile device (enables mobile-specific behaviors).
    • withTouch

      public EmulateDevice withTouch()
      Enable touch events.
    • performAs

      public <T extends net.serenitybdd.screenplay.Actor> void performAs(T actor)
      Specified by:
      performAs in interface net.serenitybdd.screenplay.Performable
    • getWidth

      public int getWidth()
      Get the viewport width.
    • getHeight

      public int getHeight()
      Get the viewport height.
    • getDeviceScaleFactor

      public Double getDeviceScaleFactor()
      Get the device scale factor (if set).
    • isMobile

      public Boolean isMobile()
      Check if this is configured as a mobile device.