playwright-dotnet icon indicating copy to clipboard operation
playwright-dotnet copied to clipboard

[Feature] Suggestion: make installation easier, for example add InstallWhenNeeded

Open 304NotModified opened this issue 3 years ago • 6 comments

Hi,

I'm trying to get Playwright working in multiple teams. But different setup/code bases makes the installation a bit of a headache.

And then I had an idea, why don't we just make it supereasy? For example, add an option, like InstallWhenNeeded to BrowserTypeLaunchOptions that installs the browser when needed.

This will prevent then:

Executable doesn't exist Looks like Playwright Test or Playwright was just installed or updated. Please run the following command to download new browsers:

Proposal

My proposal, and feel free to use another name for InstallWhenNeeded

//  No install somewhere, will  do internally Program.Main(new[] { "install", "chromium" });
var browser = await playwright.Chromium.LaunchAsync();

// This should still work (manual install before launch)
Program.Main(new[] { "install", "chromium" }); 
var browser = await playwright.Chromium.LaunchAsync();

// Won't do internally Program.Main(new[] { "install", "chromium" });  - so old behavior. So crash if the browser aren't installed yet
var options = new BrowserTypeLaunchOptions { InstallWhenNeeded = false}; 
var browser = await playwright.Chromium.LaunchAsync(options);

Current work around

This is currently my work around to make things super easy:

private static async Task<IBrowser> LaunchChromiumBrowser(IPlaywright playwright, BrowserTypeLaunchOptions options)
{
    IBrowser browser;
    try
    {
        browser = await playwright.Chromium.LaunchAsync(options);
    }
    catch (PlaywrightException e) when (e.Message.Contains("Executable doesn't exist"))
    {
        Program.Main(new[] { "install", "chromium" });
        browser = await playwright.Chromium.LaunchAsync(options);
    }
    return browser;
}

Current work around - Generic version:

private static async Task<IBrowser> LaunchBrowser(IBrowserType browserType, BrowserTypeLaunchOptions options)
{
    IBrowser browser;
    try
    {
        browser = await browserType.LaunchAsync(options);
    }
    catch (PlaywrightException e) when (e.Message.Contains("Executable doesn't exist"))
    {
        Program.Main(new[] { "install", browserType.Name });
        browser = await browserType.LaunchAsync(options);
    }
    return browser;
}

And yes, it make the first test slow. But if that's an issue, you could still install it manually like before.

related: #990, #1692,#1793, #1822, #2006, #2181, #2198

304NotModified avatar Jun 01 '22 21:06 304NotModified

Calling Program.Main(new[] { "install", "chromium" }); is essentially "install if needed". It will do internally check if the corresponding browser of the Playwright version is installed.

Would that work for you?

mxschmitt avatar Jun 04 '22 15:06 mxschmitt

Calling Program.Main(new[] { "install", "chromium" });

I'm already calling that?

Or do you mean:

private static async Task<IBrowser> LaunchBrowser(IBrowserType browserType, BrowserTypeLaunchOptions options)
{
  Program.Main(new[] { "install", browserType.Name });
  return await browserType.LaunchAsync(options);
}

304NotModified avatar Jun 07 '22 14:06 304NotModified

No, Program.Main(new[] { "install", "chromium" }); will check if the browsers are there and install them if they are not installed. See here: https://playwright.dev/docs/cli#install-browsers

mxschmitt avatar Jun 07 '22 16:06 mxschmitt

+1 for this one. so i can get rid of this https://github.com/VerifyTests/Verify.HeadlessBrowsers/blob/main/src/Verify.Playwright/VerifyPlaywright.cs#L7

SimonCropp avatar Jun 07 '22 20:06 SimonCropp

@mxschmitt I'm not sure if you get the idea of this feature request.

It should make things easier, and preferabele, working out of the box. So there is no need to run the obsolete CLI tool, Powershell script or a special call to Program.Main.

+1 for this one. so i can get rid of this https://github.com/VerifyTests/Verify.HeadlessBrowsers/blob/main/src/Verify.Playwright/VerifyPlaywright.cs#L7

Exactly :+1:

304NotModified avatar Jun 07 '22 21:06 304NotModified

The proposed workaround does not work on 1.30 or 1.28... the downloaded browsers at ms-playwright appdata (via playwright install) are ignored and instead the currently installed browser in my program files is used. This does not match the documentation and I assume is a bug, because I am not specifying a browser directory in my initialization of playwright.

Billybishop avatar Feb 19 '23 07:02 Billybishop