Rayrun
← Back to Discord Forum

how to assert that the button is pressed using the aria-pressed accessibility

Hi, I am trying to check if the <button aria-pressed="true" /> is pressed via toHaveAttribute but it always returns an empty string

This thread is trying to answer question "How can I check if a button with the 'aria-pressed="true"' attribute is pressed using the 'toHaveAttribute' method?"

0 replies

Answer

Checking Button Attributes with Playwright

Playwright's API is a powerful tool for interacting with elements on a webpage. Let's say you want to check if a button with the 'aria-pressed=true' attribute is pressed.

First, you'll need to locate the button element. Playwright allows you to do this using locators. Here's how you can find a button by its role and name:

const button = await page.getByRole('button', { name: /submit/i });

Now, you might want to check if this button has the 'aria-pressed' attribute set to true. While the Playwright API doesn't explicitly mention a toHaveAttribute method, if such a method exists in your testing framework, you could use it like this:

expect(button).toHaveAttribute('aria-pressed', 'true');

This code checks if the button has an 'aria-pressed' attribute with a value of 'true'. If not, it throws an assertion error.

Remember, the specifics of how you check attributes might vary depending on the libraries or frameworks you're using alongside Playwright. If you're unsure, it's always a good idea to refer to the official documentation or ask for help in relevant communities.

For more insights on using Playwright, check out Mastering Playwright Test Automation: Your Comprehensive Cheat Sheet and Understanding the Difference Between Locators and Selectors in @playwright/test.

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 luc@ray.run.