【问题标题】:Element could not be located error in nightwatch jsnightwatch js中的元素无法定位错误
【发布时间】:2021-07-04 02:14:57
【问题描述】:

我想确认我的每个字符串都有文本“Operational”。这是我的代码

module.exports = {

test: function (client) {

    var text1;

    client
        .maximizeWindow()
        .url('https://status.gitlab.com/')
        .waitForElementVisible('img[alt="Logo"]', 10 * 1000)
        .elements('css selector', '.component',
            function (elements) {
                elements.value.forEach(function (elementsObj) {
                    client.elementIdText(elementsObj.ELEMENT, function (result) {
                        text1 = result.value
                        text1 = text1.replace(/\s+/g, '')
                        console.log(text1);
                        client.assert.containsText(text1, 'Operational')

                    })

                })
            })
}
};

当我运行它时,我收到错误 - Testing if element <WebsiteGoogleComputeEngineOperational> contains text 'Operational' in 5000ms - expected "contains text 'Operational'" but got: "element could not be located" (5341ms)

当我在没有client.assert.containsText(text1, 'Operational') 的情况下运行时,我会收到我的字符串的完整列表

WebsiteGoogleComputeEngineOperational
APIGoogleComputeEngineOperational
Git(sshandhttps)GoogleComputeEngineOperational
PagesGoogleComputeEngineOperational
CI/CDGoogleComputeEngineOperational
BackgroundProcessingGoogleComputeEngineOperational
SupportServicesZendeskOperational
packages.gitlab.comAWSOperational
customers.gitlab.comAzureOperational
version.gitlab.comAWSOperational
forum.gitlab.comDigitalOceanOperational
WindowsRunners(beta)GoogleComputeEngineOperational
CanaryGoogleComputeEngineOperational
dashboards.gitlab.comGoogleComputeEngineOperational

问题出在哪里,我该如何解决?

【问题讨论】:

    标签: javascript automation client assert nightwatch.js


    【解决方案1】:

    您不能使用client.assert.containsText(text1, 'Operational'),因为这是为了验证元素的内部文本。你正在做的是比较两个文本,你可以使用一个简单的 if- else。

    module.exports = {
    
      test: function(client) {
    
        var text1;
    
        client
          .maximizeWindow()
          .url('https://status.gitlab.com/')
          .waitForElementVisible('img[alt="Logo"]', 10 * 1000)
          .elements('css selector', '.component',
            function(elements) {
              elements.value.forEach(function(elementsObj) {
                client.elementIdText(elementsObj.ELEMENT, function(result) {
                  text1 = result.value.replace(/\s+/g, '')
                  if (text1.includes('Operational')) {
                    console.log('Found Operational in - ' + '"' + text1 + '"' + '\n')
                  } else {
                    console.log('Didn\'t Found Operational in - ' + '"' + text1 + '"' + '\n')
                  }
                })
    
              })
            })
      }
    };
    

    执行时,您可以在控制台中看到类似的内容 -

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-01-09
      • 2015-10-06
      • 2021-05-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多