如何创建条件“如果元素不存在-做某事"通过使用Nightwatch? [英] How to create condition "if element not exist - do smth" by using Nightwatch?

查看:74
本文介绍了如何创建条件“如果元素不存在-做某事"通过使用Nightwatch?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个按表列出的用户列表,我需要搜索具有适当值的td元素-如果该元素在当前页面上不存在,请单击链接进入另一页面. 我不知道如何执行此操作,但是我使用了xpath选择器来查找具有某些值的元素.

I have a list of users, presented by table, and I need to search for td element with appropriate value - and if element not exist on current page - click on link to go on another page. I have no ideas how to do this but I used xpath selector to find element with some value.

每个用户由"tr"元素呈现,并在其中包含"td"元素:

Each user presented by "tr" element and contains "td" elements inside:

<tr>
 <td class="break-all">acceptance</td>
 <td>client</td>
 <td class="break-all"></td>
</tr>

因此,如果当前页面上不存在td元素,则需要找到其值为="acceptance"-单击链接以转到另一页并继续搜索.

So, I need to find td element with value = "acceptance", if it's not exist on current page - click on link to go on another page and continue the search.

我找到了解决方案,它对我有用!

function isActive(client) {
  client.getAttribute("div.right", "class", function (result) {
    if (result.value == "right active") {
      client
        .click("div.icon-caret-right")
        .perform(function () {
          findByText(client)
        })
    } else {
      //Throw error in this case!!!
    }
  })
}

  function findByText(username, client) {
    client.elements("xpath", "//td[text()='" + username + "']", function (result) {
      if (!result.value.length) {
        isActive(client)
      }
    })
  }

  module.exports = findByText;

然后我打电话给findByText(username, client).

但是现在我还有另一个问题-如何从else语句引发错误?

But now I have another question - how to throw error from else statement???

推荐答案

也许像这样,也许有一个更简单的解决方案:

Maybe something like this, there might be an easier solution though:

function findStringInElementsData(elements, stringToFind, callback) 
{
    var counter = 0,
    numberOfElements = elements.value.length;

    elements.value.forEach(function(element){
        browser.elementIdText(element.ELEMENT, function(result){

            // The string was found, return true
            if(result.value === stringToFind) {
                return callback(true);
            } 

            // The string was not found return false
            if(numberOfElements-1 === counter) {
                return callback(false);
            }
            counter++;
        })
    })
}

browser
.waitForElementVisible('td', 2000)
.elements('css selector', 'td', function(elements){

    findStringInElementsData(elements, "acceptance", function(foundString) {

        if(foundString) {
            // The string was found
        } else {
            // The string was not found, click on link to go on another page
        }
    })
})

上面的代码将获取所有td元素,然后调用findStringInElementsData函数,该函数将循环遍历它们,以查看其文本值等于提供的字符串(在您的情况下为"acceptance").如果找到该字符串,则该函数将返回true,否则将返回false.

The code above will get all td elements and then call the findStringInElementsData function which will loop through them to see it their text value is equal to a supplied string ("acceptance" in your case). If the string is found the function will return true, else it will return false.

这篇关于如何创建条件“如果元素不存在-做某事"通过使用Nightwatch?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