Closed marlowpayne closed 8 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.
}
});
+1
This is probably too case specific to generalize into a custom command, the snippet above is what you should integrate into your tests.
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) {
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.
@ellenmobify just try this and work perfectly, thanks! :)
That's nice, but how to use it using @ alias ?
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: