Microsoft.Playwright.PlaywrightException : unable to verify the first certificate Using Playwright C# While connecting Moon

3k Views Asked by At

Trying with below code connecting Moon. Can someone please help me on this? Microsoft.Playwright.PlaywrightException: unable to verify the first certificate Using Playwright

Strack Trace:

onnection.SendMessageToServerAsync[T](String guid, String method, Object args)
    BrowserType.ConnectAsync(String wsEndpoint, BrowserTypeConnectOptions options)
    MoonDotNetCore.InitiateMoonWebDriver() line 68
    GenericAdapter`1.BlockUntilCompleted()
    NoMessagePumpStrategy.WaitForCompletion(AwaitAdapter awaitable)
    AsyncToSyncAdapter.Await(Func`1 invoke)
    SetUpTearDownItem.RunSetUpOrTearDownMethod(TestExecutionContext context, MethodInfo method)
    SetUpTearDownItem.RunSetUp(TestExecutionContext context)
    <.ctor>b__0(TestExecutionContext context)
    <>c__DisplayClass1_0.<Execute>b__0()
    BeforeAndAfterTestCommand.RunTestMethodInThreadAbortSafeZone(TestExecutionContext context, Action action)

Code:

Dictionary<string, string> moonOptions = new Dictionary<string, string>();
moonOptions.Add("Http","--ignore-certificate-errors");
BrowserTypeConnectOptions launchOptions = new BrowserTypeConnectOptions() {
    Headers = moonOptions
};
var playwright = await Playwright.CreateAsync();
await playwright.Chromium.ConnectAsync("wss://moon.url.com/wd/hub", launchOptions);
2

There are 2 best solutions below

1
On

I hit the same issue recently.

  1. The error you are getting means that the certificates presented by the moon server to your client is incomplete. Try to provide the full chain of certs there!
  2. If later, you get the "WebSocket error: self signed certificate in certificate chain" then you need to set the NODE_EXTRA_CA_CERTS env variable pointing at your pem file. This is happening being Node cannot read the trusted CAs by default.

This is the question/solution I posted for the Playwright team along with the resolution.

0
On

If we set this parameter IgnoreHTTPSErrors , then the server will ignore doing the SSL validation. Search here

You can set this in code and it will avoid the check

 public override BrowserNewContextOptions ContextOptions()
 {  
            BrowserNewContextOptions()
            {
                IgnoreHTTPSErrors = true
            };
 }

Once this is yet, the error related to "unable to verify the first certificate" will not come.

for API

    APIRequestNewContextOptions aPIRequestNewContextOptions = new(
                new()
                  {
                    IgnoreHTTPSErrors = true
                  }
             );

Here is the documentation from playwright