DeviceDescriptor Class

A device to emulate, as the device descriptors of Playwright (playwright.devices): the size of its viewport and of its screen, its device pixel ratio, whether it is a mobile device with a touch screen, and its user agent string. The library has no list of devices: copy the values of a descriptor of Playwright, or describe your own device.

public sealed class DeviceDescriptor : IEquatable<DeviceDescriptor>

Example

// The values of the "iPhone 13" device of Playwright
var iPhone13 = new DeviceDescriptor
{
    Width = 390,
    Height = 664,
    ScreenWidth = 390,
    ScreenHeight = 844,
    DevicePixelRatio = 3,
    IsMobile = true,
    HasTouch = true,
    UserAgent = "Mozilla/5.0 (iPhone; CPU iPhone OS 15_0 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/15.0 Mobile/15E148 Safari/604.1",
};

using var context = new BrowsingContext(new BrowsingContextOptions
{
    Viewport = iPhone13.CreateViewport(),
    UserAgent = iPhone13.UserAgent,
});
var document = context.Navigate("https://example.com/", _ => "<!DOCTYPE html><p>Hello</p>");

Assert.True(document.DefaultView!.MatchMedia("(pointer: coarse) and (max-device-width: 500px)").Matches);
Assert.Equal(iPhone13.UserAgent, document.DefaultView.Navigator.UserAgent);
Implements

Constructors

NameDescription
DeviceDescriptor()

DeviceDescriptor()

DeviceDescriptor.cs
  • Obsolete: Constructors of types with required members are not supported in this version of your compiler.
[Obsolete("Constructors of types with required members are not supported in this version of your compiler.", true)]
public DeviceDescriptor()

No documentation is available.

Properties

NameDescription
DevicePixelRatioGets or sets the number of device pixels per CSS pixel (deviceScaleFactor in Playwright). The default is 1.
HasTouchGets or sets whether the device has a touch screen (hasTouch in Playwright, see ViewportOptions.HasTouch).
HeightGets or sets the height of the viewport, in CSS pixels (viewport.height in Playwright).
IsMobileGets or sets whether the device is a mobile device (isMobile in Playwright, see ViewportOptions.IsMobile).
ScreenHeightGets or sets the height of the screen, in CSS pixels (screen.height in Playwright), or null for the height of the viewport.
ScreenWidthGets or sets the width of the screen, in CSS pixels (screen.width in Playwright), or null for the width of the viewport.
UserAgentGets or sets the user agent string of the device (userAgent in Playwright, see BrowsingContextOptions.UserAgent), or null.
WidthGets or sets the width of the viewport, in CSS pixels (viewport.width in Playwright).

DevicePixelRatio

DeviceDescriptor.cs

Gets or sets the number of device pixels per CSS pixel (deviceScaleFactor in Playwright). The default is 1.

public double DevicePixelRatio { get; init; }

Property value

double

Gets or sets whether the device has a touch screen (hasTouch in Playwright, see ViewportOptions.HasTouch).

public bool HasTouch { get; init; }

Property value

bool

Gets or sets the height of the viewport, in CSS pixels (viewport.height in Playwright).

public required double Height { get; init; }

Property value

double

Gets or sets whether the device is a mobile device (isMobile in Playwright, see ViewportOptions.IsMobile).

public bool IsMobile { get; init; }

Property value

bool

Gets or sets the height of the screen, in CSS pixels (screen.height in Playwright), or null for the height of the viewport.

public double? ScreenHeight { get; init; }

Property value

double?

Gets or sets the width of the screen, in CSS pixels (screen.width in Playwright), or null for the width of the viewport.

public double? ScreenWidth { get; init; }

Property value

double?

Gets or sets the user agent string of the device (userAgent in Playwright, see BrowsingContextOptions.UserAgent), or null.

public string? UserAgent { get; init; }

Property value

string?

Gets or sets the width of the viewport, in CSS pixels (viewport.width in Playwright).

public required double Width { get; init; }

Property value

double

Methods

NameDescription
CreateViewport(ViewportOptions?)Returns the viewport of the device: the options of viewport (the preferences of the user, the media type...) with the size, the screen, the device pixel ratio and the kind of device of the descriptor.
Equals(DeviceDescriptor?)
Equals(object?)
GetHashCode()
ToString()

CreateViewport(ViewportOptions?)

DeviceDescriptor.cs

Returns the viewport of the device: the options of viewport (the preferences of the user, the media type...) with the size, the screen, the device pixel ratio and the kind of device of the descriptor.

public ViewportOptions CreateViewport(ViewportOptions? viewport = null)

Parameters

viewport ViewportOptions?
The other options of the viewport, or null for ViewportOptions.Default.

Returns

ViewportOptions The options of the viewport, to set in BrowsingContextOptions.Viewport.

Equals overloads

Equals(DeviceDescriptor?)

public bool Equals(DeviceDescriptor? other)
Parameters
other DeviceDescriptor?
Returns

bool

No documentation is available.

Equals(object?)

public override bool Equals(object? obj)
Parameters
obj object?
Returns

bool

No documentation is available.

GetHashCode()

public override int GetHashCode()

Returns

int

No documentation is available.

ToString()

public override string ToString()

Returns

string

No documentation is available.

Operators

NameDescription
operator ==(DeviceDescriptor?, DeviceDescriptor?)
operator !=(DeviceDescriptor?, DeviceDescriptor?)

operator ==(DeviceDescriptor?, DeviceDescriptor?)

public static bool operator ==(DeviceDescriptor? left, DeviceDescriptor? right)

Parameters

left DeviceDescriptor?
right DeviceDescriptor?

Returns

bool

No documentation is available.

operator !=(DeviceDescriptor?, DeviceDescriptor?)

public static bool operator !=(DeviceDescriptor? left, DeviceDescriptor? right)

Parameters

left DeviceDescriptor?
right DeviceDescriptor?

Returns

bool

No documentation is available.