Skip to content
This repository was archived by the owner on Mar 27, 2026. It is now read-only.
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
39 changes: 39 additions & 0 deletions src/main/java/io/qameta/htmlelements/matcher/EnabledMatcher.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package io.qameta.htmlelements.matcher;

import org.hamcrest.Description;
import org.hamcrest.Factory;
import org.hamcrest.Matcher;
import org.hamcrest.TypeSafeMatcher;
import org.openqa.selenium.WebDriverException;
import org.openqa.selenium.WebElement;

public class EnabledMatcher extends TypeSafeMatcher<WebElement> {

private EnabledMatcher() {
}

@Override
protected boolean matchesSafely(WebElement element) {
try {
return element.isEnabled();
} catch (WebDriverException e) {
return false;
}
}

@Override
public void describeTo(Description description) {
description.appendText("element is enabled");
}

@Override
public void describeMismatchSafely(WebElement element, Description mismatchDescription) {
mismatchDescription.appendText("element ").appendValue(element).appendText(" is not enabled");
}

@Factory
public static Matcher<WebElement> enabled() {
return new EnabledMatcher();
}
}

12 changes: 8 additions & 4 deletions src/test/java/io/qameta/htmlelements/MatcherTest.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
package io.qameta.htmlelements;

import io.qameta.htmlelements.example.element.SuggestItem;
import io.qameta.htmlelements.statement.Listener;
import org.openqa.selenium.WebDriver;
import io.qameta.htmlelements.example.TestData;
import io.qameta.htmlelements.example.element.SuggestItem;
import io.qameta.htmlelements.example.page.SearchPage;
import io.qameta.htmlelements.statement.Listener;
import org.junit.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;

import java.lang.reflect.Method;

import static io.qameta.htmlelements.matcher.DisplayedMatcher.displayed;
import static io.qameta.htmlelements.matcher.EnabledMatcher.enabled;
import static io.qameta.htmlelements.matcher.HasTextMatcher.hasText;
import static java.lang.String.format;
import static org.hamcrest.Matchers.*;
Expand All @@ -38,11 +38,15 @@ public void beforeMethodCall(String description, Method method, Object[] args) {

searchPage.searchArrow().suggest()
.filter(WebElement::isDisplayed)
.filter(WebElement::isEnabled)
.should(hasSize(2));

searchPage.searchArrow()
.should(displayed());

searchPage.searchArrow()
.should(enabled());

searchPage.searchArrow()
.waitUntil("", hasText("search-arrow"), 10);

Expand Down
12 changes: 8 additions & 4 deletions src/test/java/io/qameta/htmlelements/example/TestData.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
package io.qameta.htmlelements.example;

import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;

import java.util.Arrays;
import java.util.List;
import java.util.NoSuchElementException;

import static io.qameta.htmlelements.example.TestData.WebElementBuilder.mockWebElement;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

Expand Down Expand Up @@ -45,11 +41,13 @@ public static WebDriver mockDriver() {
.withChildElement(SUGGEST_ITEM_XPATH, mockWebElement().withText("first suggest item").build())
.withText("first suggest")
.withDisplayed(false, true)
.withEnabled(false, true)
.build(),
mockWebElement()
.withChildElement(SUGGEST_ITEM_XPATH, mockWebElement().withText("second suggest item").build())
.withText("second suggest")
.withDisplayed(false, false, false, true)
.withEnabled(false, false, false, true)
.build()
);

Expand All @@ -63,6 +61,7 @@ public static WebDriver mockDriver() {
.withChildElement(SEARCH_FORM_XPATH, searchForm)
.withText("search-arro", "search", "search-arrow")
.withDisplayed(true)
.withEnabled(true)
.build();


Expand Down Expand Up @@ -102,6 +101,11 @@ public WebElementBuilder withDisplayed(boolean displayed, Boolean... other) {
return this;
}

public WebElementBuilder withEnabled(boolean enabled, Boolean... other) {
when(getElement().isEnabled()).thenReturn(enabled, other);
return this;
}

public WebElementBuilder withChildElement(String xpath, WebElement element) {
when(getElement().findElement(By.xpath(xpath))).thenReturn(element);
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@

import io.qameta.htmlelements.annotation.Description;
import io.qameta.htmlelements.annotation.FindBy;
import io.qameta.htmlelements.annotation.Name;
import io.qameta.htmlelements.annotation.Param;
import io.qameta.htmlelements.element.*;
import io.qameta.htmlelements.element.ExtendedWebElement;
import io.qameta.htmlelements.example.TestData;
import io.qameta.htmlelements.extension.DescriptionProvider;

/**
* @author Artem Eroshenko <erosenkoam@me.com>
Expand Down