1

I need to store the value of number of records from the table in some variable in order to use it lately. For now tried to simple print number of records like this:

    var rows = element.all(by.xpath('//*[@id="content"]/div/div/table/tbody/tr'));
    var sum = rows.count();
    console.log(sum.getText());

or like that:

element.all(by.xpath('//*[@id="content"]/div/div/table/tbody/tr')).count().then(function(count) { console.log(count); });

but each time I am getting Failed: object is not a function. Anyone could help with that ?

Tried solutions from this thread: Protractor - count elements in repeater and print it - but did not work.

I would like to do scenario similiar to Java code:

@Test
public void test() throws Exception {
    When:
    clickOn(welcomePage.usersButton);
    clickOn(usersPage.archivedTab);
    int numberOfRecords = getDriver().findElements(By.xpath("//*[@id=\"content\"]/div/div/table/tbody/tr")).size();
if (numberOfRecords > 0) {  do test, test steps ar here  } else {break test}
Community
  • 1
  • 1
Michal
  • 563
  • 3
  • 14
  • 24

1 Answers1

3

First thing, your sum variable is not a protractor element object to perform getText() on it. You have to get the promise that count() function returns to store it in the sum variable. Here's how -

var rows = element.all(by.xpath('//*[@id="content"]/div/div/table/tbody/tr'));
rows.count().then(function(count){
    var sum = count;
    console.log(sum);
});

If you want to assert the value of your count() of the elements then expect it to be greater than 0 using jasmine. Here's how -

expect(element.all(by.xpath('//*[@id="content"]/div/div/table/tbody/tr')).count()).toBeGreaterThan(0);

element.all() waits for implicit wait time to get the elements present in the DOM. Once the implicit wait timeouts, it returns the elements that it finds. If it doesn't find any element with the locator then it returns zero.

Hope it helps.

giri-sh
  • 6,934
  • 2
  • 25
  • 50
  • I wanted to make somethign similiar to this simple java code: `int numberOfRecords = getDriver().findElements(By.xpath("//*[@id=\"content\"]/div/div/table/tbody/tr")).size(); assumeTrue(numberOfRecords > 0);` - so it must handle situation with none elements and less than one element. Any more ideas ? – Michal Oct 01 '15 at 11:57
  • @Michał updated answer for your query. I believe it should work for all conditions, even when you have zero elements. Do you get same error when you use the expect statement mentioned above? – giri-sh Oct 01 '15 at 12:25
  • Are you sure that your script fails when you expect it the way it is shown above? Which line of the code is showing the error for you in console? – giri-sh Oct 01 '15 at 12:32
  • It was problem with waiting on archivedTab :) Now it works fine - but , how to store it into variable for later usage ? – Michal Oct 01 '15 at 12:44
  • First method should work for you where you store it using the promise that `count()` function returns. It stores the value in variable sum and you can use it elsewhere. If you want it to be available globally then do declare it globally, which i haven't done in the code above. – giri-sh Oct 01 '15 at 12:46