I would like to override the click functionality that nightwatch provides with a different method. I have tried using the custom scripts with a click.js but that tells me it is already being used.
If there is a mechanism for doing this already it may make sense to document - the problem I am running into described below would allow a workaround for various browsers running into issues with commands.
The why: The reason I am pursuing this is in firefox 3.5+ (node-firefox or node-firefox-debug) the click function does not work. In the short term my fix would be to invoke the click action by using document.selector ( which does work). I have the method added to my custom scripts to click in this way but I would rather not find and replace all .click() methods with my own. If I can override the click I can keep using the latest firefox for testing and then return to use the default .click() from nightwatch when firefox/selenium fixes the issue.
think I answered my own question. For the scenario above I was able to override the default .click by doing this.
globals.js
beforeEach: (browser, cb) => {
browser
.perform(() => {
const currentBrowser = browser.options.desiredCapabilities.browserName;
if (currentBrowser === 'firefox') { // reassign the click to be clickByExecute for the time being.
console.log('reassigning click to clickByExecute');
browser.click = browser.clickByExecute;
}
})
browser.perform(() => { cb(); } );
}
Now when .click is used in the test when running firefox I will be running my clickByExecute override which is this. (I have a few extra library commands in there like logger and getLocatorStrategy but you can get the idea).
custom_command - clickByExecute.js
/**
* Will click an element using direct execute method
* @param {string} selector selector
* @param {function} callback - option callback
* @returns {*} nightwatch browser object
*/
function clickByExecute(selector, callback = null) {
let selectorType = '';
return this
.perform(() => { this.getLocatorStrategy((strategy) => { selectorType = (strategy === 'xpath') ? 'xpath' : 'css'; }); })
.logger('attempting to click by execute')
.perform(() => {
if (selectorType === 'css') {
this.execute(function click(sel) { document.querySelector(sel).click(); }, [selector]);
} else {
this.execute(function click(sel) { document.evaluate(sel, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue.click(); }, [selector]);
}
})
.pause(1000)
.perform(() => { if (typeof callback === 'function') { callback(); } });
}
exports.command = clickByExecute;
I think this is worthwhile to make note of and document - there have been several times where an issue has arisen with a selenium driver where the default action just ceased to work. Firefox just fails to click in the selenium docker node-firefox 3.5-3.6 so I keep having to go back to a previous 3.4 build to make it work. With this I can get by that annoyance and keep using the latest firefox to test against... the intent being I remove that click override once the selenium firefox issues have been addressed. I don't have to go and change any existing tests to accommodate the new click method either - I didn't want to bulk change .click calls to my override .clickByExecute in all my tests... and I also didn't want the click overridden for chrome or other browsers.
Hope this helps somebody else - will leave this up as I think it is worthwhile to document how to do this. I can make README.md if you want.
cool idea @Zechtitus
This issue has been automatically marked as stale because it has not had any recent activity.
If possible, please retry using the latest Nightwatch version and update the issue with any relevant details. If no further activity occurs, it will be closed. Thank you for your contribution.
@Zechtitus , thanks for the solution but can you provide a guide on how to use this function? I'm quite new to this. Thanks
Most helpful comment
think I answered my own question. For the scenario above I was able to override the default .click by doing this.
globals.js
Now when .click is used in the test when running firefox I will be running my clickByExecute override which is this. (I have a few extra library commands in there like logger and getLocatorStrategy but you can get the idea).
custom_command - clickByExecute.js
I think this is worthwhile to make note of and document - there have been several times where an issue has arisen with a selenium driver where the default action just ceased to work. Firefox just fails to click in the selenium docker node-firefox 3.5-3.6 so I keep having to go back to a previous 3.4 build to make it work. With this I can get by that annoyance and keep using the latest firefox to test against... the intent being I remove that click override once the selenium firefox issues have been addressed. I don't have to go and change any existing tests to accommodate the new click method either - I didn't want to bulk change .click calls to my override .clickByExecute in all my tests... and I also didn't want the click overridden for chrome or other browsers.
Hope this helps somebody else - will leave this up as I think it is worthwhile to document how to do this. I can make README.md if you want.