Skip to content Skip to sidebar Skip to footer

How To Create Condition "if Element Not Exist - Do Smth" By Using Nightwatch?

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 p

Solution 1:

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

functionfindStringInElementsData(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 trueif(result.value === stringToFind) {
                return callback(true);
            } 

            // The string was not found return falseif(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
        }
    })
})

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.

Post a Comment for "How To Create Condition "if Element Not Exist - Do Smth" By Using Nightwatch?"