com.gargoylesoftware.htmlunit.WebAssert Java Examples

The following examples show how to use com.gargoylesoftware.htmlunit.WebAssert. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: JavaScriptEngine.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
/**
 * Performs initialization for the given webWindow.
 * @param webWindow the web window to initialize for
 */
@Override
public void initialize(final WebWindow webWindow) {
    WebAssert.notNull("webWindow", webWindow);

    getContextFactory().call(cx -> {
        try {
            init(webWindow, cx);
        }
        catch (final Exception e) {
            LOG.error("Exception while initializing JavaScript for the page", e);
            throw new ScriptException(null, e); // BUG: null is not useful.
        }
        return null;
    });
}
 
Example #2
Source File: HtmlElement.java    From HtmlUnit-Android with Apache License 2.0 6 votes vote down vote up
/**
 * Searches for an element based on the specified criteria, returning the first element which matches
 * said criteria. Only elements which are descendants of this element are included in the search.
 *
 * @param elementName the name of the element to search for
 * @param attributeName the name of the attribute to search for
 * @param attributeValue the value of the attribute to search for
 * @param <E> the sub-element type
 * @return the first element which matches the specified search criteria
 * @throws ElementNotFoundException if no element matches the specified search criteria
 */
public final <E extends HtmlElement> E getOneHtmlElementByAttribute(final String elementName,
        final String attributeName,
    final String attributeValue) throws ElementNotFoundException {

    WebAssert.notNull("elementName", elementName);
    WebAssert.notNull("attributeName", attributeName);
    WebAssert.notNull("attributeValue", attributeValue);

    final List<E> list = getElementsByAttribute(elementName, attributeName, attributeValue);

    if (list.isEmpty()) {
        throw new ElementNotFoundException(elementName, attributeName, attributeValue);
    }

    return list.get(0);
}
 
Example #3
Source File: JavaScriptEngine.java    From HtmlUnit-Android with Apache License 2.0 6 votes vote down vote up
/**
 * Performs initialization for the given webWindow.
 * @param webWindow the web window to initialize for
 */
@Override
public void initialize(final WebWindow webWindow) {
    WebAssert.notNull("webWindow", webWindow);

    final ContextAction action = new ContextAction() {
        @Override
        public Object run(final Context cx) {
            try {
                init(webWindow, cx);
            }
            catch (final Exception e) {
                LOG.error("Exception while initializing JavaScript for the page", e);
                throw new ScriptException(null, e); // BUG: null is not useful.
            }

            return null;
        }
    };

    getContextFactory().call(action);
}
 
Example #4
Source File: CSSStyleDeclaration.java    From HtmlUnit-Android with Apache License 2.0 6 votes vote down vote up
/**
 * Initializes the object.
 * @param htmlElement the element that this style describes
 */
private void initialize(final Element element) {
    // Initialize.
    WebAssert.notNull("htmlElement", element);
    jsElement_ = element;
    setDomNode(element.getDomNodeOrNull(), false);

    // If an IE behavior was specified in the style, apply the behavior.
    if (getBrowserVersion().hasFeature(CSS_SUPPORTS_BEHAVIOR_PROPERTY)
        && element instanceof HTMLElement) {
        final HTMLElement htmlElement = (HTMLElement) element;
        final String behavior = getStyleAttribute(BEHAVIOR);
        if (StringUtils.isNotBlank(behavior)) {
            try {
                final Object[] url = URL_FORMAT.parse(behavior);
                if (url.length > 0) {
                    htmlElement.addBehavior((String) url[0]);
                }
            }
            catch (final ParseException e) {
                LOG.warn("Invalid behavior: '" + behavior + "'.");
            }
        }
    }
}
 
