Skip to content

Commit

Permalink
add EnabledMatcher
Browse files Browse the repository at this point in the history
  • Loading branch information
vicdev committed Feb 10, 2017
1 parent 3202eca commit 4407430
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 10 deletions.
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();
}
}

10 changes: 7 additions & 3 deletions src/test/java/io/qameta/htmlelements/MatcherTest.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package io.qameta.htmlelements;

import io.qameta.htmlelements.example.element.SuggestItem;
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 org.junit.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;

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 org.hamcrest.Matchers.*;

Expand All @@ -27,11 +27,15 @@ public void testOutput() throws Exception {

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"));

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
@@ -1,17 +1,17 @@
package io.qameta.htmlelements.example.element;

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;

/**
* @author Artem Eroshenko <erosenkoam@me.com>
*/
public interface SearchArrow extends WithSuggest<SearchArrow>, ExtendedWebElement<SearchArrow> {

@Name("form {{ className }}")
@Description("form {{ className }}")
@FindBy(TestData.SEARCH_FORM_TEMPLATE)
SearchForm form(@Param("className") String className);

Expand Down

0 comments on commit 4407430

Please sign in to comment.