package com.autonomy.abc.matchers;

import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.hamcrest.TypeSafeMatcher;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;

import static org.hamcrest.Matchers.containsString;

public class ElementMatchers {
    public static Matcher<? super WebElement> containsElement(final By by) {
        return new TypeSafeMatcher<WebElement>() {
            @Override
            protected boolean matchesSafely(WebElement webElement) {
                return webElement.findElements(by).size() > 0;
            }

            @Override
            public void describeTo(Description description) {
                description.appendText("a parent element containing a child ").appendValue(by);
            }

            @Override
        public void describeMismatchSafely(final WebElement webElement, final Description description) {
                description.appendText("no child found inside ").appendValue(webElement);
            }
        };
    }

    public static Matcher<? super WebElement> containsText(final String text) {
        return new TypeSafeMatcher<WebElement>() {
            private Matcher<String> container = containsString(text);

            @Override
            protected boolean matchesSafely(WebElement item) {
                return container.matches(item.getText());
            }

            @Override
            public void describeTo(Description description) {
                description.appendText("a tag containing ").appendValue(text);
            }

            @Override
            public void describeMismatchSafely(final WebElement item, final Description description) {
                description.appendText("<" + item.getTagName() + "> tag had text ").appendValue(item.getText());
            }
        };
    }

    public static Matcher<? super WebElement> hasAttribute(final String text) {
        return new TypeSafeMatcher<WebElement>() {

            @Override
            protected boolean matchesSafely(WebElement item) {
                return item.getAttribute(text) != null;
            }

            @Override
            public void describeTo(Description description) {
                description.appendText("a tag with attribute ").appendValue(text);
            }

            @Override
            public void describeMismatchSafely(final WebElement item, final Description description) {
                description.appendText("element was ").appendText(item.toString());
            }
        };
    }
}

@@ -2542,10 +2542,695 @@
 ;%0A    %7D%0A
+%0A    public static Matcher%3C? super WebElement%3E disabled() %7B%0A        return new TypeSafeMatcher%3CWebElement%3E() %7B%0A%0A            @Override%0A            protected boolean matchesSafely(WebElement item) %7B%0A                return item.getAttribute(%22disabled%22) != null;%0A            %7D%0A%0A            @Override%0A            public void describeTo(Description description) %7B%0A                description.appendText(%22an element that is disabled%22);%0A            %7D%0A%0A            @Override%0A            public void describeMismatchSafely(final WebElement item, final Description description) %7B%0A                description.appendText(%22element was %22).appendText(item.toString());%0A            %7D%0A        %7D;%0A    %7D%0A%0A
 %7D%0A

Add disabled Matcher for testing if an element is disabled