house9

random code and what not

Protractor - promises all the way down

| Comments

Expected undefined to equal 7.

We recently started using Protractor for end to end testing of an angular application. Protractor returns promises when locating elements on the page, in some cases you may need to do your assertions inside of callback functions.

For example:

1
2
3
4
5
6
7
8
9
10
// this works just fine
expect(element(by.id('email')).getText()).toEqual('x@x.com');

// this will fail
expect(element(by.id('email')).getText().length).toEqual(7);

// getText retuns a promise and not a string, instead...
element(by.id('email')).getText().then(function (data) {
  expect(data.length).toEqual(7);
});

You can use the elementexplorer to debug your locators, but be aware that even non-matched elements will return an ‘Element Finder’ object.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
node node_modules/protractor/bin/elementexplorer.js

Getting page at: about:blank
> browser.get('http://www.angularjs.org');
null
> element(by.model('xxxx'))
{ click: [Function],
  sendKeys: [Function],
  getTagName: [Function],
  getCssValue: [Function],
  getAttribute: [Function],
  getText: [Function],
  getSize: [Function],
  getLocation: [Function],
  isEnabled: [Function],
  isSelected: [Function],
  submit: [Function],
  clear: [Function],
  isDisplayed: [Function],
  getOuterHtml: [Function],
  getInnerHtml: [Function],
  toWireValue: [Function],
  findElements: [Function],
  isElementPresent: [Function],
  evaluate: [Function],
  findElement: [Function],
  find: [Function],
  isPresent: [Function],
  element: { [Function] all: [Function] },
  '$': [Function],
  '$$': [Function] }

However, invoking one of the methods on an unmatched element will raise a NoSuchElementError.

Resources:

Comments