How to make Nightwatch use xpath by default in page object files

You can just use a javascript function like this (depending on your favourite way to create objects):

var xSelector = function (selector) {
    return {
        selector: selector,
        locateStrategy: 'xpath'
    }
};

And then use it like so:

elements: {

    xxx: xSelector('//a[text()="Bank Details"]')
    createStepButton: { selector: '#menu-create-item' },
}

Hint: In the sample above the createStepButton is still using the css selector strategy. Consider also creating a cssSelector function for uniform readability of the elements section.


Try to set: "use_xpath": true

in nightwatch.conf.js file, not nightwatch.json

Worked for me :)