diff --git a/playwright/src/main/java/com/microsoft/playwright/impl/LocatorAssertionsImpl.java b/playwright/src/main/java/com/microsoft/playwright/impl/LocatorAssertionsImpl.java index 088910c98..4e107d00a 100644 --- a/playwright/src/main/java/com/microsoft/playwright/impl/LocatorAssertionsImpl.java +++ b/playwright/src/main/java/com/microsoft/playwright/impl/LocatorAssertionsImpl.java @@ -190,6 +190,18 @@ private void hasAttribute(String name, ExpectedTextValue expectedText, Object ex expectImpl("to.have.attribute.value", expectedText, expectedValue, message, commonOptions, "Assert \"hasAttribute\""); } + @Override + public void hasAttribute(String name, HasAttributeOptions options) { + if (options == null) { + options = new HasAttributeOptions(); + } + FrameExpectOptions commonOptions = convertType(options, FrameExpectOptions.class); + commonOptions.expressionArg = name; + String message = "Locator expected to have attribute '" + name + "'"; + List expectedText = null; + expectImpl("to.have.attribute", expectedText, null, message, commonOptions, "Assert \"hasAttribute\""); + } + @Override public void hasClass(String text, HasClassOptions options) { ExpectedTextValue expected = new ExpectedTextValue(); diff --git a/playwright/src/test/java/com/microsoft/playwright/TestLocatorAssertions.java b/playwright/src/test/java/com/microsoft/playwright/TestLocatorAssertions.java index 27fa7dc2f..a82195e28 100644 --- a/playwright/src/test/java/com/microsoft/playwright/TestLocatorAssertions.java +++ b/playwright/src/test/java/com/microsoft/playwright/TestLocatorAssertions.java @@ -294,6 +294,27 @@ void hasAttributeRegExpFail() { assertTrue(e.getMessage().contains("Locator expected to have attribute 'id' matching regex\nExpected: .Nod..\nReceived: node"), e.getMessage()); } + @Test + void hasAttributeNamePass() { + page.setContent("
Text content
"); + Locator locator = page.locator("#node"); + assertThat(locator).hasAttribute("id"); + assertThat(locator).hasAttribute("checked"); + assertThat(locator).not().hasAttribute("open"); + } + + @Test + void hasAttributeNameFail() { + page.setContent("
Text content
"); + Locator locator = page.locator("#node"); + AssertionFailedError e = assertThrows(AssertionFailedError.class, + () -> assertThat(locator).hasAttribute("disabled", new LocatorAssertions.HasAttributeOptions().setTimeout(1000))); + assertTrue(e.getMessage().contains("Locator expected to have attribute 'disabled'"), e.getMessage()); + e = assertThrows(AssertionFailedError.class, + () -> assertThat(locator).not().hasAttribute("checked", new LocatorAssertions.HasAttributeOptions().setTimeout(1000))); + assertTrue(e.getMessage().contains("Locator expected not to have attribute 'checked'"), e.getMessage()); + } + @Test void hasClassTextPass() { page.setContent("
");