Rayrun

How do I use Playwright's assertion methods to check if a list of names is sorted in ascending or descending order?

Answer

Using Playwright's Assertion Methods to Check List Order

You can use Playwright's assertion methods to check if a list of names is sorted in ascending or descending order. Here's how:

Ascending Order

First, get a reference to your page object and locate your list element. Then, define an array of expected names in ascending order. Use the toHaveText assertion to check if the list elements match the expected names.

const page = await browser.newPage();
const nameList = await page.locator('ul > li');
const expectedNamesAscending = ['Alice', 'Bob', 'Charlie'];
await expect(nameList).toHaveText(expectedNamesAscending);

If the elements match 'Alice', 'Bob', and 'Charlie' in this exact order, the assertion will pass. If not, it will fail.

Descending Order

For descending order, define an array of expected names in descending order. Use the toHaveText assertion to check if the list elements match these names.

const expectedNamesDescending = ['Charlie', 'Bob', 'Alice'];
await expect(nameList).toHaveText(expectedNamesDescending);

If the elements match 'Charlie', 'Bob', and 'Alice' in this exact order, the assertion will pass. If not, it will fail.

So, with Playwright's toHaveText assertion, you can easily check if a list of names is sorted in ascending or descending order.

References

Thank you!
Was this helpful?
Still have questions?

If you still have questions, please ask a question and I will try to answer it.

Related Discord Threads

Related Questions

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].