1

I new to protractor, and trying to implement hooks. But the hooks are not getting executed. Following is my code: Hook.js:

const {
 Before
} = require('cucumber');
const {AfterAll} = require('cucumber');
module.exports = function () {
 
 Before(function () {
 browser.manage().window().maximize();
 /// await browser.manage().deleteAllCookies();
 
 });
 AfterAll(async function () {
 return driver.quit();
 });
}

I have also added the path in config.js: enter image description here

Do I need to give its path in spec.js as well?

asked Jun 22, 2020 at 1:21
3
  • Compare what you've written to github.com/cucumber/cucumber-js/blob/master/docs/support_files/…. Just requiring your hook file doesn't do anything, because the function isn't executed. Also you can't have two require keys in one object, the value is an array so you can have multiple entries there. Commented Jun 22, 2020 at 6:52
  • The issue is now resolved. After removing module.exports = function() from hooks.js,it worked fine. Commented Jun 22, 2020 at 7:34
  • 1
    I'd suggest deleting this then Commented Jun 22, 2020 at 7:44

1 Answer 1

1

Try to understand what the code does:

const {
 Before
} = require('cucumber');
const {AfterAll} = require('cucumber');
module.exports = function () {
 
 Before(function () {
 browser.manage().window().maximize();
 /// await browser.manage().deleteAllCookies();
 
 });
 AfterAll(async function () {
 return driver.quit();
 });
}

Here you creates a function but never calls the function.

you can make it work in three ways:

First Approach: Call the function and pass the object to module.exports

const {
 Before
} = require('cucumber');
const {AfterAll} = require('cucumber');
//store function to a variable
let hooks = function () {
 
 Before(function () {
 browser.manage().window().maximize();
 /// await browser.manage().deleteAllCookies();
 
 });
 AfterAll(async function () {
 return driver.quit();
 });
}
//call the function and pass it to module.exports
module.exports = hooks();

Second Approach: Create self-invoking function

const {
 Before
} = require('cucumber');
const {AfterAll} = require('cucumber');
module.exports = (function () {
 
 Before(function () {
 browser.manage().window().maximize();
 /// await browser.manage().deleteAllCookies();
 
 });
 AfterAll(async function () {
 return driver.quit();
 });
})();

Third Approach: create an object using object literal instead of creating a function and calling it ( This what you have done as the fix )

const {
 Before
} = require('cucumber');
const {AfterAll} = require('cucumber');
module.exports = {
 
 Before(function () {
 browser.manage().window().maximize();
 /// await browser.manage().deleteAllCookies();
 
 });
 AfterAll(async function () {
 return driver.quit();
 });
};
answered Jun 22, 2020 at 16:56

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.