Example #5
Source File: HtmlElement.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
/**
 * Searches for an element based on the specified criteria, returning the first element which matches
 * said criteria. Only elements which are descendants of this element are included in the search.
 *
 * @param elementName the name of the element to search for
 * @param attributeName the name of the attribute to search for
 * @param attributeValue the value of the attribute to search for
 * @param <E> the sub-element type
 * @return the first element which matches the specified search criteria
 * @throws ElementNotFoundException if no element matches the specified search criteria
 */
public final <E extends HtmlElement> E getOneHtmlElementByAttribute(final String elementName,
        final String attributeName,
    final String attributeValue) throws ElementNotFoundException {

    WebAssert.notNull("elementName", elementName);
    WebAssert.notNull("attributeName", attributeName);
    WebAssert.notNull("attributeValue", attributeValue);

    final List<E> list = getElementsByAttribute(elementName, attributeName, attributeValue);

    if (list.isEmpty()) {
        throw new ElementNotFoundException(elementName, attributeName, attributeValue);
    }

    return list.get(0);
}
 
Example #6
Source File: DomNamespaceNode.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
/**
 * Creates an instance of a DOM node that can have a namespace.
 *
 * @param namespaceURI the URI that identifies an XML namespace
 * @param qualifiedName the qualified name of the element type to instantiate
 * @param page the page that contains this element
 */
protected DomNamespaceNode(final String namespaceURI, final String qualifiedName, final SgmlPage page) {
    super(page);
    WebAssert.notNull("qualifiedName", qualifiedName);
    qualifiedName_ = qualifiedName;

    if (qualifiedName.indexOf(':') != -1) {
        namespaceURI_ = namespaceURI;
        final int colonPosition = qualifiedName_.indexOf(':');
        localName_ = qualifiedName_.substring(colonPosition + 1);
        prefix_ = qualifiedName_.substring(0, colonPosition);
    }
    else {
        namespaceURI_ = namespaceURI;
        localName_ = qualifiedName_;
        prefix_ = null;
    }

    localNameLC_ = localName_.toLowerCase(Locale.ROOT);
}
 
Example #7
Source File: DomNamespaceNode.java    From HtmlUnit-Android with Apache License 2.0 6 votes vote down vote up
/**
 * Creates an instance of a DOM node that can have a namespace.
 *
 * @param namespaceURI the URI that identifies an XML namespace
 * @param qualifiedName the qualified name of the element type to instantiate
 * @param page the page that contains this element
 */
protected DomNamespaceNode(final String namespaceURI, final String qualifiedName, final SgmlPage page) {
    super(page);
    WebAssert.notNull("qualifiedName", qualifiedName);
    qualifiedName_ = qualifiedName;

    if (qualifiedName.indexOf(':') != -1) {
        namespaceURI_ = namespaceURI;
        final int colonPosition = qualifiedName_.indexOf(':');
        localName_ = qualifiedName_.substring(colonPosition + 1);
        prefix_ = qualifiedName_.substring(0, colonPosition);
    }
    else {
        namespaceURI_ = namespaceURI;
        localName_ = qualifiedName_;
        prefix_ = null;
    }

    localNameLC_ = localName_.toLowerCase(Locale.ROOT);
}
 
Example #8
Source File: HtmlForm.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the first checked radio button with the specified name. If none of
 * the radio buttons by that name are checked, this method returns {@code null}.
 *
 * @param name the name of the radio button
 * @return the first checked radio button with the specified name
 */
public HtmlRadioButtonInput getCheckedRadioButton(final String name) {
    WebAssert.notNull("name", name);

    for (final HtmlRadioButtonInput input : getRadioButtonsByName(name)) {
        if (input.isChecked()) {
            return input;
        }
    }
    return null;
}
 
Example #9
Source File: HtmlSelect.java    From HtmlUnit-Android with Apache License 2.0 5 votes vote down vote up
private HtmlOption getOptionByValueStrict(final String value) throws ElementNotFoundException {
    WebAssert.notNull("value", value);
    for (final HtmlOption option : getOptions()) {
        if (option.getAttributeDirect("value").equals(value)) {
            return option;
        }
    }
    throw new ElementNotFoundException("option", "value", value);
}
 
