microsoft / playwright-dotnet

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

[BUG] Issue with Remote Cloud Integration with Nunit #2475

Open princebaretto99 opened 1 year ago

princebaretto99 commented 1 year ago

Context:

Hi Team,

We are trying to inject the remote browser into playwright-nunit framework. While doing so we noticed that the Browser never gets closed using Browser.close() which results in "Abrupt Close" at the remote cloud's end. We have rewritten the BrowserService.cs by extending the IWorkerService and used the browserType.ConnectAsync method with BrowserStack's CDP URL to connect to the remote cloud.
Apart from this we also noticed that the browser gets closed only in cases of errors/assertion errors.

Code Snippet

Below is the code snippet for the rewritten BrowserService.cs

using System.Threading.Tasks;
using Microsoft.Playwright;
using Microsoft.Playwright.NUnit;
using Microsoft.Playwright.TestAdapter;
using Newtonsoft.Json;

internal class BrowserStackService : IWorkerService
{
    public IBrowser Browser { get; internal set; } = null!;

    public static Task<BrowserStackService> Register(WorkerAwareTest test, IBrowserType browserType)
    {
        Dictionary<string, string> browserstackOptions = new Dictionary<string, string>();
        browserstackOptions.Add("os", "osx");
        browserstackOptions.Add("os_version", "catalina");
        browserstackOptions.Add("browser", "chrome");
        browserstackOptions.Add("browserstack.username", "BROWSERSTACK_USERNAME");
        browserstackOptions.Add("browserstack.accessKey", "BROWSERSTACK_ACCESS_KEY");

        string capsJson = JsonConvert.SerializeObject(browserstackOptions);
        string cdpUrl = "wss://cdp.browserstack.com/playwright?caps=" + Uri.EscapeDataString(capsJson);

        return test.RegisterService("Browser", async () => new BrowserStackService
        {
            Browser = await browserType.ConnectAsync(cdpUrl).ConfigureAwait(false)
        }) ;
    }

    public Task ResetAsync() => Task.CompletedTask;
    public Task DisposeAsync() => Browser.CloseAsync();
}

Please let me know your thoughts on this.

sreenikarthik commented 1 year ago

@mxschmitt, any updates on this one?

karanshah-browserstack commented 1 year ago

@mxschmitt @pavelfeldman @avodovnik Any updates on suggestion on how this could be done? As per our investigation this is the line of code causing the issue https://github.com/microsoft/playwright-dotnet/blob/main/src/Playwright.NUnit/WorkerAwareTest.cs#L78 . If we can add a check for TestOk() and connection.isRemote(). But the connection or channel object is not available here. We would be happy to contribute too, can someone help on this?