Surendra Jnawali
Surendra Jnawali

Reputation: 3240

Protractor ng-if condition and element availability

I want to check whether element displayed or not depends upon ng-if condition,

<div id="discount_percent" ng-if="data.isDiscount">  
     <div id="dis_available"> Discount: {{data.discount}} % </div> 
</div> 

I want to check if data.isDiscount is true than div "discount_percent" & dis_available should display.

Note: div "discount_percent" and "dis_available" won't be present in DOM if data.isDiscount is false.

I did following way, but I'm not fully satisfied with my solution.

var discountPercentId = element(by.id('discount_percent'));
discountPercentId.isPresent().then(function (present){
   if(present){
      expect(discountPercentId.evaluate("data.isDiscount")).toBeTruthy();
      expect(discountPercentId.element(by.id("dis_available")).isDisplayed()).toBeTruthy();
   }
}); 

Any better approach to do? If condition true than check its availability!!!

Upvotes: 1

Views: 5191

Answers (1)

sdet.ro
sdet.ro

Reputation: 327

try this:

var elem = element(by.css('[ng-if="data.isDiscount"]'));
elem.isPresent().then(function(fnct){
            if(fnct){
             //do whatever
             }
           else{
            //do something else
             }
        });

also, I am not sure, but you might want to get the value of data.IsDiscount first. You can get the value of this with the following function:

elem.getAttribute('value').then(function(value) { 
                                    //do something with the value
                                });

Upvotes: 2

Related Questions