Protractor: How to get the count of the number of child div element present inside a root div element in Protractor

Created on 25 Feb 2014  ·  3Comments  ·  Source: angular/protractor

I am testing a web application project using Protractor . The few test cases which I have written is running fine but I am getting difficulties when I try to count the number of div elements present inside a parent div element, The structure of my div is given below

  <div class="row kontact-title">
    <h2>Våra medarbetare</h2>
    <p>
      Test desc <span class="s47jd228h2" id="s47jd228h2_5">med</span> passion och engagemang för it &amp; system.  &nbsp;Skicka in din ansöka
    </p>
    <p>&nbsp;</p>
  </div>

  <div class="row clearfix">
    <div class="col206"></div>
    <div class="col206"></div>
    <div class="col206"></div>
    <div class="col206"></div>
    <div class="col206"> </div>
    <div class="col206"></div>
    <div class="col206"></div>
    <div class="col206"> </div>
  </div>
</div>

And I have written the test case like this as given below but it is not running the error is expected undefined to equal 21.

it('Getting the count of contact names ', function() {
        driver.get("http://likipe.se/kontakt/");
        var element = driver.findElement(protractor.By.css('.clearfix .col206'));
        expect(element.length).toEqual(21);

    });

Thanks
Utpal

Most helpful comment

What if the sub div elements don't have classes? How can we count the number of child div elements in the following situation?

<div class="row clearfix">
    <div data-selectable>1</div>
    <div data-selectable>2</div>
    <div data-selectable>3</div>
    <div data-selectable>4</div>
    <div data-selectable>5</div>
    <div data-selectable>6</div>
</div>

All 3 comments

I'm using latest protractor, so using browser.driver instead of driver and this is the code that does what you need:

it('should get correct count of contact names', function() {
    browser.driver.get("http://likipe.se/kontakt/");
    browser.driver.findElements(by.css('.clearfix .col206')).
        then(function(elems) {
            expect(elems.length).toEqual(21);
        }
    );
});                

Thanks a lot. It resolve the issue.

What if the sub div elements don't have classes? How can we count the number of child div elements in the following situation?

<div class="row clearfix">
    <div data-selectable>1</div>
    <div data-selectable>2</div>
    <div data-selectable>3</div>
    <div data-selectable>4</div>
    <div data-selectable>5</div>
    <div data-selectable>6</div>
</div>
Was this page helpful?
0 / 5 - 0 ratings