microsoft / playwright-testing-service

MIT License
70 stars 10 forks source link

[Question]:[How to use service with Manual browserType.connect] #70

Closed puagarwa closed 7 months ago

puagarwa commented 7 months ago

Describe the bug Example of how to use MPT service when manually launching browser in test suite

If test suite has anywhere browserType.launch() anywhere in code, to migrate to service, it need to change to browserType.Launch()

Here is sample code to migrate

// Name the test run if it's not named yet.
process.env.PLAYWRIGHT_SERVICE_RUN_ID = process.env.PLAYWRIGHT_SERVICE_RUN_ID || new Date().toISOString();
function connectOptions() : [string, ConnectOptions]{
  // return string and options
  return [
    `${process.env.PLAYWRIGHT_SERVICE_URL}?cap=${JSON.stringify({
      os: process.env.PLAYWRIGHT_SERVICE_OS || 'linux', // 'linux' or 'windows
      runId: process.env.PLAYWRIGHT_SERVICE_RUN_ID
    })}`,
    {
      headers: {
      'x-mpt-access-key': process.env.PLAYWRIGHT_SERVICE_ACCESS_TOKEN!
      },
      timeout: 120000,
      // Allow service to access the localhost, comment if testing public endpoint
      exposeNetwork: '<loopback>'
    }
  ];
}

Caller

const browser = await chromium.connect(...connectOptions());

You can check logs DEBUG=pw:browser* to confirm if test is using service browser.

AB#1913569

puagarwa commented 7 months ago

closing it.