Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

beforeEach and AfterEach for every test case using Globals.js

So, I have a globals.js file where I have mentioned beforeEach and afterEach, but what I could understand from this link Nightwatch Globals, the beforeEach and afterEach are called once before and after a Test Suite (A single Js file). But in my framework I have multiple test cases in the a single js file (or Test Suite) and I want to call beforeEach and afterEach before and after every test case. Anyway to achieve that? Below is my globals.js file:

module.exports = {
    asyncHookTimeout: 40000,

    beforeEach: function (browser, done) {
        // browser.maximizeWindow();
        // browser.deleteCookies();
        browser.perform(function () {
            console.log('Inside BeforeEach');
            done();
        });
    }

    afterEach: function (browser, done) {
        browser.end(function () {
            console.log("Inside After Each");
            done();
        });
    },
};
like image 614
Alapan Das Avatar asked Dec 14 '18 15:12

Alapan Das


People also ask

What is beforeEach and afterEach?

beforeEach() is run before each test in a describe. afterEach() is run after each test in a describe.28-Jan-2014.

When to use beforeEach in Jest?

beforeEach(fn) # This is often useful if you want to reset some global state that will be used by many tests. Here the beforeEach ensures that the database is reset for each test. If beforeEach is inside a describe block, it runs for each test in the describe block.

What is beforeEach code block in test case?

The beforeEach code block runs before each unit test and re-initializes the props that will be passed to the custom Block to a default, empty state.

Does beforeEach run before each describe?

Just like the name suggests, it runs before each test in your test suite. For example, the beforeEach method will run four times in a suite with four tests. Each execution will happen just before the actual test function.


1 Answers

Of course there is! Just make use of the notorious Nightwatch test hooks.

  • If you want to filter your test-suites, then as you pointed out, we'll use the global test hooks.
  • If you want to filter your test-cases, then we'll use the test hooks

Example (your test-file should look like this):

module.exports = {

  before(browser) {
    // > this will get run only ONCE, before all the tests <
  },
  beforeEach(browser) {
    // > this will get run before every test case <
  }

  tags: ['your', 'tags', 'go', 'here'],
  'Test Case No.1': (browser) => {
     // > this test does something here <
  },
  'Test Case No.2': (browser) => {
     // > this test does something else here <
  },
  'Test Case No.3': (browser) => {
     // > this test does something else here <
  },

  afterEach(browser) {
    // > this will get run after every test case <
  },
  after(browser) {
    // > this will get run ONCE, after all tests have run <
  }
};

Lastly, quoting the DOCs:

The before and after will run before and after the execution of the test suite respectively (in our case, the test-file), while beforeEach and afterEach are ran before and after each test case (test step).


LE: What @AlapanDas wants is to custom-tailor the way the Nightwatch test-runner handles test-level hooks. This is of course doable, but dirty. You have to re-write the hooking logic from the following files:

[email protected]:

  • testcase.js (path: /nightwatch/lib/runner/testcase.js);
  • testsuite.js (path: /nightwatch/lib/runner/testsuite.js);

[email protected]:

  • every {hookName}.js file from the /hooks folder (path: /nightwatch/lib/testsuite/hooks/*.js);

Still, a compromise can be made here! Just try to find the common, recurring steps/instructions from your before, after, etc. hooks and extract that logic inside a /custom_commands file. That would condense your test files, as well as decoupling the login from your hooks. On the long run, this will also grant the advantage of a single-point-of-change when maintaining the hooks.

like image 86
iamdanchiv Avatar answered Sep 21 '22 05:09

iamdanchiv