如何在NightwatchJS中编写简单的if / else?如果元素存在,请单击此元素,否则 - 单击另一个元素。
'Test2: click login button': function(client) {
client.expect.element('.login .btn').to.be.present;
client.click('.login .btn');
// if "client.expect.element('.login .btn') is not present(visible)" -
// but "client.expect.element('a.menu-link').to.be.present(visible)";
// client.click('a.menu-link');
client.pause(1000);
});
},
答案 0 :(得分:1)
可能有多种方法可以执行此操作,但我会使用Webdriver Protocol的.element()
功能:
'Test2: click login button': client => {
const buttonSelector = '.login .btn';
const menuLinkSelector = 'a.menu-link';
client.element('css selector', buttonSelector, result => {
if (result.status > -1) {
// `.login .btn` is present
client.click(buttonSelector);
} else {
// else, check for `a.menu-link`
client.element('css selector', menuLinkSelector, () => {
client.click(menuLinkSelector);
});
}
});
client.end();
}