mlosev
mlosev

Reputation: 5227

How do I get a text of all the cells of the table using testcafe

I am using the testcafe testing framework - https://devexpress.github.io/testcafe.
I wrote the following code:

const table = Selector('#table');

for(let i = 0; i < table.rows.length; i++){
   for(let j = 0; j < table.columns.length; j++) {
         let tdText = await table.rows[i].cells[j].textContent;
        //another actions
   }
}

How do I get a text of all the cells of the table using testcafe?

Upvotes: 2

Views: 4199

Answers (2)

Boris Kirov
Boris Kirov

Reputation: 756

Do you need all text content? In this case you can just use ClientFunction

import { ClientFunction } from 'testcafe';

const getInnerText = ClientFunction(() => document.getElementById('table').innerText);

const text = await getInnerText();

Upvotes: 3

Alexander Moskovkin
Alexander Moskovkin

Reputation: 1861

Selector provides methods and properties to select elements on the page and get theirs state, but has no 'rows' and 'columns' properties.

So, use the following solution:

const table       = Selector('#table');
const rowCount    = await table.find('tr').count;
const columnCount = await table.find('tr').nth(0).find('td').count;

for(let i = 0; i < rowCount; i++) {
    for(let j = 0; j < columnCount; j++) {  
        let tdText = await table.find('tr').nth(i).find('td').nth(j).textContent;
        //another actions
    }
}

Note that Selector provides 'find' and 'nth' functions since v0.11.0 (it will be released soon, but it's available yet with npm "alpha" tag).

Upvotes: 9

Related Questions