CodeceptJS icon indicating copy to clipboard operation
CodeceptJS copied to clipboard

Playwright: Method _startBrowser() doesn't start browser window since CodeceptJS 3.1.0

Open mirao opened this issue 3 years ago • 2 comments

It's a regression since 3.1.0 (also 3.3.0 is affected). It works well with 3.0.7 I use similar scenario - I need to change timezone in browser and the actions "stop/start" browser are needed for it. You can find a simple test below.

What are you trying to achieve?

The test passes, a page is open

What do you get instead?

startBrowser() doesn't start a browser window, therefore I.amOnPage("/"); fails

$ codeceptjs run --verbose
context
CodeceptJS v3.3.0
Using test root "/home/mirao/workspace/codeceptjs/tests/test"
Helpers: Playwright, Common
Plugins: screenshotOnFail, retryFailedStep, tryTo

Test --
    [1]  Starting recording promises
    Timeouts: 
 › [Session] Starting singleton browser session
  Stop/start
    I stop browser 
    I start browser 
    I am on page "/"
    [1] Error | TypeError: Cannot read property 'goto' of null
    [1] Error | TypeError: Cannot read property 'goto' of null
    [1] <teardown> Stopping recording promises
 › <screenshotOnFail> Test failed, try to save a screenshot
 › Screenshot is saving to /home/mirao/workspace/codeceptjs/tests/test/output/Stop_start.failed.png
 › <TypeError: Cannot read property 'screenshot' of null> 
  ✖ FAILED in 424ms

    [2]  Starting recording promises

-- FAILURES:

  1) Test
       Stop/start:
     Cannot read property 'goto' of null
      at Playwright.amOnPage (/home/mirao/workspace/codeceptjs/node_modules/codeceptjs/lib/helper/Playwright.js:780:21)
      at Step.run (/home/mirao/workspace/codeceptjs/node_modules/codeceptjs/lib/step.js:122:47)
      at /home/mirao/workspace/codeceptjs/node_modules/codeceptjs/lib/actor.js:134:23
  
  Scenario Steps:
  - I.amOnPage("/") at Test.<anonymous> (./Test_test.js:6:7)
  - I.startBrowser() at Test.<anonymous> (./Test_test.js:5:7)
  - I.stopBrowser() at Test.<anonymous> (./Test_test.js:4:7)

common_helper.js:

const Helper = require("@codeceptjs/helper");

class Common extends Helper {

    async stopBrowser() {
        return await this.helpers.Playwright._stopBrowser();
    }

    async startBrowser() {
        return await this.helpers.Playwright._startBrowser();
    }
}

module.exports = Common;

Test_test.js:

Feature("Test");

Scenario("Stop/start", ({ I }) => {
    I.stopBrowser();
    I.startBrowser();
    I.amOnPage("/");
});

Details

  • CodeceptJS version: 3.3.0
  • NodeJS Version: v14.19.0
  • Operating System: Ubuntu 21.10
  • Playwright 1.16.3
  • Configuration file:
exports.config = {
    tests: "./*_test.js",
    output: "./output",
    helpers: {
        Playwright: {
            url: "https://codecept.io",
            show: true,
            browser: "chromium"
        },
        Common: {
            require: "./common_helper.js",
        },
    },
    include: {
        I: "./steps_file.js"
    },
    bootstrap: null,
    mocha: {},
    name: "test",
    plugins: {
        pauseOnFail: {},
        retryFailedStep: {
            enabled: true
        },
        tryTo: {
            enabled: true
        },
        screenshotOnFail: {
            enabled: true
        }
    }
};

mirao avatar Feb 10 '22 18:02 mirao

A difference between versions 3.0.7 and >= 3.1.x is that in versions >= 3.1.x, the Playwright helper method _startBrowser() doesn't create a new context and a new page. If I call those actions manually, a new browser window appears.

mirao avatar Feb 11 '22 14:02 mirao

I fixed it to myself by a new class method in Playwright helper. Not sure it's a correct solution, but it works for me.

async _startContextPage(config) {
  this.browserContext = await this.browser.newContext(config);
  const page = await this.browserContext.newPage();
  targetCreatedHandler.call(this, page);
  await this._setPage(page);
}

and the method startBrowser() in my custom helper can look e.g. this way:

async startBrowser(timezone: string) {
    const playwright = this.helpers.Playwright;
    await playwright._startBrowser();
    // Start a new browser session in a specific timezone and a new page
    await playwright._startContextPage({ timezoneId: timezone });
}

mirao avatar Feb 11 '22 21:02 mirao