Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the count of the number of child div element present inside a root div element in 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="page-kontact-main">
  <div class="buttons contact_page">
    <a href="http://mysite.se/om-oss/" class="btn btn-default button">Om oss</a>
    <a href="http://mysite.se/offert-3/" class="btn btn-default button">Offert</a>
    <a href="http://mysite.se/tjanster/" class="btn btn-default button">Våra Tjänster</a>
  </div>

  <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://mysite.se/kontakt/");
        var element = driver.findElement(protractor.By.css('.clearfix .col206'));
        expect(element.length).toEqual(21);

    });

Thanks Utpal

like image 326
Utpal Avatar asked Feb 25 '14 10:02

Utpal


2 Answers

You should use element.all and count() instead of length:

it('Getting the count of contact names ', function() {
    driver.get("http://mysite.se/kontakt/");
    var elements = element.all(protractor.By.css('.clearfix .col206'));
    expect(elements.count()).toEqual(21);
});
like image 50
Bastien Caudan Avatar answered Nov 17 '22 21:11

Bastien Caudan


$ and $$ are equivalent to element.all and by.css, so you can use the following code

it('Getting the count of contact names ', function() {
    driver.get("http://mysite.se/kontakt/");
    var elements = $$('.clearfix .col206'));
    expect(elements.count()).toEqual(21);
});
like image 23
Andrii Malykhin Avatar answered Nov 17 '22 20:11

Andrii Malykhin