Example usage for org.openqa.selenium InvalidSelectorException InvalidSelectorException

List of usage examples for org.openqa.selenium InvalidSelectorException InvalidSelectorException

Introduction

In this page you can find the example usage for org.openqa.selenium InvalidSelectorException InvalidSelectorException.

Prototype

public InvalidSelectorException(String reason) 

Source Link

Usage

From source file:com.opera.core.systems.OperaDriver.java

License:Apache License

/**
 * Find a single element using the selenium atoms.
 *
 * @param by    how to find the element, strings defined in RemoteWebDriver
 * @param using the value to use to find the element
 * @param el    the element to search within
 * @return an element/*  w w  w.  jav a2s . co m*/
 */
protected WebElement findElement(String by, String using, OperaWebElement el) {
    if (using == null) {
        throw new IllegalArgumentException("Cannot find elements when the selector is null");
    }

    using = OperaStrings.escapeJsString(using);

    long start = System.currentTimeMillis();
    boolean isAvailable;
    Integer id;

    String script;
    if (el == null) {
        // Search the document
        script = "return " + OperaAtoms.FIND_ELEMENT + "({\"" + by + "\": \"" + using + "\"})";
    } else {
        // Search within an element
        script = "return " + OperaAtoms.FIND_ELEMENT + "({\"" + by + "\": \"" + using + "\"}, locator)";
    }

    do {
        if (el == null) {
            id = debugger.getObject(script);
        } else {
            id = debugger.executeScriptOnObject(script, el.getObjectId());
        }

        isAvailable = (id != null);

        if (!isAvailable) {
            sleep(OperaIntervals.EXEC_SLEEP.getValue());
        }
    } while (!isAvailable && hasTimeRemaining(start));

    if (isAvailable) {
        String error = debugger.callFunctionOnObject("return (locator instanceof Error) ? locator.message : ''",
                id);
        if (!error.isEmpty()) {
            throw new InvalidSelectorException(error);
        }

        Boolean isStale = Boolean.valueOf(debugger.callFunctionOnObject("locator.parentNode == undefined", id));

        if (isStale) {
            throw new StaleElementReferenceException("This element is no longer part of DOM");
        }

        return new OperaWebElement(this, id);
    } else {
        throw new NoSuchElementException("Cannot find element(s) with " + by);
    }
}

From source file:com.opera.core.systems.OperaDriver.java

License:Apache License

protected List<WebElement> findElements(String by, String using, OperaWebElement el) {
    if (using == null) {
        throw new IllegalArgumentException("Cannot find elements when the selector is null");
    }/*from   w  ww  . j ava 2s  . co m*/

    using = OperaStrings.escapeJsString(using);

    Integer id;

    long start = System.currentTimeMillis();
    int count = 0;

    List<WebElement> elements;

    String script;
    if (el == null) {
        // Search in document
        script = "return " + OperaAtoms.FIND_ELEMENTS + "({\"" + by + "\": \"" + using + "\"})";
    } else {
        // Search within an element
        script = "return " + OperaAtoms.FIND_ELEMENTS + "({\"" + by + "\": \"" + using + "\"}, locator)";
    }

    do {
        if (el == null) {
            id = debugger.getObject(script);
        } else {
            id = debugger.executeScriptOnObject(script, el.getObjectId());
        }

        String error = debugger.callFunctionOnObject("return (locator instanceof Error) ? locator.message : ''",
                id);
        if (!error.isEmpty()) {
            throw new InvalidSelectorException(error);
        }

        elements = processElements(id);

        if (elements != null) {
            count = elements.size();
        }

        if (count == 0 && hasTimeRemaining(start)) {
            sleep(OperaIntervals.EXEC_SLEEP.getValue());
        } else {
            break;
        }

    } while (true);

    if (id != null) {
        return elements;
    } else {
        throw new NoSuchElementException("Cannot find element(s) with " + by);
    }
}

From source file:io.github.seleniumquery.by.csstree.condition.pseudoclass.SQCssFunctionalIndexArgumentPseudoClassCondition.java

License:Apache License

protected int getArgumentAsIndex() {
    String eqPseudoClassArgument = getArgument();
    Matcher m = INDEX_REGEX.matcher(eqPseudoClassArgument);
    boolean isArgumentAnInteger = m.find();
    if (!isArgumentAnInteger) {
        String reason = String.format("The :%s() pseudo-class requires an integer as argument but got: \"%s\".",
                getPseudoClassName(), eqPseudoClassArgument);
        throw new InvalidSelectorException(reason);
    }//  www.  j a  v  a 2  s .c  o  m
    String integerIndex = m.group(1);
    if (integerIndex.startsWith("+")) {
        integerIndex = integerIndex.substring(1);
    }
    return Integer.valueOf(integerIndex);
}

From source file:io.github.seleniumquery.by.secondgen.csstree.condition.pseudoclass.childfilter.NthArgument.java

License:Apache License

private InvalidSelectorException createInvalidArgumentException(String argument) {
    String reason = String.format("The :nth-child() pseudo-class must have an argument like"
            + " :nth-child(odd), :nth-child(even), :nth-child(an+b), :nth-child(an) or"
            + " :nth-child(b) - where a and b are positive or negative integers -, but was :nth-child(\"%s\").",
            argument);// w  w w  . j  a v  a  2 s  .  c  o m
    return new InvalidSelectorException(reason);
}

