Focus
For a given WebClient, focus can be on at most one element at any given time.
Focus does not need to be set on any element.
There are several ways to move focus between elements.
The simplest way is to call HtmlPage.setFocusedElement(HtmlElement).
This method removes focus from whichever element currently holds focus (if any) and sets focus
to the target component, triggering any defined onfocus and onblur handlers along the way.
final HtmlInput userName = page.getHtmlElementById("userName");
page.setFocusedElement(userName);
The element currently owning focus can be retrieved with a call to
HtmlPage.getFocusedElement().
HtmlElement elem = page.getFocusedElement();
Item Traversal
To simulate keyboard navigation via the Tab key, call
HtmlPage.tabToNextElement() and HtmlPage.tabToPreviousElement()
to cycle forward or backward through the tab order.
This order is governed by the
tabindex
attribute on elements as defined by the HTML specification.
You can query all tabbable elements in order using HtmlPage.getTabbableElements().
To assert that every tabbable element on a page has an explicit tabindex attribute set,
use WebAssert.assertAllTabIndexAttributesSet(page).
Access Keys
Access keys (keyboard mnemonics) can be simulated using the method
HtmlPage.pressAccessKey(char).
Special Keys
To send special keys, use htmlElement.type(int) with constants such as KeyboardEvent.DOM_VK_PAGE_DOWN.
To simulate typing while holding modifier keys (Shift/Ctrl/Alt), use the Keyboard class.
For example, to simulate Ctrl+C to copy content to the clipboard:
final HtmlInput input = (HtmlInput) page.getElementById("i1");
final Keyboard kb = new Keyboard();
kb.press(KeyboardEvent.DOM_VK_CONTROL);
kb.type('c');
kb.release(KeyboardEvent.DOM_VK_CONTROL);
input.type(kb);
Clipboard
Clipboard interaction is disabled by default in WebClient to prevent side effects during testing
and to remove the requirement for a running graphical environment (e.g., Windows/X11/xvfb).
To enable clipboard support, set a clipboard handler on the WebClient.
HtmlUnit provides AwtClipboardHandler, which integrates with system/desktop clipboards:
final ClipboardHandler clipboardHandler = new AwtClipboardHandler();
webClient.setClipboardHandler(clipboardHandler);
Note: The provided AwtClipboardHandler will not work if your Java VM runs in headless mode.
If you need clipboard interaction in headless mode, write a custom implementation of ClipboardHandler.
You can directly manage clipboard content from your test cases:
clipboardHandler.setClipboardContent("HtmlUnit");
You can also implement your custom ClipboardHandler for full control over clipboard behaviors.
To interact with the clipboard, send standard keyboard shortcuts:
final Keyboard kb = new Keyboard();
// Select all
kb.press(KeyboardEvent.DOM_VK_CONTROL);
kb.type('a');
kb.release(KeyboardEvent.DOM_VK_CONTROL);
// Copy
kb.press(KeyboardEvent.DOM_VK_CONTROL);
kb.type('c');
kb.release(KeyboardEvent.DOM_VK_CONTROL);
input.type(kb);
final Keyboard kb = new Keyboard();
// Paste
kb.press(KeyboardEvent.DOM_VK_CONTROL);
kb.type('v');
kb.release(KeyboardEvent.DOM_VK_CONTROL);
input.type(kb);

