Protractor: Getting the count of child elements who match a certain value using protractor

Created on 26 Feb 2014  路  8Comments  路  Source: angular/protractor

Hi All,

I have a parent DIV in a non angular page and in side that paternt DIV there are many child DIVs also in those child DIVs I have many tags with data . The structure of the HTML is given as attachment and I want to test the project using Protractor.

Now I want to get the count of those elements whose project name is tester . How to do that?

question

Most helpful comment

You can do this by getting things by class and doing a map function:

var names = element.all(by.className('project-name')).map(function(elem) {
  return elem.getText();
});
// now names will resolve to an Array of strings like ['Manager', 'Manager', 'Developer', ...]

All 8 comments

Sorry , the attachment is given below because if i put the HTML markup in page it is not appearing as HTML markup.
html

You may want to read over http://github.github.com/github-flavored-markdown/ to learn how to use markdown in github comments.

You can do this by getting things by class and doing a map function:

var names = element.all(by.className('project-name')).map(function(elem) {
  return elem.getText();
});
// now names will resolve to an Array of strings like ['Manager', 'Manager', 'Developer', ...]

Hi,

Then what will be the case when the child elements don't have the same clas name. Like in the below screenshot I wan to count the total number of li element under ul element.
child

element.all(by.css('#menu-menu-teniker-left li')).count()

@juliemr I've been trying to use your example to convert to a string array from above, but I am failing miserably. The code I've got looks like this:

var viewNames = element.all(by.className('interaction-thumb-titlebar')).map(function(element) {
    return element.getText();
});
console.log("View names:", viewNames);

Now I'd expect this to spit out something like:

View names: ['item1', 'item2', 'item3']

But instead, what I'm getting is this:

View names: { closure_uid_834287284: 2004,
  flow_: 
   { events_: {},
     closure_uid_834287284: 1,
     activeFrame_: 
      { events_: {},
        closure_uid_834287284: 1954,
        flow_: [Circular],
        parent_: [Object],
        children_: [Object],
        lastInsertedChild_: [Object],
        pendingTask_: null,
        isLocked_: false,
        isBlocked_: false,
        pendingCallback: false,
        pendingRejection: false,
        cancellationError_: null },
     schedulingFrame_: 
      { events_: {},
        closure_uid_834287284: 1954,
        flow_: [Circular],
        parent_: [Object],
        children_: [Object],
        lastInsertedChild_: [Object],
        pendingTask_: null,
        isLocked_: false,
        isBlocked_: false,
        pendingCallback: false,
        pendingRejection: false,
        cancellationError_: null },
     shutdownTask_: null,
     eventLoopTask_: null,
     hold_: 
      { _idleTimeout: 2147483647,
        _idlePrev: [Object],
        _idleNext: [Object],
        _idleStart: 1437578564712,
        _onTimeout: [Function: wrapper],
        _repeat: true },
     yieldCount_: 3 },
  stack_: null,
  parent_: 
   { closure_uid_834287284: 2002,
     flow_: 
      { events_: {},
        closure_uid_834287284: 1,
        activeFrame_: [Object],
        schedulingFrame_: [Object],
        shutdownTask_: null,
        eventLoopTask_: null,
        hold_: [Object],
        yieldCount_: 3 },
     stack_: null,
     parent_: 
      { closure_uid_834287284: 2000,
        flow_: [Object],
        stack_: null,
        parent_: [Object],
        callbacks_: [Object],
        state_: 'pending',
        handled_: true,
        pendingNotifications_: false,
        value_: undefined },
     callbacks_: [ [Object] ],
     state_: 'pending',
     handled_: true,
     pendingNotifications_: false,
     value_: undefined },
  callbacks_: null,
  state_: 'pending',
  handled_: false,
  pendingNotifications_: false,
  value_: undefined }

I wondered if this had something to do with getText returning a promise rather than a string but I get the exact same result if I change return element.getText() to return 'turtle'

I'm really rather baffled, could you lend me a hand?

Hey, Julie is about to leave for sabbatical. Also we've moved Q&A to stack overflow so please ask your questions there

Fair enough. For anyone who lands here, here is a link to my question: http://stackoverflow.com/questions/31581986/protractor-how-to-count-elements-containing-specified-text

Was this page helpful?
0 / 5 - 0 ratings