Conditional commands without throwing error?

1,696 views
Skip to first unread message

Yedidya Weiner

unread,
Dec 10, 2015, 10:22:56 AM12/10/15
to NightwatchJs
In regards to the actual tests, I am trying to conditionally run a certain command. If element is present, then execute this. Using a JS If statement, nightwatch throws an error if the element is not present, as well as if I use the .isVisible command with a callback.

  
function iterateAndClick(elements) {
    elements
.value.forEach(function(element) {
        browser
.elementIdClick(element.ELEMENT)
   
})
};

if (browser.assert.elementPresent('select')) {
    browser
.elements('css selector', 'select.form-control > option:nth-of-type(2)', iterateAndClick)
}


or

.isVisible('#comment-textarea', function(result) {
        browser
.setValue('textarea[id=comment-textarea]', 'OL TEST 1234')
});

Is there a way to check if the element is present and, if it is, execute the command, and if it isn't, to graze over it and continue on without throwing an error?

Ellen Wong

unread,
Dec 10, 2015, 12:28:56 PM12/10/15
to NightwatchJs
Use browser.element to check for the presence of the element. You can do the if/else in the callback:

        browser.element('css selector', 'select', function(result){
            if (result.value && result.value.ELEMENT) {
                // Element is present, do the appropriate tests
            } else {
                // Element is not present.
            }
        });

Elad Moshe

unread,
Jul 24, 2016, 7:43:51 AM7/24/16
to NightwatchJs
That work, thanks!
I opened an issue at Github asking to have isVisible fixed.

Naveed Sami

unread,
Aug 6, 2025, 8:51:33 AMAug 6
to NightwatchJs
Reply all
Reply to author
Forward
0 new messages