简体   繁体   中英

Protractor - count elements in xpath and store it to var

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}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM