nrs
nrs

Reputation: 945

Is it possible to print a text message, if condition is not satisfied in protractor?

My code:

this.condition = function() {
        element(by.partialLinkText('Times Jobs')).isDisplayed().then(function (result) {
            if (result) {
                element(by.partialLinkText('Times Jobs')).click();
            } else {
                console.log('Times source not found')
            }
        });
    }

If "Times Jobs" is not presented in the page then it should display whatever text inside the else statement. How can i do that?

I don't want to make test fail, just i need to print that text in console.

Upvotes: 0

Views: 67

Answers (2)

Michal Charemza
Michal Charemza

Reputation: 27012

You can use isPresent(), as in this answer, but if you want to keep using isDisplayed(), you must handle the case of the element not being found in the error callback of its returned promise:

element(by.partialLinkText('Times Jobs')).isDisplayed().then(function (result) {
  if (result) {
    element(by.partialLinkText('Times Jobs')).click();
  } else {
    console.log('Times source in page but not displayed');
  }
}, function() {
  console.log('Times source not in page');  
});

Or, if you want to output the same message in both of the cases of not being in the page, or being in the page but not displayed, you can...

element(by.partialLinkText('Times Jobs')).isDisplayed().then(function (result) {
  if (result) {
    element(by.partialLinkText('Times Jobs')).click();
  } else {
    return protractor.promise.rejected();
  }
}).catch(function() {
  console.log('Times source not in page or in page and not displayed');  
});

Upvotes: 2

Sohel Saiyed
Sohel Saiyed

Reputation: 49

You can try this ::

this.condition = function() {
        element(by.partialLinkText('Times Jobs')).isPresent().then(function (result) {
            if (result) {
                element(by.partialLinkText('Times Jobs')).click();
            } else {
                console.log('Times source not found')
            }
        });
    }

as if the element is not present on the page than it give element not found error

Upvotes: 0

Related Questions