I want to check if an element exists; if it does not I don't want an error or failure

3,063 views
Skip to first unread message

Aaron Kuehn

unread,
Jul 28, 2015, 3:42:16 PM7/28/15
to NightwatchJs
In the page I'm testing, two buttons may be displayed: BASIC or ADVANCED.

I want to be able to tell if the ADVANCED button is showing -- and if so, click it.

If the BASIC button is showing, I want to do nothing and continue with my test.

All of the Nightwatchjs options I've experimented with generate a failure message. For example if I "waitforpresent" or "waitforvisible" and the button is not there, it generates an error/failure. I just want to know which button is present so I can make a decision in my code.

Thoughts?

Daniel Rinehart

unread,
Jul 29, 2015, 8:29:15 AM7/29/15
to nightw...@googlegroups.com
Would isVisible with a callback support your use case:

http://nightwatchjs.org/api#isVisible

--
You received this message because you are subscribed to the Google Groups "NightwatchJs" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nightwatchjs...@googlegroups.com.
To post to this group, send email to nightw...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/nightwatchjs/597972be-afb8-47a7-b50c-8173fd4746d6%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Aaron Kuehn

unread,
Jul 29, 2015, 11:35:27 AM7/29/15
to NightwatchJs, dan...@neophi.com
The problem is that if isVisible comes back negative (i.e. the button is not visible) that shows as an error.

Daniel Rinehart

unread,
Jul 29, 2015, 2:29:12 PM7/29/15
to Aaron Kuehn, NightwatchJs
Perhaps using a similar technique with waitForElementPresent using its abortOnFailure flag? I'm guessing here since I've not tried to setup a branching test like this.

Erica S

unread,
Jul 29, 2015, 2:35:48 PM7/29/15
to NightwatchJs, aaron...@gmail.com
This is what i did


     browser.element('id', 'yourid', function(visible) {
        if(visible.status !== -1){
 
 DO SOMETHING IF IT EXISTS
 
        } else {
              DO SOMETHING ELSE IF IT DOESN'T EXIST
   
        }
    });

Aaron Kuehn

unread,
Aug 6, 2015, 9:52:42 AM8/6/15
to NightwatchJs, aaron...@gmail.com
Erica: Thank you! This lead me in the right direction. Here's the code I ended up with for my situation. Thanks again!

// CHECK WHETHER THE BASIC OR ADVANCED BUTTON IS VISIBLE.
// THE SELECTOR FOR BROWSER.ELEMENT IS THE ADVANCED BUTTON.
browser.element('css selector', 'html.mozilla body#jira.aui-layout.aui-theme-default.ka.ajax-issue-search-and-view.page-type-navigator div#page section#content div.navigator-container div.navigator-body div.contained-content form.navigator-search.query-component.generic-styled div.aui-group div.aui-item.search-wrap div.search-container div.search-options-container div.mode-switcher a.switcher-item.active', function(visible) {
// 0 INDICATES THAT THE ADVANCED BUTTON WAS FOUND -- CLICKING IT WILL TOGGLE TO ADVANCED MODE.
if (visible.status == 0) {
console.log('Switching to Advanced Search Mode....');
browser.waitForElementVisible('html.mozilla body#jira.aui-layout.aui-theme-default.ka.ajax-issue-search-and-view.page-type-navigator div#page section#content div.navigator-container div.navigator-body div.contained-content form.navigator-search.query-component.generic-styled div.aui-group div.aui-item.search-wrap div.search-container div.search-options-container div.mode-switcher a.switcher-item.active',5000,'%s Found.')
browser.click('html.mozilla body#jira.aui-layout.aui-theme-default.ka.ajax-issue-search-and-view.page-type-navigator div#page section#content div.navigator-container div.navigator-body div.contained-content form.navigator-search.query-component.generic-styled div.aui-group div.aui-item.search-wrap div.search-container div.search-options-container div.mode-switcher a.switcher-item.active')
}
});
Reply all
Reply to author
Forward
0 new messages