Rayrun
← Back to Discord Forum

How to validate a Table based on cells in an order?

If I have a table, and I want to validate that a row has cells of certain values from left to right, what would be the best way to do that?

This thread is trying to answer question "What is the best way to validate that a row in a table has cells of certain values from left to right?"

5 replies
jennifer0419
jennifer0419

You can pass an array to the toContainText() assertion. There's an explanation and examples here: https://playwright.dev/docs/api/class-locatorassertions#locator-assertions-to-contain-text

jennifer0419
jennifer0419

For table cells, I use a pattern like the following. You don't have to use fixtures and could use the relevant locator in the test.

// In the constructor for a fixture:
this.rows = this.tableContainer.locator('tbody').getByRole('row');

// A convenience method in the fixture:
getRowValues(rowIndex: number) {
    return this.rows.nth(rowIndex).getByRole('cell');
}

// Assertion in a test:
expect(getRowValues(2)).toContainText(['value 1', 'value 2']);

Awesome thank you! Is there something similar to this for retrieving a locator if the values of the cells have data in that order?

The assertion does check order; it will fail if all the values are present but not in the same order. I might be misunderstanding your question, though.

Ah yes, I mean a locator rather than an assertion (aka what would be a way for me to retrieve a row based on the contents (cells that are in order))? If that makes sense

Related Discord Threads

TwitterGitHubLinkedIn
AboutQuestionsDiscord ForumBrowser ExtensionTagsQA Jobs

Rayrun is a community for QA engineers. I am constantly looking for new ways to add value to people learning Playwright and other browser automation frameworks. If you have feedback, email [email protected].