0

HTLM source:

<ul id="dropdownSelectRole" class="dropdown-menu" role="menu"> <li><a href="#"></a></li> <li><a href="#">Admin</a></li> <li><a href="#">aaaa</a></li> <li><a href="#">abcd</a></li> <li><a href="#">admin</a></li> </ul>

JS code:

'should verify counter on active tab': function (browser) {
    //Given
    var welcome = browser.page.welcome();
    var users = browser.page.users();
    //When
    welcome.click('@usersButton');
    var count = users.size.element('@table');
    //Then
    users.expect.element('@activeTab').contains.text(count);
},

My code doesn't work cause still getting errors like TypeError: Cannot read property 'element' of undefined or TypeError: undefined is not a function.

What i need is to take the number of elements from the HTML table and put it to some string in order to assert later on with value on a tab. Simple test that number displayed in a tab is relevant to number of records.

I am using page object (users and welcome are pseudo-classes with listed elements selectors, @table is selector of the table with elements, @activeTab is selector of the tab which contain number of records).

To make question simply I need in JS something like this JAVA:

int count = getDriver().findElements(By.xpath("//*@id=\"content\"]/div/div/table/tbody/tr")).size(); String count1 = Integer.toString(count);

Jaroslav Kadlec
  • 2,505
  • 4
  • 32
  • 43
Michau
  • 59
  • 1
  • 9

1 Answers1

0

If you have jQuery, you may use size(), or length (if version greater than 1.8)

$(object).size(); //$(object).length

Or use something like this

var count = 0;
for(var i in object) {
    count++;
}
//If you need count only attributes (not functions)
var count = 0;
for(var i in object) {
    if(object.hasOwnProperty(i)) {
        count++;
    }
}
  • Second part of code is pure js, you can use it without jQuery – Aleksey Krivtsov Sep 08 '15 at 10:52
  • `.length` is the plain javascript method to count array elements. If you select some elements with jQuery, it returns -compare to plain javascript it returns not an array if the size of result is zero or one- always an array. So it doesn't need Jquery 1.8+ – Reporter Sep 08 '15 at 11:49