codeceptjs / CodeceptJS

Supercharged End 2 End Testing Framework for NodeJS
http://codecept.io
MIT License
4.11k stars 724 forks source link

Order of hooks execution when browser restart is set to true #3857

Closed myrepojuly1987 closed 1 year ago

myrepojuly1987 commented 1 year ago

What are you trying to achieve?

Hello, I have a situation in my project, where I need to perform couple of actions using I object in the browser window in the after.test.event. Example: Delete a data from UI once the test execution is complete.

I am using promise-based approach.

What do you get instead?

There appears to be a race condition occurring when the event.test.after and the After event, in which the browser is closed, does not wait for the steps to complete under event.test.passed.

Provide console output if related. Use --verbose mode for more details.

# paste output here
Test something --
    [1]  Starting recording promises
    Timeouts:
  test scenario @delete
    I am on page "https://www.google.com/"
Test Passed
  √ OK in 3498ms

    I grab current url
Test Complete
    » [Url] https://www.google.com/
Url is https://www.google.com/
    I fill field {"id":"APjFqb"}, "testttt"
    [1] Error | TypeError: elemResponse.map is not a function
    [1] Error | TypeError: elemResponse.map is not a function
  × "after all" hook: codeceptjs.afterSuite for "test scenario @delete" in 18ms
    [1] Error | TypeError: elemResponse.map is not a function
TypeError: elemResponse.map is not a function

-- FAILURES:

  1) Test something
       "after all" hook: codeceptjs.afterSuite for "test scenario @delete":
     elemResponse.map is not a function
      at Browser.getElements (node_modules\webdriverio\build\utils\getElementObject.js:53:35)
      at Browser.$$ (node_modules\webdriverio\build\commands\browser\$$.js:7:59)
      at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
      at async Browser.wrapCommandFn (node_modules\@wdio\utils\build\shim.js:63:29)
      at async WebDriver._locate (node_modules\codeceptjs\lib\helper\WebDriver.js:801:19)
      at async WebDriver.fillField (node_modules\codeceptjs\lib\helper\WebDriver.js:1024:17)

Provide test source code if related

For any test scenario, set `restart: true` for the helper. Just have these hooks executed

Feature('Test something');

Scenario('test scenario', async ({ I }) => {
   await I.amOnPage('https://www.google.com/');
});

********************Event Dispatcher*************
 event.dispatcher.on(event.test.passed, async function () {
    const {I} = inject();
    console.log("Test Passed");
    console.log(`Url is ${await I.grabCurrentUrl()}`);
    await I.fillField({id: 'APjFqb'}, 'testttt');
  });

  event.dispatcher.on(event.test.after, async function () {
   console.log("Test Complete");
  });

Details

# paste config here
myrepojuly1987 commented 1 year ago

Closing this. Was able to address this by overriding the value of restart at run time for specific tests using helpers