Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
seleniumhq
GitHub Repository: seleniumhq/selenium
Path: blob/trunk/cpp/iedriver/CommandHandlers/GetElementAttributeCommandHandler.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 "GetElementAttributeCommandHandler.h"
18
#include "errorcodes.h"
19
#include "../Browser.h"
20
#include "../Element.h"
21
#include "../IECommandExecutor.h"
22
#include "../VariantUtilities.h"
23
24
namespace webdriver {
25
26
GetElementAttributeCommandHandler::GetElementAttributeCommandHandler(void) {
27
}
28
29
GetElementAttributeCommandHandler::~GetElementAttributeCommandHandler(void) {
30
}
31
32
void GetElementAttributeCommandHandler::ExecuteInternal(
33
const IECommandExecutor& executor,
34
const ParametersMap& command_parameters,
35
Response* response) {
36
ParametersMap::const_iterator id_parameter_iterator = command_parameters.find("id");
37
ParametersMap::const_iterator name_parameter_iterator = command_parameters.find("name");
38
if (id_parameter_iterator == command_parameters.end()) {
39
response->SetErrorResponse(ERROR_INVALID_ARGUMENT, "Missing parameter in URL: id");
40
return;
41
} else if (name_parameter_iterator == command_parameters.end()) {
42
response->SetErrorResponse(ERROR_INVALID_ARGUMENT, "Missing parameter in URL: name");
43
return;
44
} else {
45
std::string element_id = id_parameter_iterator->second.asString();
46
std::string name = name_parameter_iterator->second.asString();
47
48
BrowserHandle browser_wrapper;
49
int status_code = executor.GetCurrentBrowser(&browser_wrapper);
50
if (status_code != WD_SUCCESS) {
51
response->SetErrorResponse(ERROR_NO_SUCH_WINDOW, "Unable to get browser");
52
return;
53
}
54
55
ElementHandle element_wrapper;
56
status_code = this->GetElement(executor, element_id, &element_wrapper);
57
if (status_code == WD_SUCCESS) {
58
CComVariant attribute_value;
59
IECommandExecutor& mutable_executor = const_cast<IECommandExecutor&>(executor);
60
Json::Value value;
61
status_code = element_wrapper->GetAttributeValue(name,
62
&attribute_value);
63
VariantUtilities::VariantAsJsonValue(mutable_executor.element_manager(),
64
attribute_value,
65
&value);
66
if (status_code != WD_SUCCESS) {
67
response->SetErrorResponse(status_code, "Unable to get attribute");
68
return;
69
} else {
70
response->SetSuccessResponse(value);
71
return;
72
}
73
} else if (status_code == ENOSUCHELEMENT) {
74
response->SetErrorResponse(ERROR_NO_SUCH_ELEMENT, "Invalid internal element ID requested: " + element_id);
75
return;
76
} else {
77
response->SetErrorResponse(ERROR_STALE_ELEMENT_REFERENCE, "Element is no longer valid");
78
return;
79
}
80
}
81
}
82
83
} // namespace webdriver
84
85