4

how to check variable in scope without ng-bind and ng-model ?

<span ng-if="user.code === 'YELLOW'">
    <span class="glyphicons glyphicons-circle-remove text-danger"></span> Yellow
</span>
<span ng-if="user.code === 'GREEN'">
    <span class="glyphicons glyphicons-circle-exclamation-mark text-warning"></span> Green
</span>

I would like to check the user.code with protractor

With ng-bind and ng-model it doesn't work

browser.expect(element(by.binding('user.code')).getText()).to.eventually.equal('');

or

browser.expect(element(by.model('user.code')).getAttribute('value')).to.eventually.equal('');

Error :

NoSuchElementError: No element found using locator: by.binding("user.code")
NoSuchElementError: No element found using locator: by.model("user.code")
Jérémie Chazelle
  • 1,721
  • 4
  • 32
  • 70

2 Answers2

2

You should not be testing that kind of stuff in protractor. You should write your tests from the perspective of the user.

However, if you want to test that use evaluate. Get a reference to a DOM element and then call evaluate:

// Choose a DOM element bound to a scope that contains the value you
// want to test
expect($('span').evaluate('user.code')).toBe('some value');

http://www.protractortest.org/#/api?view=ElementArrayFinder.prototype.evaluate

Andres D
  • 8,910
  • 2
  • 26
  • 31
1

First solution

If you can locate your element through CSS selector.

$('.glyphicons-circle-remove').isPresent(); // For user.code == 'YELLOW'
$('.glyphicons-circle-exclamation-mark').isPresent(); // For user.code == 'GREEN'

Hacky way

Work only If you are using ng-if

$('[ng-if="user.code === \'YELLOW\'"]').isPresent(); // For user.code == 'YELLOW'
$('[ng-if="user.code === \'GREEN\'"]').isPresent(); // For user.code == 'GREEN'

Since the element is only presenting when condition of ng-if satified. So you can also do this.

NOTE: .isPresent() because ng-if will remove the element from DOM. If you are using ng-show you will need to use .isDisplayed() instead (this does not work for second solution).

Linh Pham
  • 3,005
  • 23
  • 34