16

I know that in HtmlUnit i can fireEvent submit on form and it will be posted. But what If I disabled javascript and would like to post a form using some built in function?

I've checked the javadoc and haven't found any way to do this. It is strange that there is no such function in HtmlForm...


I read the javadoc and tutorial on htmlunit page and I Know that i can use getInputByName() and click it. BuT sometimes there are forms that don't have submit type button or even there is such button but without name attribute.

I am asking for help in such situation, this is why i am using fireEvent but it does not always work.

Gray
  • 115,027
  • 24
  • 293
  • 354
user967639
  • 161
  • 1
  • 1
  • 3
  • I'd recommend using an `HttpURLConnection` and follow the instructions outlined [here](http://stackoverflow.com/questions/2793150/how-to-use-java-net-urlconnection-to-fire-and-handle-http-requests). Or use Apache's `HttpClient` class. – mrkhrts Sep 27 '11 at 17:57
  • Check the JavaDoc again :) Or also the Introduction -> Getting Started section, as Ransom Briggs sugests. I wouldn't go for mrkhrts approach... it is too low level – Mosty Mostacho Sep 27 '11 at 22:42

5 Answers5

44

You can use a 'temporary' submit button:

WebClient client = new WebClient();
HtmlPage page = client.getPage("http://stackoverflow.com");

// create a submit button - it doesn't work with 'input'
HtmlElement button = page.createElement("button");
button.setAttribute("type", "submit");

// append the button to the form
HtmlElement form = ...;
form.appendChild(button);

// submit the form
page = button.click();
mirovarga
  • 461
  • 5
  • 7
  • 1
    You my friend are a genius! Been working around this for a week now! – duffanpj Jan 03 '17 at 16:52
  • It is the solution , I used it a lot of tile in the past and never had any problem. – Alain BUFERNE Jul 13 '17 at 16:53
  • **imho** this is cumbersome and might have some side effects. Unless it's your very test scenario you should try to emulate to user behaviour as close as possible. See my answer for some "native" solution. – m02ph3u5 May 11 '18 at 10:50
  • You Mr Mirovaraga ARE a genius. Also been working on this for TWO weeks now. Many thanks - I owe you a night out in Manchester at a top restaurant – Richard Hammond Sep 14 '21 at 11:47
7
WebRequest requestSettings = new WebRequest(new URL("http://localhost:8080/TestBox"), HttpMethod.POST);

// Then we set the request parameters
requestSettings.setRequestParameters(Collections.singletonList(new NameValuePair(InopticsNfcBoxPage.MESSAGE, Utils.marshalXml(inoptics, "UTF-8"))));

// Finally, we can get the page
HtmlPage page = webClient.getPage(requestSettings);
Martin
  • 1,385
  • 15
  • 21
1

How about getting use of built-in javascript support? Just fire submit event on that form:

HtmlForm form = page.getForms().get(0);
form.fireEvent(Event.TYPE_SUBMIT);

The code supposes you want to submit first form on the site.

And if the submit forwards you to another site, just link the response to the page variable:

HtmlForm form = page.getForms().get(0);
page = (HtmlPage) form.fireEvent(Event.TYPE_SUBMIT).getNewPage();
jirislav
  • 340
  • 6
  • 16
1
final HtmlSubmitInput button = form.getInputByName("submitbutton");
final HtmlPage page2 = button.click()

From the htmlunit doc

@Test
public void submittingForm() throws Exception {
    final WebClient webClient = new WebClient();

    // Get the first page
    final HtmlPage page1 = webClient.getPage("http://some_url");

    // Get the form that we are dealing with and within that form, 
    // find the submit button and the field that we want to change.
    final HtmlForm form = page1.getFormByName("myform");

    final HtmlSubmitInput button = form.getInputByName("submitbutton");
    final HtmlTextInput textField = form.getInputByName("userid");

    // Change the value of the text field
    textField.setValueAttribute("root");

    // Now submit the form by clicking the button and get back the second page.
    final HtmlPage page2 = button.click();

    webClient.closeAllWindows();
}
Ransom Briggs
  • 3,025
  • 3
  • 32
  • 46
0

Although this question has good and working answers none of them seems to emulate the acutal user behaviour quite well.
Think about it: What does a human do when there's no button to click? Simple, you hit enter (return). And that's just how it works in HtmlUnit:

// assuming page holds your current HtmlPage
HtmlForm form = page.getFormByName("yourFormName");
HtmlTextInput input = form.getInputByName("yourTextInputName");
// type something in
input.type("here goes the input");
// now hit enter and get the new page
page = (HtmlPage) input.type('\n');

Note that input.type("\n"); is not the same as input.type('\n');!

This works when you have disabled javascript exection and when there's no submit button available.


IMHO you should first think of how you want to submit the form. What scenario is it that you want to test? A user hitting return might be a different case that clicking some button (that might have some onClick). And submitting the form via JavaScript might be another test case.

When you figured that out pick the appropriate way of submitting your form from the other answers (and this one of course).

m02ph3u5
  • 3,022
  • 7
  • 38
  • 51