Skip to content

Commit

Permalink
Separating out detection of focusable elements for sendKeys in IE
Browse files Browse the repository at this point in the history
Allows separate error messages for attempting to send keys to a
non-focusable element. Also fixes sending keystrokes to <option> elements
by forwarding the detections to the parent <select> element.
  • Loading branch information
jimevans committed Mar 19, 2018
1 parent b44592f commit 8ffb552
Showing 1 changed file with 28 additions and 2 deletions.
30 changes: 28 additions & 2 deletions cpp/iedriver/CommandHandlers/SendKeysCommandHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,30 @@ void SendKeysCommandHandler::ExecuteInternal(
HWND window_handle = browser_wrapper->GetContentWindowHandle();
HWND top_level_window_handle = browser_wrapper->GetTopLevelWindowHandle();

ElementHandle element_wrapper;
status_code = this->GetElement(executor, element_id, &element_wrapper);
ElementHandle initial_element;
status_code = this->GetElement(executor, element_id, &initial_element);

if (status_code == WD_SUCCESS) {
ElementHandle element_wrapper = initial_element;
CComPtr<IHTMLOptionElement> option;
HRESULT hr = initial_element->element()->QueryInterface<IHTMLOptionElement>(&option);
if (SUCCEEDED(hr) && option) {
// If this is an <option> element, we want to operate on its parent
// <select> element.
CComPtr<IHTMLElement> parent_node;
hr = initial_element->element()->get_parentElement(&parent_node);
while (SUCCEEDED(hr) && parent_node) {
CComPtr<IHTMLSelectElement> select;
HRESULT select_hr = parent_node->QueryInterface<IHTMLSelectElement>(&select);
if (SUCCEEDED(select_hr) && select) {
IECommandExecutor& mutable_executor = const_cast<IECommandExecutor&>(executor);
mutable_executor.AddManagedElement(parent_node, &element_wrapper);
break;
}
hr = parent_node->get_parentElement(&parent_node);
}
}

CComPtr<IHTMLElement> element(element_wrapper->element());

// Scroll the target element into view before executing the action
Expand Down Expand Up @@ -233,6 +253,12 @@ void SendKeysCommandHandler::ExecuteInternal(
return;
}

if (!element_wrapper->IsFocusable()) {
response->SetErrorResponse(ERROR_ELEMENT_NOT_INTERACTABLE,
"Element cannot be interacted with via the keyboard because it is not focusable");
return;
}

if (!this->VerifyPageHasFocus(top_level_window_handle, window_handle)) {
LOG(WARN) << "HTML rendering pane does not have the focus. Keystrokes may go to an unexpected UI element.";
}
Expand Down

0 comments on commit 8ffb552

Please sign in to comment.