Javascript 在nightwatch js中找不到元素错误

Javascript 在nightwatch js中找不到元素错误,javascript,automation,client,assert,nightwatch.js,Javascript,Automation,Client,Assert,Nightwatch.js,我想确认我的每个字符串都有文本“Operational”。这是我的密码 module.exports = { test: function (client) { var text1; client .maximizeWindow() .url('https://status.gitlab.com/') .waitForElementVisible('img[alt="Logo"]', 10 * 1000)

我想确认我的每个字符串都有文本“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')

                    })

                })
            })
}
};
当我运行此程序时,我收到错误-
测试元素是否在5000ms内包含文本“可操作”-预期“包含文本“可操作”,但得到:“元素无法定位”(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

问题在哪里?我如何解决这个问题?

您不能使用
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')
              }
            })

          })
        })
  }
};
执行后,您可以在控制台中看到类似的内容-


谢谢!这对我有用