How can I export async await functions to use like a utils file?

Viewed 524

I have a utils file where I have async await functions.

I am exporting these functions like this:

module.exports.pressing = pressing;
module.exports.login = login;
module.exports.logout = logout;
module.exports.getImage = getImage;
module.exports.decodeUnicodeCharacters = decodeUnicodeCharacters;
module.exports.initApp = initApp;

What I would like to achieve is that through the protractor configuration file I can call the functions of the utils file like this:

browser.params.utils.pressing(protractor.Key.DOWN);

What is the best way to do it?

1 Answers
  1. Create a module and require anywhere you want to use it

utils.js

module.exports = {
  pressing: function () {
    // what it does
  },
  login: function () {
    // what it does
  },
  logout: function () {
    // what it does
  },
  getImage: function () {
    // what it does
  },
  decodeUnicodeCharacters: function () {
    // what it does
  }, 
  initApp: function () {
    // what it does
  }, 
};

then anywhere in project

let utils = require('PATH/TO/utils.js')

await utils.login();
  1. Make it global

config

// ...
onPrepare() {
  global.utils = {
    login: function () {
      // what it does
    },
  }
}
// ...

anywhere in project

await utils.login() // no need to require
Related