Skip to content

Instantly share code, notes, and snippets.

@dapperdandev
Last active August 8, 2022 00:36
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dapperdandev/7760124a98847389dbf72d2eb85eb5de to your computer and use it in GitHub Desktop.
Save dapperdandev/7760124a98847389dbf72d2eb85eb5de to your computer and use it in GitHub Desktop.
TestCafe: Finding Cells In a Table
<!-- served up with `live-server --port=5000` -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<table>
<thead>
<tr>
<th>Id</th>
<th>First Column Header</th>
<th>Second Column Header</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>Red</td>
<td>Red</td>
</tr>
<tr>
<td>2</td>
<td>Yellow</td>
<td>Yellow</td>
</tr>
<tr>
<td>3</td>
<td>Green</td>
<td>Green</td>
</tr>
</tbody>
</table>
</body>
</html>
import { Selector } from 'testcafe';
import { TableIterator } from './tableIterator';
class SomeTablePage {
findRow(id: string): Selector {
try {
return Selector('td').withExactText(id).parent('tr')
} catch (err) {
console.log(err)
}
}
async getRowCells(tableRow: Selector) {
try {
const firstColumn = await tableRow.find('td').nth(await TableIterator.getHeaderIndex('First Column Header')).innerText;
const secondColumn = await tableRow.find('td').nth(await TableIterator.getHeaderIndex('Second Column Header')).innerText;
return {
firstColumn: firstColumn,
secondColumn: secondColumn
};
} catch (err) {
console.log(err)
}
}
}
export const someTablePage = new SomeTablePage();
import {
someTablePage
} from './someTable.page';
fixture(`SOME TABLE`)
test('Find Third Row', async t => {
await t.navigateTo('http://127.0.0.1:5000');
const data = {
id: "3",
firstColumn: "Green",
secondColumn: "Green"
};
let tableRow = someTablePage.findRow(data.id);
let tableCells = await someTablePage.getRowCells(tableRow);
await t
.expect(tableCells.firstColumn).eql(data.firstColumn)
.expect(tableCells.secondColumn).eql(data.secondColumn)
});
import { Selector } from 'testcafe';
export class TableIterator {
static async getHeaderIndex(headerText: any): Promise<number> {
interface CustomSelector extends Selector {
getIndex: Promise<number>
}
const headerElement = <CustomSelector>Selector('th').withText(headerText).addCustomDOMProperties({
getIndex: el => {
const nodes = Array.prototype.slice.call(el.parentElement.children);
return nodes.indexOf(el)
}
})
return await headerElement.getIndex
}
}
@anjanarajagopalan
Copy link

I have two rows within a table, one as "SC 11" and another as "SC 1". It appears the iterator always returns "SC 11" and does not return "SC 1".

@dapperdandev
Copy link
Author

I have two rows within a table, one as "SC 11" and another as "SC 1". It appears the iterator always returns "SC 11" and does not return "SC 1".

Hi @anjanarajagopalan!

I completely forgot this gist was public. Anyway, this was written before TestCafe added a withExactText option. Just shooting off the cuff here, but I imagine that's the problem.

Try changing: return Selector('td').withText(id).parent('tr') to return Selector('td').withExactText(id).parent('tr') and see what happens

@anjanarajagopalan
Copy link

I didn't think of that. It works beautifully.

@dapperdandev
Copy link
Author

Glad to hear!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment