Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
seleniumhq
GitHub Repository: seleniumhq/selenium
Path: blob/trunk/cpp/iedriver/CommandHandlers/FindElementsCommandHandler.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 "FindElementsCommandHandler.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
FindElementsCommandHandler::FindElementsCommandHandler(void) {
27
}
28
29
FindElementsCommandHandler::~FindElementsCommandHandler(void) {
30
}
31
32
void FindElementsCommandHandler::ExecuteInternal(
33
const IECommandExecutor& executor,
34
const ParametersMap& command_parameters,
35
Response* response) {
36
ParametersMap::const_iterator using_parameter_iterator = command_parameters.find("using");
37
ParametersMap::const_iterator value_parameter_iterator = command_parameters.find("value");
38
if (using_parameter_iterator == command_parameters.end()) {
39
response->SetErrorResponse(ERROR_INVALID_ARGUMENT, "Missing parameter: using");
40
return;
41
}
42
if (!using_parameter_iterator->second.isString()) {
43
response->SetErrorResponse(ERROR_INVALID_ARGUMENT, "using parameter must be a string");
44
return;
45
}
46
if (value_parameter_iterator == command_parameters.end()) {
47
response->SetErrorResponse(ERROR_INVALID_ARGUMENT, "Missing parameter: value");
48
return;
49
}
50
if (!value_parameter_iterator->second.isString()) {
51
response->SetErrorResponse(ERROR_INVALID_ARGUMENT, "value parameter must be a string");
52
return;
53
}
54
55
std::string mechanism = using_parameter_iterator->second.asString();
56
std::string value = value_parameter_iterator->second.asString();
57
58
if (mechanism != "css selector" &&
59
mechanism != "tag name" &&
60
mechanism != "link text" &&
61
mechanism != "partial link text" &&
62
mechanism != "xpath") {
63
response->SetErrorResponse(ERROR_INVALID_ARGUMENT, "using parameter value '" + mechanism + "' is not a valid value");
64
return;
65
}
66
67
BrowserHandle browser_wrapper;
68
int status_code = executor.GetCurrentBrowser(&browser_wrapper);
69
if (status_code != WD_SUCCESS) {
70
response->SetErrorResponse(status_code, "Currently focused window has been closed.");
71
return;
72
}
73
74
int timeout = static_cast<int>(executor.implicit_wait_timeout());
75
clock_t end = clock() + (timeout / 1000 * CLOCKS_PER_SEC);
76
if (timeout > 0 && timeout < 1000) {
77
end += 1 * CLOCKS_PER_SEC;
78
}
79
80
status_code = WD_SUCCESS;
81
Json::Value found_elements;
82
do {
83
status_code = executor.LocateElements(ElementHandle(),
84
mechanism,
85
value,
86
&found_elements);
87
if (status_code == WD_SUCCESS) {
88
if (found_elements.isArray() && found_elements.size() > 0) {
89
response->SetSuccessResponse(found_elements);
90
return;
91
}
92
} else if (status_code == ENOSUCHWINDOW) {
93
response->SetErrorResponse(ERROR_NO_SUCH_WINDOW, "Unable to find elements on closed window");
94
return;
95
} else {
96
response->SetErrorResponse(status_code, found_elements.asString());
97
return;
98
}
99
100
// Release the thread so that the browser doesn't starve.
101
::Sleep(FIND_ELEMENT_WAIT_TIME_IN_MILLISECONDS);
102
} while (clock() < end);
103
104
// This code is executed when no elements where found and no errors occurred.
105
if (status_code == WD_SUCCESS) {
106
response->SetSuccessResponse(found_elements);
107
} else {
108
response->SetErrorResponse(status_code,
109
"Finding elements with " + mechanism + " == " + value +
110
"returned an unexpected error");
111
}
112
}
113
114
} // namespace webdriver
115
116