Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
seleniumhq
GitHub Repository: seleniumhq/selenium
Path: blob/trunk/cpp/iedriver/CommandHandlers/GetActiveElementCommandHandler.cpp
2868 views
1
// Licensed to the Software Freedom Conservancy (SFC) under one
2
// or more contributor license agreements. See the NOTICE file
3
// distributed with this work for additional information
4
// regarding copyright ownership. The SFC licenses this file
5
// to you under the Apache License, Version 2.0 (the "License");
6
// you may not use this file except in compliance with the License.
7
// You may obtain a copy of the License at
8
//
9
// http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing, software
12
// distributed under the License is distributed on an "AS IS" BASIS,
13
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
// See the License for the specific language governing permissions and
15
// limitations under the License.
16
17
#include "GetActiveElementCommandHandler.h"
18
#include "errorcodes.h"
19
#include "../Browser.h"
20
#include "../Element.h"
21
#include "../IECommandExecutor.h"
22
23
namespace webdriver {
24
25
GetActiveElementCommandHandler::GetActiveElementCommandHandler(void) {
26
}
27
28
GetActiveElementCommandHandler::~GetActiveElementCommandHandler(void) {
29
}
30
31
void GetActiveElementCommandHandler::ExecuteInternal(
32
const IECommandExecutor& executor,
33
const ParametersMap& command_parameters,
34
Response* response) {
35
BrowserHandle browser_wrapper;
36
int status_code = executor.GetCurrentBrowser(&browser_wrapper);
37
if (status_code != WD_SUCCESS) {
38
response->SetErrorResponse(ERROR_NO_SUCH_WINDOW, "Unable to get browser");
39
return;
40
}
41
42
CComPtr<IHTMLDocument2> doc;
43
browser_wrapper->GetDocument(&doc);
44
if (!doc) {
45
response->SetErrorResponse(ERROR_NO_SUCH_WINDOW, "Document is not found");
46
return;
47
}
48
49
CComPtr<IHTMLElement> element(NULL);
50
HRESULT hr = doc->get_activeElement(&element);
51
52
if (FAILED(hr)) {
53
// For some contentEditable frames, the <body> element will be the
54
// active element. However, to properly have focus, we must explicitly
55
// set focus to the element.
56
CComPtr<IHTMLBodyElement> body_element;
57
HRESULT body_hr = element->QueryInterface<IHTMLBodyElement>(&body_element);
58
if (body_element) {
59
CComPtr<IHTMLElement2> body_element2;
60
body_element->QueryInterface<IHTMLElement2>(&body_element2);
61
body_element2->focus();
62
}
63
}
64
65
// If we don't have an element at this point, but the document
66
// has a body element, we should return a null result, as that's
67
// what document.activeElement() returns. However, if there is no
68
// body element, throw no such element.
69
if (!element) {
70
CComPtr<IHTMLElement> body;
71
hr = doc->get_body(&body);
72
if (body) {
73
response->SetSuccessResponse(Json::Value::null);
74
} else {
75
response->SetErrorResponse(ERROR_NO_SUCH_ELEMENT, "No active element found, and no body element present.");
76
}
77
return;
78
}
79
80
if (element) {
81
IECommandExecutor& mutable_executor = const_cast<IECommandExecutor&>(executor);
82
ElementHandle element_wrapper;
83
mutable_executor.AddManagedElement(element, &element_wrapper);
84
response->SetSuccessResponse(element_wrapper->ConvertToJson());
85
} else {
86
response->SetErrorResponse(ERROR_NO_SUCH_ELEMENT, "An unexpected error occurred getting the active element");
87
}
88
}
89
90
} // namespace webdriver
91
92