nightwatchjs / nightwatch

Integrated end-to-end testing framework written in Node.js and using W3C Webdriver API. Developed at @browserstack
https://nightwatchjs.org
MIT License
11.85k stars 1.35k forks source link

beforeEach does not run if test doesn't contain any lifecycle hooks #3847

Open kumarchandresh opened 1 year ago

kumarchandresh commented 1 year ago

Description of the bug/issue

When I write a command in beforeEach hook in globals.js and don't write any before or after hooks in the test suite, I expected that command to be executed but nightwatch skips it instead.

Steps to reproduce

  1. Create new nightwatch project using npm nightwatch init command.
  2. Create and configure globals.js file.

    
    // globals.js
    module.exports = {
    
    beforeEach(browser, done) {
        browser.window.maximize();
        done();
    }

}

// nightwatch.conf.js module.exports = {

// ...

globals_path: 'globals.js',

// ...

}

3. Run the `duckDuckGo.js` test from examples using `npx nightwatch nightwatch/examples/basic/duckDuckGo.js`.
   - **Note that the window _does't maximize_ for this test.**
```js
// duckDuckGo.js
describe('duckduckgo example', function() {
  it('Search Nightwatch.js and check results', function(browser) {
    browser
      .navigateTo('https://duckduckgo.com')
      .waitForElementVisible('input[name=q]')
      .sendKeys('input[name=q]', ['Nightwatch.js'])
      .click('*[type="submit"]')
      .assert.visible('.results--main')
      .assert.textContains('.results--main', 'Nightwatch.js');
  }); 
});
  1. Run the ecosia.js test from examples using npx nightwatch nightwatch/examples/basic/ecosia.js.

    • Note that the window does maximize for this test
      
      // ecosia.js
      describe('Ecosia.org Demo', function() {
      before(browser => browser.navigateTo('https://www.ecosia.org/'));

    it('Demo test ecosia.org', function(browser) { browser .waitForElementVisible('body') .assert.titleContains('Ecosia') .assert.visible('input[type=search]') .setValue('input[type=search]', 'nightwatch') .assert.visible('button[type=submit]') .click('button[type=submit]') .assert.textContains('.layout__content', 'Nightwatch.js'); });

    after(browser => browser.end()); });

Sample test

No response

Command to run

No response

Verbose Output

No response

Nightwatch Configuration

No response

Nightwatch.js Version

3.1.1

Node Version

18.17.0

Browser

Chrome 115.0.0

Operating System

Windows 11 Pro version 10.0.22621

Additional Information

No response

emouawad commented 1 year ago

+1

garg3133 commented 9 months ago

A workaround for this is to define beforeEach function in globals.js to be async.

The following would work fine even without a before hook in the actual test:

// globals.js
module.exports = {

    async beforeEach(browser, done) {
        browser.window.maximize();
        done();
    }

}

// nightwatch.conf.js
module.exports = {

    // ...

    globals_path: 'globals.js',

    // ...

}

So, the actual issue here is that the beforeEach global hook doesn't function when not defined as a async function, unless the test suite itself contains a before hook.

bkhanale commented 8 months ago

This is not an issue when using mocha as the test runner.