如何让 Nightwatch 在页面对象文件中默认使用 xpath

Gre*_*ade 6 xpath pageobjects nightwatch.js

我所有的页面对象看起来像这样:

  elements: {
      header: {
         locateStrategy: 'xpath',
         selector: "//h3[text()='Welcome']"
      },
      loginButton: {
         locateStrategy: 'xpath',
         selector: "//button[text()='Login']"
      },
      forgotPasswordLink: {
         locateStrategy: 'xpath',
         selector: "//a[text()='Forgot Password?']"
      },
      signupButton: {
         locateStrategy: 'xpath',
         selector: "//button[text()='Signup']"
      }
Run Code Online (Sandbox Code Playgroud)

如果我能说“在任何地方使用 xpath”,那就更好了——这一切都会崩溃

文档说你应该能够"use_xpath" : true在你的“测试设置”中进行设置,但我已经在 nightwatch.json 中可以看到的所有地方都试过了,但它没有任何效果。

(在任何情况下,它们是否意味着此设置会影响页面目标文件中的声明尚不完全清楚:该示例仅显示它会影响测试用例中的后续断言调用)。

pro*_*mba 4

您可以使用这样的 JavaScript 函数(取决于您最喜欢的创建对象的方式):

var xSelector = function (selector) {
    return {
        selector: selector,
        locateStrategy: 'xpath'
    }
};
Run Code Online (Sandbox Code Playgroud)

然后像这样使用它:

elements: {

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

提示:在上面的示例中,createStepButton 仍然使用 css 选择器策略。还可以考虑创建一个 cssSelector 函数,以实现元素部分的统一可读性。