Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/383.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在NightWatch中比较循环中的字符串_Javascript_Xpath_Automation_Nightwatch.js - Fatal编程技术网

Javascript 如何在NightWatch中比较循环中的字符串

Javascript 如何在NightWatch中比较循环中的字符串,javascript,xpath,automation,nightwatch.js,Javascript,Xpath,Automation,Nightwatch.js,我正在使用NightWatch JS,我有一个问题,我如何在NightWatch JS中比较字符串并检查循环中的唯一性 My code module.exports = { test: function(client) { var text1; client .maximizeWindow() .url('https://gitlab.com/') .waitFo

我正在使用NightWatch JS,我有一个问题,我如何在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);
    }
};

有些东西工作不正常。控制台中接收到错误×失败[equal]:(0==3)-应为“3”,但得到:“0”我已更新我的答案。您可以再试一次吗?仍然出现相同的错误您可以执行
console.log(uniqueStrings.size)
而不是
client.assert.equal(uniqueStrings.size,3)
并检查打印的内容吗?执行后打印“0”,但在检查元素之前打印0