Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue connecting to https://playwright.microsoft.com/ web sockets #397

Open
sylvesteraswin opened this issue Dec 14, 2023 · 2 comments
Open

Comments

@sylvesteraswin
Copy link

sylvesteraswin commented Dec 14, 2023

Hello,

I am trying to use https://playwright.microsoft.com/ for a project. The intension is to connect to cloud browser instance using a web socket url. I am unable to use this feature due to the below error message

Error message

WebSocket error: wss://browser.playwright.microsoft.com/ws 400 Bad Request
The browser that was requested, 'undefined', is not supported. Supported values are 'chromium', 'firefox', and 'webkit'.

The region and service id for this test run are : westeurope, 19da2b05-137f-4303-9b5f-db0cd22a80f0


=========================== logs ===========================
<ws connecting> wss://westeurope.api.playwright.microsoft.com/api/authorize/connectSession
<ws error> wss://westeurope.api.playwright.microsoft.com/api/authorize/connectSession error WebSocket was closed before the connection was established
<ws connect error> wss://westeurope.api.playwright.microsoft.com/api/authorize/connectSession WebSocket was closed before the connection was established
<ws disconnected> wss://westeurope.api.playwright.microsoft.com/api/authorize/connectSession code=1006 reason=
<ws connecting> wss://browser.playwright.microsoft.com/ws
<ws unexpected response> wss://browser.playwright.microsoft.com/ws 400 Bad Request
The browser that was requested, 'undefined', is not supported. Supported values are 'chromium', 'firefox', and 'webkit'.

The region and service id for this test run are : westeurope, 19da2b05-137f-4303-9b5f-db0cd22a80f0


<ws error> wss://browser.playwright.microsoft.com/ws error WebSocket was closed before the connection was established
<ws connect error> wss://browser.playwright.microsoft.com/ws WebSocket was closed before the connection was established
<ws disconnected> wss://browser.playwright.microsoft.com/ws code=1006 reason=
============================================================

Sample code

pw, err := playwright.Run()
if err != nil {
    return "", err
}
defer pw.Stop()


// // Start a browser server
browser, err := pw.Chromium.Connect(browserInstanceUrl, browserInstanceOptions)
if err != nil {
    return "", err
}
defer browser.Close()
@canstand
Copy link
Collaborator

I don't have a service instance to test with. But in the sample code, the authentication header is not set before connecting. Could this be the reason?

@musticode
Copy link

musticode commented Jun 14, 2024

I have the same problem in my case, I tried to convert this code;
found in documentation : https://github.com/microsoft/playwright-testing-service/blob/main/samples/get-started/playwright.service.config.ts

  use: {
    // Specify the service endpoint.
    connectOptions: {
      wsEndpoint: `${process.env.PLAYWRIGHT_SERVICE_URL}?cap=${JSON.stringify({
        // Can be 'linux' or 'windows'.
        os,
        runId: process.env.PLAYWRIGHT_SERVICE_RUN_ID
      })}`,
      timeout: 30000,
      headers: {
        'x-mpt-access-key': process.env.PLAYWRIGHT_SERVICE_ACCESS_TOKEN!
      },
      // Allow service to access the localhost.
      exposeNetwork: '<loopback>'
    }
  }

I am writing test in java, so this code is worked for me

  String os = "windows"; // Set OS here, can be 'linux' or 'windows'
  String runId = System.getenv("PLAYWRIGHT_SERVICE_RUN_ID");

  String wsEndpoint = wssEndpoint + "?cap=" + String.format("{\"os\":\"%s\",\"runId\":\"%s\"}", os, runId);

      BrowserType.ConnectOptions connectOptions = new BrowserType.ConnectOptions()
              .setHeaders(Map.of(
                      "x-mpt-access-key", apiKey
              ))
              .setTimeout(30000)
              .setExposeNetwork("<loopback>");

      Browser browser = playwright.chromium().connect(wsEndpoint, connectOptions);
  • wssEndpoint : Generated by playwright service in azure. Can be like : "wss://eastus.api.playwright.microsoft.com/api/authorize/connectSession
  • apiKey : your api key in generated in playwright service

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants