Vladimir
Vladimir

Reputation: 101

Jsoup: appending child <input> element to a <foorm> doesn't affect the form data

The following unit test fails:

@Test
public void test() throws Exception {
    String html = "<html><form id=\"myform\"></form></html>";
    Document document = Jsoup.parse(html);
    Element inputElement = document.createElement("input");
    inputElement.attr("name", "any_name");
    inputElement.attr("value", "any_value");
    Element formElement = document.getElementById("myform");
    formElement = formElement.appendChild(inputElement);
    List<Connection.KeyVal> formData =  ((FormElement)formElement).formData();
    Assert.assrt(1 == formData.size());
}

Questions:

  1. is it a bug or do I do something wrong?
  2. is there a workaround?

Upvotes: 0

Views: 271

Answers (1)

Janez Kuhar
Janez Kuhar

Reputation: 4266

You have to add the input element to the form!

Look at the documentation of addElement​(Element element):

Add a form control element to this form.

So, in your case, you have to add the following line to your code before the assertion:

((FormElement) formElement).addElement(inputElement);

Upvotes: 0

Related Questions