Java Code Examples for com.gargoylesoftware.htmlunit.WebAssert#notNull()

The following examples show how to use com.gargoylesoftware.htmlunit.WebAssert#notNull() . 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 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: 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 4
Source File: DomNode.java    From htmlunit 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 5
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 6
Source File: HtmlPage.java    From htmlunit 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 7
Source File: HtmlSelect.java    From htmlunit 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 8
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;
}
 
Example 9
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 10
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 11
Source File: SimpleScriptable.java    From htmlunit 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: HTMLFormElement.java    From htmlunit 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)
            && !FormEncodingType.URL_ENCODED.getName().equals(enctype)
            && !FormEncodingType.MULTIPART.getName().equals(enctype)
            && !FormEncodingType.TEXT_PLAIN.getName().equals(enctype)) {
        throw Context.reportRuntimeError("Cannot set the encoding property to invalid value: '" + enctype + "'");
    }
    getHtmlForm().setEnctypeAttribute(enctype);
}
 
Example 13
Source File: HtmlPage.java    From htmlunit 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 14
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 15
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 16
Source File: HTMLFormElement.java    From htmlunit with Apache License 2.0 4 votes vote down vote up
/**
 * Sets the value of the property {@code target}.
 * @param target the new value
 */
@JsxSetter
public void setTarget(final String target) {
    WebAssert.notNull("target", target);
    getHtmlForm().setTargetAttribute(target);
}
 
Example 17
Source File: HTMLOptionsCollection.java    From htmlunit with Apache License 2.0 4 votes vote down vote up
/**
 * Initializes this object.
 * @param select the HtmlSelect that this object will retrieve elements from
 */
public void initialize(final HtmlSelect select) {
    WebAssert.notNull("select", select);
    htmlSelect_ = select;
}
 
Example 18
Source File: HtmlElement.java    From htmlunit with Apache License 2.0 3 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 this HtmlElement, as well as descendant elements.
 *
 * @param listener the attribute change listener to be removed
 * @see #addHtmlAttributeChangeListener(HtmlAttributeChangeListener)
 */
public void removeHtmlAttributeChangeListener(final HtmlAttributeChangeListener listener) {
    WebAssert.notNull("listener", listener);
    synchronized (attributeListeners_) {
        attributeListeners_.remove(listener);
    }
}
 
Example 19
Source File: UrlUtils.java    From HtmlUnit-Android with Apache License 2.0 2 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);
    return URL_CREATOR.toUrlUnsafeClassic(url);
}
 
Example 20
Source File: HtmlIsIndex.java    From HtmlUnit-Android with Apache License 2.0 2 votes vote down vote up
/**
 * Sets the value that will be returned during submission of a form.
 *
 * @param newValue the value
 */
public void setValue(final String newValue) {
    WebAssert.notNull("newValue", newValue);
    value_ = newValue;
}