From source file:org.seleniumhq.selenium.fluent.FluentBy.java

License:Apache License

/**
 * Finds elements based on the value of the "class" attribute.
 * If the second param is set to true, if an element has many classes then
 * this will match against each of them. For example if the value is "one two onone",
 * then the following "className"s will match: "one" and "two"
 *
 * @param className The value of the "class" attribute to search for
 * @return a By which locates elements by the value of the "class" attribute.
 *//*from   w  w w .j  a  v a  2  s  .c o  m*/
public static ByStrictClassName strictClassName(final String className) {
    if (className == null)
        throw new IllegalArgumentException("Cannot find elements when the class name expression is null.");

    if (className.matches(".*\\s+.*")) {
        throw new InvalidSelectorException(
                "Compound class names are not supported. Consider searching for one class name and filtering the results.");
    }

    return new ByStrictClassName(className);
}

From source file:org.uiautomation.ios.server.application.ServerSideL10NDecorator.java

License:Apache License

private void decorateContent(Criteria c) {
    if (!isElligibleForContent(c)) {
        return;/*from   w  w w .  ja va2s .  co m*/
    }
    PropertyEqualCriteria criteria = (PropertyEqualCriteria) c;
    String oldValue = criteria.getValue();
    LanguageDictionary dict = app.getCurrentDictionary();
    String newValue = dict.getContentForKey(oldValue);
    if (newValue == null) {
        throw new InvalidSelectorException(
                "No entry for key " + oldValue + " in dictionary for " + app.getCurrentLanguage());
    }

    criteria.setValue(LanguageDictionary.getRegexPattern(newValue));
    criteria.setL10nstrategy(L10NStrategy.none);
    //criteria.setMatchingStrategy(MatchingStrategy.regex);
}

From source file:org.uiautomation.ios.server.application.ServerSideL10NDecorator.java

License:Apache License

private void decorateMatching(Criteria c) {
    if (!isElligibleForMatching(c)) {
        return;/*from www. j a  va  2s  .  c  om*/
    }
    PropertyEqualCriteria criteria = (PropertyEqualCriteria) c;
    String oldValue = criteria.getValue();

    String newValue = oldValue;
    switch (criteria.getMatchingStrategy()) {
    case starts:
        newValue = oldValue + "(.*)";
        break;
    case contains:
        newValue = "(.*)" + oldValue + "(.*)";
        break;
    case ends:
        newValue = "(.*)" + oldValue;
        break;
    default:
        throw new InvalidSelectorException("Can't find strategy");
    }
    criteria.setValue(newValue);
    criteria.setMatchingStrategy(MatchingStrategy.regex);

}

From source file:org.uiautomation.ios.UIAModels.predicate.AbstractCriteria.java

License:Apache License

@SuppressWarnings("unchecked")
public static <T extends Criteria> T parse(JSONObject serialized, CriteriaDecorator decorator) {
    try {/*from w  w  w . j  a va  2 s .  c  om*/
        int nbKeys = serialized.length();
        switch (nbKeys) {
        case KEYS_IN_EMPTY_CRITERIA:
            return (T) new EmptyCriteria();
        case KEYS_IN_COMPOSED_CRITERIA:
            String key = (String) serialized.keys().next();
            CompositionType type = CompositionType.valueOf(key);
            return (T) buildComposedCriteria(serialized, type, decorator);
        case KEYS_IN_LOCATION_CRITERIA:
            int x = serialized.getInt("x");
            int y = serialized.getInt("y");
            return (T) buildLocationCriteria(serialized, x, y, decorator);
        case KEYS_IN_PROPERTY_CRITERIA:
            String method = serialized.getString("method");
            String tmp = method.substring(0, 1).toUpperCase() + method.toLowerCase().substring(1) + "Criteria";
            String clazz = AbstractCriteria.class.getPackage().getName() + "." + tmp;
            Class<? extends PropertyEqualCriteria> c = (Class<? extends PropertyEqualCriteria>) Class
                    .forName(clazz);
            return (T) buildPropertyBaseCriteria(serialized, c, decorator);
        default:
            throw new InvalidSelectorException("can't find the type : " + serialized.toString());
        }
    } catch (Exception e) {
        throw new WebDriverException(e);
    }

}

From source file:org.uiautomation.ios.UIAModels.predicate.AbstractCriteria.java

License:Apache License

private static ComposedCriteria buildComposedCriteria(JSONObject serialized, CompositionType type,
        CriteriaDecorator decorator) throws Exception {
    JSONArray array = serialized.getJSONArray(type.toString());
    if (type == CompositionType.NOT && array.length() != 1) {
        throw new InvalidSelectorException("negative criteria apply to 1 criteria only " + serialized);
    }//w w  w .j av a  2  s. co  m
    List<Criteria> criterias = new ArrayList<Criteria>();

    for (int i = 0; i < array.length(); i++) {
        JSONObject c = array.getJSONObject(i);
        Criteria crit = parse(c, decorator);
        criterias.add(crit);
    }

    Object[] args = new Object[] { criterias };
    Class<?>[] argsClass = new Class[] { List.class };

    Constructor<?> c = type.getAssociatedClass().getConstructor(argsClass);
    ComposedCriteria crit = (ComposedCriteria) c.newInstance(args);
    crit.addDecorator(decorator);
    crit.decorate();
    return crit;
}