mobify / nightwatch-commands

A set of Mobify specific custom commands for Nightwatch.js
60 stars 18 forks source link

Add if/else logic for page elements #24

Closed marlowpayne closed 8 years ago

marlowpayne commented 10 years ago

We could really use if/else logic functions for presence/visibility of page elements. This would allow us to set up tests with conditional checks and adjust the test accordingly. For example, to check whether a "Sold out"-type element is present, and divert an Add-to-cart flow to add a different, non-sold-out item.

Possible commands could be: ifElementExists(selector) ifElementNotExists(selector) ifElementVisible(selector) ifElementHidden(selector)

A possible test flow using a command could be of the form:

ifElementExists(selector) {
    do foo
} else {
    do bar
}
ellenmobify commented 10 years ago

This is the if/else code that @scalvert demonstrated during one of the Nightwatch group sessions. This snippet ends the test if the "unavailable" element is present or does something else if the element is not present.

        browser.element('css selector', selectors.unavailable, function(result){
            if (result.value && result.value.ELEMENT) {
                // Element is present, do the appropriate tests
                browser.end();
            } else {
                // Element is not present.
            }
        });
quantuminformation commented 8 years ago

+1

qa-derrick commented 8 years ago

This is probably too case specific to generalize into a custom command, the snippet above is what you should integrate into your tests.

oubre commented 8 years ago

I'm using code like @ellenmobify's example, but it's pretty messy when using page objects, and doesn't really integrate with the flow they're designed for. Being able to do something like: if (mypage.elements.myElem.is.present)

instead of:

this.element(
      'css selector',
      this.elements.myElem.selector,
      function(elem) {
        if(elem.status === -1) {
danielburkewilliams commented 7 years ago

Thanks. Using this solution and it worked perfectly. had a service that i was never sure would be on or not so checks to see if the element associated to that page is present or not. if it's not present - i move on to the next page. if it is present fill in the form and then hit the next page.

image

pakpaak commented 7 years ago

@ellenmobify just try this and work perfectly, thanks! :)

Ftelf commented 6 years ago

That's nice, but how to use it using @ alias ?