【问题标题】:How to compare strings between each other in loop in NightWatch如何在 NightWatch 中循环比较字符串
【发布时间】:2021-06-24 17:53:15
【问题描述】:

我正在使用 NightWatch JS,我有一个问题,我如何比较字符串并检查 NightwatchJS 循环中的唯一性?

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 个字符串) 有一个问题,我现在如何检查,如何比较每个字符串在循环中是唯一的?

【问题讨论】:

    标签: javascript xpath automation nightwatch.js


    【解决方案1】:

    您可以使用 ma​​p 来检查字符串是否唯一。如果地图中已经存在类似的字符串,则永远不会再次添加该字符串。所以在你的情况下,如果所有三个文本都是唯一的,那么地图的大小应该是 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
    • 0 如何将 GitLab.com 与 Jira Cloud Map 集成 (1) { 0 => '如何将 GitLab.com 与 Jira Cloud 集成' } 我们正在开源 Protocol Fuzzer 社区版! Map(2) { 0 => '如何将 GitLab.com 与 Jira Cloud 集成', 1 => "我们正在开源 Protocol Fuzzer 社区版!" } 你如何为 GitLab 的开放 DevOps 平台做出贡献 Map(3) { 0 => '如何将 GitLab.com 与 Jira Cloud 集成',1 => “我们正在开源 Protocol Fuzzer 社区版!”,2 => “如何你为 GitLab 的开放 DevOps 平台做出了贡献”
    猜你喜欢
    • 2017-08-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多