microsoft / playwright-dotnet

.NET version of the Playwright testing and automation library.
https://playwright.dev/dotnet/
MIT License
2.5k stars 237 forks source link

[Feature]: Add DefaultBrowserType to object return by Playwright.Devices #2990

Closed potnoddle closed 2 months ago

potnoddle commented 2 months ago

🚀 Feature Request

For some reason the DefaultBrowserType is missing from the BrowserNewContextOptions, which is strange as it's found in the source json file; deviceDescriptorsSource.json.

Alternatively is there another way of selecting this value.

Example

using var playwright = await Playwright.CreateAsync();

    // Define the device you want to emulate
    var deviceName = "iPhone 11"; // Change this to the desired device
    var device = playwright.Devices[deviceName];

    // Determine the browser type based on the device
    var browserType = device.DefaultBrowserType switch
    {
        "chromium" => playwright.Chromium,
        "firefox" => playwright.Firefox,
        "webkit" => playwright.Webkit,
        _ => throw new System.Exception("Unsupported browser type")
    };

    // Launch the browser
    await using var browser = await browserType.LaunchAsync(new BrowserTypeLaunchOptions { Headless = false });

Motivation

I need the default browser type by device!

mxschmitt commented 2 months ago

Thank you for filing a feature request. Since in .NET we can't cast a type into another type (class) easily, we were forced to re-use the BrowserNewContextOptions type for the Playwright.Devices Dictionary. We don't feel comfortable to expose DefaultBrowserType on it, since it would mislead users when creating a new context.

I recommend to make a copy of the JSON file an reading it manually as a workaround, since Playwright doesn't change it often over time.