0
votes

I am trying to test a dynamic web table using protractor and trying to find the count of headers, rows and cols but always getting 0 as the count var x = element.all(by.xpath('//table//thead//tr//th'))

x.count().then(function(c){

console.log(c);

});

I tried using element.all(by.css ) as well and it returns the same , can anyone help? I used selenium and able to retrieve the value, so xpath is not wrong, but I have to use protractor to fetch the same. Selenium script which is working List col = driver.findElements(By.xpath("//div[@class='table-wrapper']//table//thead//tr/th")); System.out.println(col.size());

html

2
if you know there is going to be at least one, you'll need to browser.wait for the presence of x.first(). Otherwise it is taking count too soon. Or you could add sleep, but that is gross. Or use async and x = await element.all... console.log (await x.count) - Jeremy Kahan

2 Answers

0
votes

Try the below code

var x = await element.all(by.css('table[title="status"]'))
//Add wait if the table take more time to load
x.count().then(function(c){
console.log(c);
});
0
votes

In general, you should avoid xpath since it's very inefficient.

This should work for you:

var table = element(by.css('table.table'));

table
 .element(by.css('thead'))
 .all(by.css('tr th'))
 .count()
 .then(function(count) {
   console.log('count:',count);
 });