簡體   English   中英

nightwatch js中的元素無法定位錯誤

[英]Element could not be located error in nightwatch js

我想確認我的每個字符串都有文本“操作”。 這是我的代碼

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

問題出在哪里,我該如何解決?

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

          })
        })
  }
};

執行后,您可以在控制台中看到類似的內容 -

執行截圖

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM