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.