0

The url shows a WebdriverIO test runner configuration

https://webdriver.io/docs/configurationfile.html

It has got many hooks. Consider the hook onComplete I want to write a function, may be a function to create a file. In another file and call that function inside the onComplete hook. Could you please help me to achieve this.

wanderors
  • 2,030
  • 5
  • 21
  • 35

2 Answers2

0

Yes, you pretty much described the flow.

Define your function in a file and export it:

module.exports = (() => {
  /** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
    * > Def: Polls the DOM until the given 'desiredState' is found. 
    * @param    {string} desiredState ['loading', 'interactive', 'complete']
    * @returns  {Promise} WebdriverIO Promise
    * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  browser.addCommand('waitForReadyState', (desiredState) => {
    let foundState;

    browser.waitUntil(() => {
      foundState = browser.execute('return document.readyState;');
      console.log(`\n> Waiting for page to load ... | Current state: '${foundState}'`);
      return foundState === desiredState;
    }, browser.options.waitforTimeout, `Timeout before expected state! Found: '${foundState}' | Expected: '${desiredState}'`);
  });
})();

Then, import it in the desired hook (e.g: for a custom_command, the before hook):

before: function (capabilities, specs) {
  require('./test/custom_commands/waitForReadyState');
}

You can easily reproduce the model to implement logging & file manipulation functions that you need to run in the onComplete hook.

iamdanchiv
  • 4,052
  • 4
  • 37
  • 42
  • Could you please explain more about ` require('./test/custom_commands/waitForReadyState');` . Consider if the function waitForReadyState is written on a file utils.js. Then how can i call it ? – wanderors Aug 05 '19 at 08:04
0

its maybe late but here is how you can do it :

/** file to keep your function should be in es5 or you have to add babel to covert it to es6 before WebdriverIO start **/

test.js

module.exports = function foo(){
   console.log('here');
}

in config file //before export.config:

const foo = require('path-to-test.js');

use foo() in onComplete() hook