我正在使用NightwatchJS,我有一个问题,我如何在NightWatch JS中比较字符串并检查循环中的唯一性?
My code module.exports = {
test: function(client) {
var text1;
client
.maximizeWindow()
.url('https://gitlab.com/')
.waitForElementVisible('svg.nav-logo', 10 * 1000)
.click('#CybotCookiebotDialogBodyLevelButtonAccept')
.useXpath()
.moveToElement('(//h2[contains(text(),"The latest from our blog")]//following-sibling::div/a/div/h4)',1,1)
.waitForElementVisible('(//h2[contains(text(),"The latest from our blog")]//following-sibling::div/a/div/h4)', 10 * 1000)
.elements('xpath','(//h2[contains(text(),"The latest from our blog")]//following-sibling::div/a/div/h4)',
function(elements)
{
elements.value.forEach(function(elementsObj)
{
client.elementIdText(elementsObj.ELEMENT,function(result){
text1 = result.value;
console.log(text1);
})
})
})
.pause(10*1000);
}
};
在控制台中,我收到了预期的结果(3个字符串(有一个问题,我现在如何检查,如何比较每个字符串在循环中是唯一的?
您可以使用映射来检查字符串是否唯一。如果映射中已经存在类似的字符串,则该字符串将不再添加。因此,在你的情况下,如果这三个文本都是唯一的,那么地图的大小应该是3,你可以在测试结束时断言。
let uniqueStrings = new Map()
My code module.exports = {
test: function(client) {
var text1;
client.maximizeWindow()
.url('https://gitlab.com/')
.waitForElementVisible('svg.nav-logo', 10 * 1000)
.click('#CybotCookiebotDialogBodyLevelButtonAccept')
.useXpath()
.moveToElement('(//h2[contains(text(),"The latest from our blog")]//following-sibling::div/a/div/h4)', 1, 1)
.waitForElementVisible('(//h2[contains(text(),"The latest from our blog")]//following-sibling::div/a/div/h4)', 10 * 1000)
.elements('xpath', '(//h2[contains(text(),"The latest from our blog")]//following-sibling::div/a/div/h4)', function(elements) {
elements.value.forEach(function(elementsObj) {
client.elementIdText(elementsObj.ELEMENT, function(result) {
text1 = result.value;
console.log(text1);
uniqueStrings.set(text1.trim())
})
})
client.assert.equal(uniqueStrings.size, 3)
}).pause(10 * 1000);
}
};