Skip to content

[Regression] HtmlForm#getInputByValue and HtmlForm#getInputsByValue no longer work in 3.x.x #602

@basil

Description

@basil

The Migrating from HtmlUnit 2.x.x to HtmlUnit 3.x.x documentation states:

Replace all usages of org.htmlunit.html.HtmlInput.getValueAttribute() by org.htmlunit.html.HtmlInput.getValue(). Replace all usages of org.htmlunit.html.HtmlInput.setValueAttribute(String) by org.htmlunit.html.HtmlInput.setValue(String).

The problem is that once you do this, org.htmlunit.html.HtmlForm#getInputByValue and org.htmlunit.html.HtmlForm#getInputsByValue stop working, since they internally use org.htmlunit.html.DomElement#getAttributeDirect("value") rather than org.htmlunit.html.HtmlInput#getValue. Observed after debugging the test failures in jenkinsci/jenkins#8050.

Workaround:

/**
 * Returns the first input in this form with the specified value.
 * @param value the value to search for
 * @return the first input in this form with the specified value
 * @throws ElementNotFoundException if this form does not contain any inputs with the specified value
 */
private static HtmlInput getInputByValue(final HtmlForm f, final String value) throws ElementNotFoundException {
    // TODO org.htmlunit.html.HtmlForm#getInputByValue is busted
    return f.getInputsByValue("").stream()
            .filter(HtmlInput.class::isInstance)
            .map(HtmlInput.class::cast)
            .filter(input -> input.getValue().equals(value))
            .findFirst()
            .orElseThrow(() -> new ElementNotFoundException("input", "value", value));
}

/**
 * Returns all the inputs in this form with the specified value.
 * @param value the value to search for
 * @return all the inputs in this form with the specified value
 */
private static List<HtmlInput> getInputsByValue(final HtmlForm f, final String value) {
    // TODO org.htmlunit.html.HtmlForm#getInputsByValue is busted
    return f.getInputsByValue("").stream()
            .filter(HtmlInput.class::isInstance)
            .map(HtmlInput.class::cast)
            .filter(input -> input.getValue().equals(value))
            .collect(Collectors.toList());
}

CC @rbri

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions