getgauge / taiko

A node.js library for testing modern web applications
https://taiko.dev
MIT License
3.56k stars 452 forks source link

`openBrowser` with `observe` set, results in "Browser or page not initialized" #2712

Open fbuetler opened 9 months ago

fbuetler commented 9 months ago

Describe the bug

For exported functions interacting with the browser, there are some hooks in place that check each time before running these functions, if the observe mode is set, and if it is, to wait before actually running the function:

const realFuncs = {};
for (const func in module.exports) {
  realFuncs[func] = module.exports[func];
  if (realFuncs[func].constructor.name === 'AsyncFunction') {
    module.exports[func] = async function () {
      if (defaultConfig.observe) {
        await waitFor(defaultConfig.observeTime);
      }
      return await realFuncs[func].apply(this, arguments);
    };
  }
}

https://github.com/getgauge/taiko/blob/v1.3.10/lib/taiko.js#L2616-L2618

The first thing waitFor does, is to call validate (https://github.com/getgauge/taiko/blob/v1.3.10/lib/taiko.js#L2340) and the first thing validate does, is to check if there is a browser instance available (https://github.com/getgauge/taiko/blob/v1.3.10/lib/connection.js#L175-L177).

Unfortunately, this hook is also executed for the openBrowser function. As the purpose of this function is to open a browser, the aforementioned check will inherently fail, as there is no browser instance available

To Reproduce Steps (or script) to reproduce the behavior:

  1. Set observe to true
  2. Call openBrowser()
  const { openBrowser, setConfig } = require('taiko');
  await setConfig({
    observe: true,
  });
  await openBrowser()

Logs

    Error Message: Error: Browser or page not initialized. Call `openBrowser()` before using this API
    Stacktrace: 
    Error: Browser or page not initialized. Call `openBrowser()` before using this API
        at validate (node_modules/taiko/lib/connection.js:176:11)
        at waitFor (node_modules/taiko/lib/taiko.js:2340:3)
        at module.exports.<computed> (node_modules/taiko/lib/taiko.js:2617:15)

Expected behavior

Error should not happen.

Versions:

DCoomer commented 9 months ago

You can pass {headless:false} into openBrowser if you need to observe the test

fbuetler commented 9 months ago

Actually, our use case was a bit different. We only wanted to have a specific time interval between each test step. Hence, we opened the browser with headless and observe set. I know observe is originally meant to observe the test with a visible browser. However, I think, openBrowser should not fail in the case described above.

DCoomer commented 9 months ago

The observeTime option for setConfig sets the delay between each test step.

So you would need to open the browser first, then call await setConfig({observeTime: timeInMilliSeconds}) or npx taiko script.js -o -w 1500 (1500 being the observeTime in ms )

I think headless == true sets it to '0'.

Otherwise make a pull request to fix the issue you're presenting. Taiko isn't actively maintained/fixing bugs. But they do accept pull requests for bug fixes from the community

peschee commented 9 months ago

Taiko isn't actively maintained/fixing bugs

That's a bummer :/

DCoomer commented 9 months ago

https://github.com/getgauge/taiko/discussions/2707

Again, they accept Pull Requests. So if you have an issue, you can submit a solution via a pull request. They are usually pretty quick to review and merge them.