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