HtmlUnit is a headless browser intended for use in Java applications to simulate a web browser, including form submissions, button clicks, and JavaScript execution. It can be particularly useful for testing web pages as it behaves like a real browser would, without the need for a graphical user interface.
To simulate browser events like clicks and form submissions in HtmlUnit, you typically work with the HtmlPage
object that represents a loaded web page, and the various HtmlElement
subclasses that represent different types of elements on the page. Here's how you can do common tasks:
Clicking an Element
To simulate a click on an element, you need to first find the element and then call the click()
method on it. For example, to click a button:
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlButton;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
try (final WebClient webClient = new WebClient()) {
HtmlPage page = webClient.getPage("http://someurl.com");
HtmlButton button = page.getFirstByXPath("//button[@id='myButton']");
HtmlPage newPage = button.click(); // This will simulate clicking the button
// newPage would be the page resulting from the click, if any
}
Submitting a Form
To submit a form, you can either click the submit button or directly call the submit()
method on the HtmlForm
object.
Here's how to submit a form by clicking the submit button:
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlForm;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.html.HtmlSubmitInput;
try (final WebClient webClient = new WebClient()) {
HtmlPage page = webClient.getPage("http://someurl.com");
HtmlForm form = page.getFormByName("myForm");
HtmlSubmitInput submitButton = form.getInputByName("submitButton");
HtmlPage newPage = submitButton.click(); // This will simulate form submission
// newPage would be the page you get after the form submission
}
Alternatively, you can submit the form directly:
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlForm;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
try (final WebClient webClient = new WebClient()) {
HtmlPage page = webClient.getPage("http://someurl.com");
HtmlForm form = page.getFormByName("myForm");
HtmlPage newPage = form.submit((HtmlSubmitInput) form.getInputByName("submitButton"));
// newPage would be the page you get after the form submission
}
Filling Out and Submitting a Form
Often, you'll need to fill out form fields before submitting. You can set the value on input elements before performing the submit action:
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.*;
try (final WebClient webClient = new WebClient()) {
HtmlPage page = webClient.getPage("http://someurl.com");
HtmlForm form = page.getFormByName("myForm");
HtmlTextInput textField = form.getInputByName("username");
textField.setValueAttribute("myUsername");
HtmlPasswordInput passwordField = form.getInputByName("password");
passwordField.setValueAttribute("myPassword");
HtmlSubmitInput submitButton = form.getInputByName("submitButton");
HtmlPage newPage = submitButton.click();
// newPage would be the page you get after the form submission
}
HtmlUnit is quite powerful and allows for much more complex interactions with the web page, including handling JavaScript, AJAX, cookies, and HTTP headers. The examples above are basic use cases to demonstrate browser event simulation like clicks and form submissions.