Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
seleniumhq
GitHub Repository: seleniumhq/selenium
Path: blob/trunk/cpp/iedriver/CommandHandlers/FindChildElementsCommandHandler.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 "FindChildElementsCommandHandler.h"
18
#include <ctime>
19
#include "errorcodes.h"
20
#include "../Browser.h"
21
#include "../IECommandExecutor.h"
22
#include "../WebDriverConstants.h"
23
24
namespace webdriver {
25
26
FindChildElementsCommandHandler::FindChildElementsCommandHandler(void) {
27
}
28
29
FindChildElementsCommandHandler::~FindChildElementsCommandHandler(void) {
30
}
31
32
void FindChildElementsCommandHandler::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 using_parameter_iterator = command_parameters.find("using");
38
ParametersMap::const_iterator value_parameter_iterator = command_parameters.find("value");
39
if (id_parameter_iterator == command_parameters.end()) {
40
response->SetErrorResponse(ERROR_INVALID_ARGUMENT, "Missing parameter in URL: id");
41
return;
42
}
43
if (using_parameter_iterator == command_parameters.end()) {
44
response->SetErrorResponse(ERROR_INVALID_ARGUMENT, "Missing parameter: using");
45
return;
46
}
47
if (!using_parameter_iterator->second.isString()) {
48
response->SetErrorResponse(ERROR_INVALID_ARGUMENT, "using parameter must be a string");
49
return;
50
}
51
if (value_parameter_iterator == command_parameters.end()) {
52
response->SetErrorResponse(ERROR_INVALID_ARGUMENT, "Missing parameter: value");
53
return;
54
}
55
if (!value_parameter_iterator->second.isString()) {
56
response->SetErrorResponse(ERROR_INVALID_ARGUMENT, "value parameter must be a string");
57
return;
58
}
59
60
std::string mechanism = using_parameter_iterator->second.asString();
61
std::string value = value_parameter_iterator->second.asString();
62
std::string element_id = id_parameter_iterator->second.asString();
63
64
if (mechanism != "css selector" &&
65
mechanism != "tag name" &&
66
mechanism != "link text" &&
67
mechanism != "partial link text" &&
68
mechanism != "xpath") {
69
response->SetErrorResponse(ERROR_INVALID_ARGUMENT, "using parameter value '" + mechanism + "' is not a valid value");
70
return;
71
}
72
73
BrowserHandle browser_wrapper;
74
int status_code = executor.GetCurrentBrowser(&browser_wrapper);
75
if (status_code != WD_SUCCESS) {
76
response->SetErrorResponse(status_code, "Currently focused window has been closed.");
77
return;
78
}
79
80
ElementHandle parent_element_wrapper;
81
status_code = this->GetElement(executor,
82
element_id,
83
&parent_element_wrapper);
84
85
if (status_code == WD_SUCCESS) {
86
Json::Value found_elements(Json::arrayValue);
87
88
int timeout = static_cast<int>(executor.implicit_wait_timeout());
89
clock_t end = clock() + (timeout / 1000 * CLOCKS_PER_SEC);
90
if (timeout > 0 && timeout < 1000) {
91
end += 1 * CLOCKS_PER_SEC;
92
}
93
94
do {
95
status_code = executor.LocateElements(parent_element_wrapper,
96
mechanism,
97
value,
98
&found_elements);
99
if (status_code == WD_SUCCESS) {
100
if (found_elements.isArray() && found_elements.size() > 0) {
101
response->SetSuccessResponse(found_elements);
102
return;
103
}
104
} else if (status_code == ENOSUCHWINDOW) {
105
response->SetErrorResponse(ERROR_NO_SUCH_WINDOW,
106
"Unable to find elements on closed window");
107
return;
108
} else {
109
response->SetErrorResponse(status_code, found_elements.asString());
110
return;
111
}
112
113
// Release the thread so that the browser doesn't starve.
114
::Sleep(FIND_ELEMENT_WAIT_TIME_IN_MILLISECONDS);
115
} while (clock() < end);
116
117
// This code is executed when no elements where found and no errors occurred.
118
if (status_code == WD_SUCCESS) {
119
response->SetSuccessResponse(found_elements);
120
} else {
121
response->SetErrorResponse(status_code,
122
"Finding elements with " + mechanism + " == " + value +
123
"returned an unexpected error");
124
}
125
} else {
126
if (status_code == EOBSOLETEELEMENT) {
127
response->SetErrorResponse(ERROR_STALE_ELEMENT_REFERENCE,
128
"Specified parent element is no longer attached to the DOM");
129
} else {
130
response->SetErrorResponse(ERROR_INVALID_ARGUMENT,
131
"Element is no longer valid");
132
}
133
}
134
}
135
136
} // namespace webdriver
137
138