Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1565,6 +1565,36 @@ default void hasAttribute(String name, Pattern value) {
* @since v1.20
*/
void hasAttribute(String name, Pattern value, HasAttributeOptions options);
/**
* Ensures the {@code Locator} points to an element with given attribute. When only the attribute name is provided, the
* method asserts attribute presence.
*
* <p> <strong>Usage</strong>
* <pre>{@code
* assertThat(page.locator("input")).hasAttribute("disabled");
* assertThat(page.locator("input")).not().hasAttribute("open");
* }</pre>
*
* @param name Attribute name.
* @since v1.62
*/
default void hasAttribute(String name) {
hasAttribute(name, (HasAttributeOptions) null);
}
/**
* Ensures the {@code Locator} points to an element with given attribute. When only the attribute name is provided, the
* method asserts attribute presence.
*
* <p> <strong>Usage</strong>
* <pre>{@code
* assertThat(page.locator("input")).hasAttribute("disabled");
* assertThat(page.locator("input")).not().hasAttribute("open");
* }</pre>
*
* @param name Attribute name.
* @since v1.62
*/
void hasAttribute(String name, HasAttributeOptions options);
/**
* Ensures the {@code Locator} points to an element with given CSS classes. When a string is provided, it must fully match
* the element's {@code class} attribute. To match individual classes use {@link
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<ExpectedTextValue> expectedText = null;
expectImpl("to.have.attribute", expectedText, null, message, commonOptions, "Assert \"hasAttribute\"");
}

@Override
public void hasClass(String text, HasClassOptions options) {
ExpectedTextValue expected = new ExpectedTextValue();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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("<div checked id=node>Text content</div>");
Locator locator = page.locator("#node");
assertThat(locator).hasAttribute("id");
assertThat(locator).hasAttribute("checked");
assertThat(locator).not().hasAttribute("open");
}

@Test
void hasAttributeNameFail() {
page.setContent("<div checked id=node>Text content</div>");
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("<div class=\"foo bar baz\"></div>");
Expand Down