node.jsautomated-testsplaywrightplaywright-test

How do I roll to a specific browser version with Playwright?


I need to run some tests using Playwright among different Chromium versions. I have different Chromium folders with different versions, but I don't know how to switch from a version to another using the CLI to run my tests. Some help? Thanks :)


Solution

  • Max Schmitt is right: the library is not guaranteed to work with non-bundled Chromiums. Anyway, you can give it a try to multiple Chromium-based browsers in the executablePath. As it is not builtin in the Playwright Test you will need to implement it yourself.

    Note: like this you lose some of the simplicity of Playwright Test.

    In my example I used Jest as a test runner so yarn add --dev jest is required. The last CLI argument - reserved for the browser version - can be retrieved with process.argv.slice(-1)[0] within Node, like this you can tell your tests what browser version you want to use. Here they will be edge, chrome and the default is the bundled chromium.

    MS Edge (Chromium)

    yarn test chrome.test.js edge
    

    Chrome

    yarn test chrome.test.js chrome
    

    Chromium (default - bundled with Playwright) (but any string, or the lack of this argument will also launch this as the default)

    yarn test chrome.test.js chromium_default
    

    chrome.test.js
    (with Windows-specific executable paths)

    const playwright = require('playwright')
    
    let browser
    let page
    
    beforeAll(async function () {
      let chromeExecutablePath
      switch (process.argv.slice(-1)[0]) {
        case 'chrome':
          chromeExecutablePath = 'C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe'
          break
        case 'edge':
          chromeExecutablePath = 'C:\\Program Files (x86)\\Microsoft\\Edge\\Application\\msedge.exe'
          break
        default:
          chromeExecutablePath = ''
      }
      browser = await playwright.chromium.launch({ 
        headless: false, 
        executablePath: chromeExecutablePath 
      })
      page = await browser.newPage()
    })
    
    describe('Google Search', function () {
      test('should respond with HTTP 200 - OK', async function () {
        const response = await page.goto('https://google.com')
        const responseCode = response.status()
        expect(responseCode).toBe(200)
      })
      afterAll(async function () {
        await browser.close()
      })
    })