Example #10
Source File: HtmlForm.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * Returns all the {@link HtmlRadioButtonInput} elements in this form that have the specified name.
 *
 * @param name the name to search for
 * @return all the {@link HtmlRadioButtonInput} elements in this form that have the specified name
 */
public List<HtmlRadioButtonInput> getRadioButtonsByName(final String name) {
    WebAssert.notNull("name", name);

    final List<HtmlRadioButtonInput> results = new ArrayList<>();

    for (final HtmlElement element : getInputsByName(name)) {
        if (element instanceof HtmlRadioButtonInput) {
            results.add((HtmlRadioButtonInput) element);
        }
    }

    return results;
}
 
Example #11
Source File: SimpleScriptable.java    From HtmlUnit-Android with Apache License 2.0 5 votes vote down vote up
/**
 * Sets the DOM node that corresponds to this JavaScript object.
 * @param domNode the DOM node
 * @param assignScriptObject If true, call <code>setScriptObject</code> on domNode
 */
protected void setDomNode(final DomNode domNode, final boolean assignScriptObject) {
    WebAssert.notNull("domNode", domNode);
    domNode_ = domNode;
    if (assignScriptObject) {
        domNode_.setScriptableObject(this);
    }
}
 
Example #12
Source File: HtmlPage.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * Removes an HtmlAttributeChangeListener from the listener list.
 * This method should be used to remove HtmlAttributeChangeListener that were registered
 * for all attributes of all HtmlElements contained in this page.
 *
 * @param listener the attribute change listener to be removed
 * @see #addHtmlAttributeChangeListener(HtmlAttributeChangeListener)
 */
public void removeHtmlAttributeChangeListener(final HtmlAttributeChangeListener listener) {
    WebAssert.notNull("listener", listener);
    synchronized (lock_) {
        if (attributeListeners_ != null) {
            attributeListeners_.remove(listener);
        }
    }
}
 
Example #13
Source File: HtmlUnitNekoHtmlParser.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
HtmlUnitNekoHTMLErrorHandler(final HTMLParserListener listener, final URL url, final String htmlContent) {
    WebAssert.notNull("listener", listener);
    WebAssert.notNull("url", url);
    listener_ = listener;
    url_ = url;
    html_ = htmlContent;
}
 
Example #14
Source File: Window.java    From HtmlUnit-Android with Apache License 2.0 5 votes vote down vote up
/**
 * Initialize the object.
 * @param enclosedPage the page containing the JavaScript
 */
public void initialize(final Page enclosedPage) {
    if (enclosedPage != null && enclosedPage.isHtmlPage()) {
        final HtmlPage htmlPage = (HtmlPage) enclosedPage;

        // Windows don't have corresponding DomNodes so set the domNode
        // variable to be the page. If this isn't set then SimpleScriptable.get()
        // won't work properly
        setDomNode(htmlPage);
        clearEventListenersContainer();

        WebAssert.notNull("document_", document_);
        document_.setDomNode(htmlPage);
    }
}
 
Example #15
Source File: HTMLFormElement.java    From HtmlUnit-Android with Apache License 2.0 5 votes vote down vote up
/**
 * Sets the value of the property {@code enctype}.
 * @param enctype the new value
 */
@JsxSetter
public void setEnctype(final String enctype) {
    WebAssert.notNull("encoding", enctype);
    if (getBrowserVersion().hasFeature(JS_FORM_REJECT_INVALID_ENCODING)) {
        if (!FormEncodingType.URL_ENCODED.getName().equals(enctype)
                && !FormEncodingType.MULTIPART.getName().equals(enctype)) {
            throw Context.reportRuntimeError("Cannot set the encoding property to invalid value: '"
                    + enctype + "'");
        }
    }
    getHtmlForm().setEnctypeAttribute(enctype);
}
 
