HtmlUnit, how to post form without clicking submit button?
Asked Answered
T

5

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.

Thoer answered 27/9, 2011 at 17:54 Comment(2)
I'd recommend using an HttpURLConnection and follow the instructions outlined here. Or use Apache's HttpClient class.Pompidou
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 levelIncrease
A
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();
Anchorite answered 27/12, 2011 at 20:36 Comment(4)
You my friend are a genius! Been working around this for a week now!Blockbusting
It is the solution , I used it a lot of tile in the past and never had any problem.Fiore
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.Suitor
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 restaurantMogilev
D
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);
Derbent answered 27/11, 2012 at 13:31 Comment(0)
J
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();
}
Jobber answered 27/9, 2011 at 18:0 Comment(1)
The OP was edited and now it says that there is not a submit button.Empiric
S
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();
Swanherd answered 31/8, 2014 at 22:23 Comment(0)
S
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).

Suitor answered 11/5, 2018 at 10:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.