Protractor: No way to get number of elements returned by `findElements`

Created on 10 Jul 2013  路  8Comments  路  Source: angular/protractor

This issue may not be Protractor related but I'm not sure where else to look. My setup is using Protractor with jasmine-node to test an Angular front-end to a Rails app. When I try to get how many elements are found by protractor.findElements it always errors out.

My spec file follows the onJasmineNode example. So far I've tried the following:

  • ptor.findElements(protractor.By.css('.selector-string')).size();: [object object] has no method size
  • ptor.findElements(protractor.By.css('.selector-string')).count();: [object object] has no method count
  • ptor.findElements(protractor.By.css('.selector-string')).length;: undefined is not a function

For fun I wrapped the findElements in a console.log and it returns this:

{ then: [Function: then],
  cancel: [Function: cancel],
  isPending: [Function: isPending] }

Most helpful comment

Hi,

like all webdriver functions, findElements returns a promise. So, to get the length, you'll need to do

ptor.findElements(protractor.By.css('selector-string')).then(function(elems) {
  var length = elems.length; // Here's your length!
});

All 8 comments

Hi,

like all webdriver functions, findElements returns a promise. So, to get the length, you'll need to do

ptor.findElements(protractor.By.css('selector-string')).then(function(elems) {
  var length = elems.length; // Here's your length!
});

Fantastic! Thank you so much. :smile:

It would be nice however it this would integrate well with jasmin matchers (https://github.com/pivotal/jasmine/wiki/Matchers), such that you could write:

expect(ptor.findElements(protractor.By.css('.selector-string'))).
  toContain(3);

So apparently this video is incorrect at 25:40 by indicating you can simply use .length()?
https://www.youtube.com/watch?v=idb6hOxlyb8

Actually there are all kinds of things in that video that dont work? Im confused.

The .length bit is incorrect, not sure where that came from. element.all(...).count() should do the trick.

:+1: on the count() - thanks @juliemr

@juliemr in your example above it returns the length, but can you also use this to be able to use the .sendKeys() function to the element? Have an element inside a tab, inside a directive and this is the only piece of code thats sent anything back from it.

@EvanBurbidge Yes, you should be able to do a number of things with the element in the example above, including sendKeys().

Was this page helpful?
0 / 5 - 0 ratings

Related issues

andyman3693 picture andyman3693  路  3Comments

rafalf picture rafalf  路  3Comments

nt3rp picture nt3rp  路  3Comments

davidkarlsen picture davidkarlsen  路  3Comments

codef0rmer picture codef0rmer  路  3Comments