Skip to content

Commit

Permalink
Updating IE driver getActiveElement command to be spec-compliant
Browse files Browse the repository at this point in the history
If there is no active element, this method may now return a null results,
as in compliance with the W3C WebDriver specification, which relies on the
DOM definition of active element. Language bindings maintainers should
take note that this is a behavior change, and that the command can now
return null values. Adjust your language bindings accordingly.
  • Loading branch information
jimevans committed Nov 28, 2017
1 parent 8d73e5f commit 6ccbe0a
Showing 1 changed file with 20 additions and 17 deletions.
37 changes: 20 additions & 17 deletions cpp/iedriver/CommandHandlers/GetActiveElementCommandHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ GetActiveElementCommandHandler::~GetActiveElementCommandHandler(void) {
}

void GetActiveElementCommandHandler::ExecuteInternal(
const IECommandExecutor& executor,
const ParametersMap& command_parameters,
Response* response) {
const IECommandExecutor& executor,
const ParametersMap& command_parameters,
Response* response) {
BrowserHandle browser_wrapper;
int status_code = executor.GetCurrentBrowser(&browser_wrapper);
if (status_code != WD_SUCCESS) {
Expand All @@ -46,24 +46,27 @@ void GetActiveElementCommandHandler::ExecuteInternal(
return;
}

CComPtr<IHTMLElement> element;
doc->get_activeElement(&element);
CComPtr<IHTMLElement> element(NULL);
HRESULT hr = doc->get_activeElement(&element);

// For some contentEditable frames, the <body> element will be the
// active element. However, to properly have focus, we must explicitly
// set focus to the element.
CComPtr<IHTMLBodyElement> body_element;
HRESULT body_hr = element->QueryInterface<IHTMLBodyElement>(&body_element);
if (body_element) {
CComPtr<IHTMLElement2> body_element2;
body_element->QueryInterface<IHTMLElement2>(&body_element2);
body_element2->focus();
if (FAILED(hr)) {
// For some contentEditable frames, the <body> element will be the
// active element. However, to properly have focus, we must explicitly
// set focus to the element.
CComPtr<IHTMLBodyElement> body_element;
HRESULT body_hr = element->QueryInterface<IHTMLBodyElement>(&body_element);
if (body_element) {
CComPtr<IHTMLElement2> body_element2;
body_element->QueryInterface<IHTMLElement2>(&body_element2);
body_element2->focus();
}
}

// If we don't have an element at this point, just return the
// body element so that we don't return a NULL pointer.
// If we don't have an element at this point, we should return a
// null result, as that's what document.activeElement() returns.
if (!element) {
doc->get_body(&element);
response->SetSuccessResponse(Json::Value::null);
return;
}

if (element) {
Expand Down

0 comments on commit 6ccbe0a

Please sign in to comment.