microsoft / microsoft/playwright-java

[Feature]: Implement hasAttribute(String)

Open
#1,844 3 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

P3-collecting-feedback
Dominant language
Java
Stars
1.6k
Forks
298
Avg merge
3d 2h
Merged PRs (30d)
14

Description

🚀 Feature Request

I want to check if a locator has an attribute.
I think that's what toHaveAttribute(name) is doing in typescript.

I tried to:
assertThat(getInputLocator()).not().hasAttribute("min", ""); // that will forbid empty min but not a filled value
or
assertThat(getInputLocator()).not().hasAttribute("min", Pattern.compile(".*"));

And the result is:

  • unexpected value "undefined"

With a new API hasAttribute(String attributeName), I would be able to do:
assertThat(getInputLocator()).not().hasAttribute("min");

That will test if there is an attribute min.

Example
import com.microsoft.playwright.assertions.PlaywrightAssertions;
import java.util.regex.Pattern;

Locator el = page.locator("#my-el");

// Assert the attribute is NOT present:
PlaywrightAssertions.assertThat(el)
    .not()
    .hasAttribute("min", Pattern.compile(".*"));
Motivation

The only workaround I found is to get the attribute and asserts it's null. But it's not the good practice.

Having that will help to check to the absence of a attribute.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at the Java assertion entry point shown in the issue, PlaywrightAssertions.assertThat, and inspect the existing hasAttribute overloads. Confirm how locator attribute absence is represented and add the requested String-only form so presence and absence can be asserted without a value; done means the API supports the shown .not().hasAttribute("min") usage.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
testing-qa
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.