1

I need to access to a div with a ng-show directive WITHOUT using xpath

<div ng-show="my_error && dirty_field">
  Custom error message.
</div>

I tried these but it doesn't work properly

element(by.css('[ng-show=my_error && dirty_field]'));

and

element(by.model('my_error && dirty_field'));

How can I do?

ZaitseV
  • 179
  • 2
  • 14
  • 1
    Try the below code. element(by.css("[ng-show='my_error && dirty_field']")); You missed to add "'" quotes for ng-show value – Sudharsan Selvaraj Jun 09 '16 at 10:30
  • I tried also with quotes (it doesn't work anyway) but somewhere somebody has written that quotes should not be used.. – ZaitseV Jun 09 '16 at 10:35
  • Can you please post the error message that you are getting – Sudharsan Selvaraj Jun 09 '16 at 10:38
  • It simple throws *Expected true to be falsy* in any case... `var el = element(by.css("[ng-show='my_error && dirty_field']")); expect(el.isPresent()).toBeFalsy(); expect(el.isPresent()).toBeTruthy();` but with xpath it works correctly.. – ZaitseV Jun 09 '16 at 10:49
  • I was using wrong Element Finder comparison... I must use isDisplayed instead of isPresent.. – ZaitseV Jun 09 '16 at 13:09

2 Answers2

1

Just to add few points here.

First of all, you definitely need the quotes around the ng-show value in this case:

element(by.css('[ng-show="my_error && dirty_field"]'));

This is because the value contains non-alphanumeric characters. See this related thread:

Also, I don't think you should use the dirty_field part in your locator. This sounds like a more technical variable used in the form validation logic. I'd use the "contains" check instead to check the my_error part only (note how I've removed the quotes in this case - the value is alphanumeric):

element(by.css('[ng-show*=my_error]'));

Also note that you can use the $ shortcut instead of element(by.css()):

$('[ng-show*=my_error]');
Community
  • 1
  • 1
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
0

You cab deal with the properties this way in doing protractor testing.

element.all(by.css('[ui-sref="about_us"]')).first();
element(by.css('[ng-click="clickme()"]')),
element(by.css('[ng-show*=show_me]'));
Zaman Afzal
  • 2,009
  • 17
  • 23