package com.pojosontheweb.selenium;

import com.google.common.base.Predicate;
import org.openqa.selenium.WebElement;

public class Findrs {

    /**
         * Create and return a new Predicate that matches an element's attribute value
         * @param attrName the name of the attribute
         * @param expectedValue the expected value of the attribute
         * @return a new Predicate
         */
        public static Predicate<WebElement> attrEquals(final String attrName, final String expectedValue) {
            return new Predicate<WebElement>() {
                @Override
                public boolean apply(WebElement webElement) {
                    String attrVal = webElement.getAttribute(attrName);
                    return attrVal!=null && attrVal.equals(expectedValue);
                }

                @Override
                public String toString() {
                    return "attrEquals(" + attrName + "," + expectedValue + ")";
                }
            };
        }

        /**
         * Create and return a new Predicate that checks for an attribute start
         * @param attrName the name of the attribute
         * @param expectedStartsWith the expected start of the attribute
         * @return a new Predicate
         */
        public static Predicate<WebElement> attrStartsWith(final String attrName, final String expectedStartsWith) {
            return new Predicate<WebElement>() {
                @Override
                public boolean apply(WebElement webElement) {
                    String attrVal = webElement.getAttribute(attrName);
                    return attrVal!=null && attrVal.startsWith(expectedStartsWith);
                }

                @Override
                public String toString() {
                    return "attrStartsWith(" + attrName + "," + expectedStartsWith + ")";
                }
            };
        }

        /**
         * Create and return a new Predicate that checks for an attribute end
         * @param attrName the name of the attribute
         * @param expectedEndsWith the expected start of the attribute
         * @return a new Predicate
         */
        public static Predicate<WebElement> attrEndsWith(final String attrName, final String expectedEndsWith) {
            return new Predicate<WebElement>() {
                @Override
                public boolean apply(WebElement webElement) {
                    String attrVal = webElement.getAttribute(attrName);
                    return attrVal!=null && attrVal.endsWith(expectedEndsWith);
                }

                @Override
                public String toString() {
                    return "attrEndsWith(" + attrName + "," + expectedEndsWith + ")";
                }
            };
        }

        /**
         * Create and return a new Predicate that checks for the presence of a css class
         * on a an element.
         * @param className the expected css class
         * @return a new Predicate
         */
        public static Predicate<WebElement> hasClass(final String className) {
            return new Predicate<WebElement>() {
                @Override
                public boolean apply(WebElement webElement) {
                    String cssClasses = webElement.getAttribute("class");
                    // TODO needs tokenize (substring ain't enough)
                    return cssClasses!=null && cssClasses.contains(className);
                }

                @Override
                public String toString() {
                    return "hasClass(" + className + ")";
                }
            };
        }

        /**
         * Create and return a new Predicate that checks for an element's
         * inner text.
         * @param expected the expected inner text
         * @return a new Predicate
         */
        public static Predicate<WebElement> textEquals(final String expected) {
            return new Predicate<WebElement>() {
                @Override
                public boolean apply(WebElement webElement) {
                    String text = webElement.getText();
                    return text!=null && text.equals(expected);
                }

                @Override
                public String toString() {
                    return "textEquals(" + expected + ")";
                }
            };
        }

        /**
         * Create and return a new Predicate checking that an element's
         * inner text starts with passed text.
         * @param expectedStartsWith the expected start of text
         * @return a new Predicate
         */
        public static Predicate<WebElement> textStartsWith(final String expectedStartsWith) {
            return new Predicate<WebElement>() {
                @Override
                public boolean apply(WebElement input) {
                    String text = input.getText();
                    if (text==null) {
                        return false;
                    }
                    return text.startsWith(expectedStartsWith);
                }

                @Override
                public String toString() {
                    return "textStartsWith(" + expectedStartsWith + ")";

                }
            };
        }

        /**
         * Create and return a new Predicate checking that an element's
         * inner text ends with passed text.
         * @param expectedEndsWith the expected start of text
         * @return a new Predicate
         */
        public static Predicate<WebElement> textEndsWith(final String expectedEndsWith) {
            return new Predicate<WebElement>() {
                @Override
                public boolean apply(WebElement input) {
                    String text = input.getText();
                    if (text==null) {
                        return false;
                    }
                    return text.endsWith(expectedEndsWith);
                }

                @Override
                public String toString() {
                    return "textEndsWith(" + expectedEndsWith + ")";

                }
            };
        }

        /**
         * Create and return a new Predicate that checks if the element is enabled.
         * @return a new Predicate
         */
        public static Predicate<WebElement> isEnabled() {
            return new Predicate<WebElement>() {
                @Override
                public boolean apply(WebElement input) {
                    return input.isEnabled();
                }

                @Override
                public String toString() {
                    return "isEnabled";
                }
            };
        }

        /**
         * Create and return a new Predicate that checks if the element is displayed.
         * @return a new Predicate
         */
        public static Predicate<WebElement> isDisplayed() {
            return new Predicate<WebElement>() {
                @Override
                public boolean apply(WebElement input) {
                    return input.isDisplayed();
                }

                @Override
                public String toString() {
                    return "isDisplayed";
                }
            };
        }

        /**
         * Create and return a new Predicate that checks if the element's text matches passed regexp.
         * @return a new Predicate
         */
        public static Predicate<WebElement> textMatches(final String regexp) {
            return new Predicate<WebElement>() {
                @Override
                public boolean apply(WebElement input) {
                    String text = input.getText();
                    if (text==null) {
                        return false;
                    }
                    return text.matches(regexp);
                }

                @Override
                public String toString() {
                    return "matches(" + regexp + ")";
                }
            };
        }

        /**
         * Create and return a new Predicate that checks for a css value on the element.
         * @param propName the css prop name
         * @param expectedValue the expected css value
         * @return a new Predicate
         */
        public static Predicate<WebElement> cssValue(final String propName, final String expectedValue) {
            return new Predicate<WebElement>() {
                @Override
                public boolean apply(WebElement webElement) {
                    String attrVal = webElement.getCssValue(propName);
                    return attrVal!=null && attrVal.equals(expectedValue);
                }

                @Override
                public String toString() {
                    return "cssValue(" + propName + "," + expectedValue + ")";
                }
            };
        }

        /**
         * Create and return a new Predicate that inverses passed predicate.
         * @param in the predicate to inverse
         * @return a new Predicate
         */
        public static Predicate<WebElement> not(final Predicate<WebElement> in) {
            return new Predicate<WebElement>() {
                @Override
                public boolean apply(WebElement input) {
                    return !in.apply(input);
                }

                @Override
                public String toString() {
                    return "not " + in.toString();
                }
            };
        }

}

@@ -111,16 +111,65 @@
 ement;%0A%0A
+import java.util.Arrays;%0Aimport java.util.List;%0A%0A
 public c
@@ -3370,112 +3370,206 @@
     
-// TODO needs tokenize (substring ain't enough)%0A                    return cssClasses!=null && cssClasse
+if (cssClasses==null) %7B%0A                        return false;%0A                    %7D%0A                    List%3CString%3E tokens = Arrays.asList(cssClasses.split(%22%5C%5Cs%22));%0A                    return token
 s.co

fix #11