我所有的页面对象看起来像这样:
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']"
}
如果我能说“在任何地方使用 xpath”,那就更好了——这一切都会崩溃
The docs说你应该可以设置
"use_xpath" : true
在您的“测试设置”中,但我在 nightwatch.json 中可以看到的所有地方都尝试过,但没有任何效果。(在任何情况下,它们是否意味着此设置会影响页面目标文件中的声明尚不完全清楚:该示例仅显示它会影响测试用例中的后续断言调用)。
最佳答案
您可以使用这样的 javascript 函数(取决于您最喜欢的创建对象的方式):
var xSelector = function (selector) {
return {
selector: selector,
locateStrategy: 'xpath'
}
};
然后像这样使用它:
elements: {
xxx: xSelector('//a[text()="Bank Details"]')
createStepButton: { selector: '#menu-create-item' },
}
提示:在上面的示例中,createStepButton 仍然使用 css 选择器策略。还考虑创建一个 cssSelector 函数,以实现元素部分的统一可读性。
关于xpath - 如何让 Nightwatch 在页面对象文件中默认使用 xpath,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36147905/