Example #16
Source File: UrlUtils.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * <p>Constructs a URL instance based on the specified URL string, taking into account the fact that the
 * specified URL string may represent an <tt>"about:..."</tt> URL, a <tt>"javascript:..."</tt> URL, or
 * a <tt>data:...</tt> URL.</p>
 *
 * <p>Unlike {@link #toUrlSafe(String)}, the caller need not be sure that URL strings passed to this
 * method will parse correctly as URLs.</p>
 *
 * @param url the URL string to convert into a URL instance
 * @return the constructed URL instance
 * @throws MalformedURLException if the URL string cannot be converted to a URL instance
 */
public static URL toUrlUnsafe(final String url) throws MalformedURLException {
    WebAssert.notNull("url", url);

    final String protocol = org.apache.commons.lang3.StringUtils.substringBefore(url, ":").toLowerCase(Locale.ROOT);

    if (protocol.isEmpty() || UrlUtils.isNormalUrlProtocol(protocol)) {
        final URL response = new URL(url);
        if (response.getProtocol().startsWith("http")
                && org.apache.commons.lang3.StringUtils.isEmpty(response.getHost())) {
            throw new MalformedURLException("Missing host name in url: " + url);
        }
        return response;
    }

    if (JavaScriptURLConnection.JAVASCRIPT_PREFIX.equals(protocol + ":")) {
        return new URL(null, url, JS_HANDLER);
    }

    if ("about".equals(protocol)) {
        if (WebClient.URL_ABOUT_BLANK != null
                && org.apache.commons.lang3.StringUtils.
                    equalsIgnoreCase(WebClient.URL_ABOUT_BLANK.toExternalForm(), url)) {
            return WebClient.URL_ABOUT_BLANK;
        }
        return new URL(null, url, ABOUT_HANDLER);
    }

    if ("data".equals(protocol)) {
        return new URL(null, url, DATA_HANDLER);
    }

    return new URL(null, url, AnyHandler.INSTANCE);
}
 
Example #17
Source File: HtmlSelect.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the {@link HtmlOption} object that corresponds to the specified value.
 *
 * @param value the value to search by
 * @return the {@link HtmlOption} object that corresponds to the specified value
 * @exception ElementNotFoundException If a particular element could not be found in the DOM model
 */
public HtmlOption getOptionByValue(final String value) throws ElementNotFoundException {
    WebAssert.notNull("value", value);
    for (final HtmlOption option : getOptions()) {
        if (option.getValueAttribute().equals(value)) {
            return option;
        }
    }
    throw new ElementNotFoundException("option", "value", value);
}
 
Example #18
Source File: HtmlSelect.java    From HtmlUnit-Android with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the {@link HtmlOption} object that corresponds to the specified value.
 *
 * @param value the value to search by
 * @return the {@link HtmlOption} object that corresponds to the specified value
 * @exception ElementNotFoundException If a particular element could not be found in the DOM model
 */
public HtmlOption getOptionByValue(final String value) throws ElementNotFoundException {
    WebAssert.notNull("value", value);
    for (final HtmlOption option : getOptions()) {
        if (option.getValueAttribute().equals(value)) {
            return option;
        }
    }
    throw new ElementNotFoundException("option", "value", value);
}
 
Example #19
Source File: CSSStyleDeclaration.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * Initializes the object.
 * @param htmlElement the element that this style describes
 */
private void initialize(final Element element) {
    // Initialize.
    WebAssert.notNull("htmlElement", element);
    jsElement_ = element;
    setDomNode(element.getDomNodeOrNull(), false);

    // If an IE behavior was specified in the style, apply the behavior.
    if (getBrowserVersion().hasFeature(CSS_SUPPORTS_BEHAVIOR_PROPERTY)
        && element instanceof HTMLElement) {
        final HTMLElement htmlElement = (HTMLElement) element;
        final String behavior = getStyleAttribute(BEHAVIOR);
        if (StringUtils.isNotBlank(behavior)) {
            try {
                final Object[] url;
                synchronized (URL_FORMAT) {
                    url = URL_FORMAT.parse(behavior);
                }
                if (url.length > 0) {
                    htmlElement.addBehavior((String) url[0]);
                }
            }
            catch (final ParseException e) {
                if (LOG.isWarnEnabled()) {
                    LOG.warn("Invalid behavior: '" + behavior + "'.");
                }
            }
        }
    }
}
 
Example #20
Source File: HtmlSelect.java    From HtmlUnit-Android with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the {@link HtmlOption} object that has the specified text.
 *
 * @param text the text to search by
 * @return the {@link HtmlOption} object that has the specified text
 * @exception ElementNotFoundException If a particular element could not be found in the DOM model
 */
public HtmlOption getOptionByText(final String text) throws ElementNotFoundException {
    WebAssert.notNull("text", text);
    for (final HtmlOption option : getOptions()) {
        if (option.getText().equals(text)) {
            return option;
        }
    }
    throw new ElementNotFoundException("option", "text", text);
}
 
Example #21
Source File: DomNode.java    From HtmlUnit-Android with Apache License 2.0 5 votes vote down vote up
/**
 * Adds a {@link DomChangeListener} to the listener list. The listener is registered for
 * all descendants of this node.
 *
 * @param listener the DOM structure change listener to be added
 * @see #removeDomChangeListener(DomChangeListener)
 */
public void addDomChangeListener(final DomChangeListener listener) {
    WebAssert.notNull("listener", listener);

    synchronized (listeners_lock_) {
        if (domListeners_ == null) {
            domListeners_ = new LinkedHashSet<>();
        }
        domListeners_.add(listener);
        domListenersList_ = null;
    }
}
 
Example #22
Source File: DomNode.java    From HtmlUnit-Android with Apache License 2.0 5 votes vote down vote up
/**
 * Removes a {@link DomChangeListener} from the listener list. The listener is deregistered for
 * all descendants of this node.
 *
 * @param listener the DOM structure change listener to be removed
 * @see #addDomChangeListener(DomChangeListener)
 */
public void removeDomChangeListener(final DomChangeListener listener) {
    WebAssert.notNull("listener", listener);

    synchronized (listeners_lock_) {
        if (domListeners_ != null) {
            domListeners_.remove(listener);
            domListenersList_ = null;
        }
    }
}
 
Example #23
Source File: DomNode.java    From HtmlUnit-Android with Apache License 2.0 5 votes vote down vote up
/**
 * Adds a {@link CharacterDataChangeListener} to the listener list. The listener is registered for
 * all descendants of this node.
 *
 * @param listener the character data change listener to be added
 * @see #removeCharacterDataChangeListener(CharacterDataChangeListener)
 */
public void addCharacterDataChangeListener(final CharacterDataChangeListener listener) {
    WebAssert.notNull("listener", listener);

    synchronized (listeners_lock_) {
        if (characterDataListeners_ == null) {
            characterDataListeners_ = new LinkedHashSet<>();
        }
        characterDataListeners_.add(listener);
        characterDataListenersList_ = null;
    }
}
 
Example #24
Source File: DomNode.java    From HtmlUnit-Android with Apache License 2.0 5 votes vote down vote up
/**
 * Removes a {@link CharacterDataChangeListener} from the listener list. The listener is deregistered for
 * all descendants of this node.
 *
 * @param listener the Character Data change listener to be removed
 * @see #addCharacterDataChangeListener(CharacterDataChangeListener)
 */
public void removeCharacterDataChangeListener(final CharacterDataChangeListener listener) {
    WebAssert.notNull("listener", listener);

    synchronized (listeners_lock_) {
        if (characterDataListeners_ != null) {
            characterDataListeners_.remove(listener);
            characterDataListenersList_ = null;
        }
    }
}
 
Example #25
Source File: HtmlPage.java    From HtmlUnit-Android with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the first anchor with the specified text.
 * @param text the text to search for
 * @return the first anchor that was found
 * @throws ElementNotFoundException if no anchors are found with the specified text
 */
public HtmlAnchor getAnchorByText(final String text) throws ElementNotFoundException {
    WebAssert.notNull("text", text);

    for (final HtmlAnchor anchor : getAnchors()) {
        if (text.equals(anchor.asText())) {
            return anchor;
        }
    }
    throw new ElementNotFoundException("a", "<text>", text);
}
 
Example #26
Source File: HtmlPage.java    From HtmlUnit-Android with Apache License 2.0 5 votes vote down vote up
/**
 * Adds an HtmlAttributeChangeListener to the listener list.
 * The listener is registered for all attributes of all HtmlElements contained in this page.
 *
 * @param listener the attribute change listener to be added
 * @see #removeHtmlAttributeChangeListener(HtmlAttributeChangeListener)
 */
public void addHtmlAttributeChangeListener(final HtmlAttributeChangeListener listener) {
    WebAssert.notNull("listener", listener);
    synchronized (lock_) {
        if (attributeListeners_ == null) {
            attributeListeners_ = new LinkedHashSet<>();
        }
        attributeListeners_.add(listener);
    }
}
 
Example #27
Source File: HtmlPage.java    From HtmlUnit-Android with Apache License 2.0 5 votes vote down vote up
/**
 * Removes an HtmlAttributeChangeListener from the listener list.
 * This method should be used to remove HtmlAttributeChangeListener that were registered
 * for all attributes of all HtmlElements contained in this page.
 *
 * @param listener the attribute change listener to be removed
 * @see #addHtmlAttributeChangeListener(HtmlAttributeChangeListener)
 */
public void removeHtmlAttributeChangeListener(final HtmlAttributeChangeListener listener) {
    WebAssert.notNull("listener", listener);
    synchronized (lock_) {
        if (attributeListeners_ != null) {
            attributeListeners_.remove(listener);
        }
    }
}
 
Example #28
Source File: HTMLParser.java    From HtmlUnit-Android with Apache License 2.0 5 votes vote down vote up
HTMLErrorHandler(final HTMLParserListener listener, final URL url, final String htmlContent) {
    WebAssert.notNull("listener", listener);
    WebAssert.notNull("url", url);
    listener_ = listener;
    url_ = url;
    html_ = htmlContent;
}
 
Example #29
Source File: HtmlForm.java    From HtmlUnit-Android with Apache License 2.0 5 votes vote down vote up
/**
 * Returns all the {@link HtmlRadioButtonInput} elements in this form that have the specified name.
 *
 * @param name the name to search for
 * @return all the {@link HtmlRadioButtonInput} elements in this form that have the specified name
 */
public List<HtmlRadioButtonInput> getRadioButtonsByName(final String name) {
    WebAssert.notNull("name", name);

    final List<HtmlRadioButtonInput> results = new ArrayList<>();

    for (final HtmlElement element : getInputsByName(name)) {
        if (element instanceof HtmlRadioButtonInput) {
            results.add((HtmlRadioButtonInput) element);
        }
    }

    return results;
}
 
Example #30
Source File: HtmlForm.java    From HtmlUnit-Android with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the first checked radio button with the specified name. If none of
 * the radio buttons by that name are checked, this method returns {@code null}.
 *
 * @param name the name of the radio button
 * @return the first checked radio button with the specified name
 */
public HtmlRadioButtonInput getCheckedRadioButton(final String name) {
    WebAssert.notNull("name", name);

    for (final HtmlRadioButtonInput input : getRadioButtonsByName(name)) {
        if (input.isChecked()) {
            return input;
        }
    }
    return null;
}