Path: blob/trunk/cpp/iedriver/CommandHandlers/FindElementsCommandHandler.cpp
2868 views
// Licensed to the Software Freedom Conservancy (SFC) under one1// or more contributor license agreements. See the NOTICE file2// distributed with this work for additional information3// regarding copyright ownership. The SFC licenses this file4// to you under the Apache License, Version 2.0 (the "License");5// you may not use this file except in compliance with the License.6// You may obtain a copy of the License at7//8// http://www.apache.org/licenses/LICENSE-2.09//10// Unless required by applicable law or agreed to in writing, software11// distributed under the License is distributed on an "AS IS" BASIS,12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.13// See the License for the specific language governing permissions and14// limitations under the License.1516#include "FindElementsCommandHandler.h"17#include <ctime>18#include "errorcodes.h"19#include "../Browser.h"20#include "../IECommandExecutor.h"21#include "../WebDriverConstants.h"2223namespace webdriver {2425FindElementsCommandHandler::FindElementsCommandHandler(void) {26}2728FindElementsCommandHandler::~FindElementsCommandHandler(void) {29}3031void FindElementsCommandHandler::ExecuteInternal(32const IECommandExecutor& executor,33const ParametersMap& command_parameters,34Response* response) {35ParametersMap::const_iterator using_parameter_iterator = command_parameters.find("using");36ParametersMap::const_iterator value_parameter_iterator = command_parameters.find("value");37if (using_parameter_iterator == command_parameters.end()) {38response->SetErrorResponse(ERROR_INVALID_ARGUMENT, "Missing parameter: using");39return;40}41if (!using_parameter_iterator->second.isString()) {42response->SetErrorResponse(ERROR_INVALID_ARGUMENT, "using parameter must be a string");43return;44}45if (value_parameter_iterator == command_parameters.end()) {46response->SetErrorResponse(ERROR_INVALID_ARGUMENT, "Missing parameter: value");47return;48}49if (!value_parameter_iterator->second.isString()) {50response->SetErrorResponse(ERROR_INVALID_ARGUMENT, "value parameter must be a string");51return;52}5354std::string mechanism = using_parameter_iterator->second.asString();55std::string value = value_parameter_iterator->second.asString();5657if (mechanism != "css selector" &&58mechanism != "tag name" &&59mechanism != "link text" &&60mechanism != "partial link text" &&61mechanism != "xpath") {62response->SetErrorResponse(ERROR_INVALID_ARGUMENT, "using parameter value '" + mechanism + "' is not a valid value");63return;64}6566BrowserHandle browser_wrapper;67int status_code = executor.GetCurrentBrowser(&browser_wrapper);68if (status_code != WD_SUCCESS) {69response->SetErrorResponse(status_code, "Currently focused window has been closed.");70return;71}7273int timeout = static_cast<int>(executor.implicit_wait_timeout());74clock_t end = clock() + (timeout / 1000 * CLOCKS_PER_SEC);75if (timeout > 0 && timeout < 1000) {76end += 1 * CLOCKS_PER_SEC;77}7879status_code = WD_SUCCESS;80Json::Value found_elements;81do {82status_code = executor.LocateElements(ElementHandle(),83mechanism,84value,85&found_elements);86if (status_code == WD_SUCCESS) {87if (found_elements.isArray() && found_elements.size() > 0) {88response->SetSuccessResponse(found_elements);89return;90}91} else if (status_code == ENOSUCHWINDOW) {92response->SetErrorResponse(ERROR_NO_SUCH_WINDOW, "Unable to find elements on closed window");93return;94} else {95response->SetErrorResponse(status_code, found_elements.asString());96return;97}9899// Release the thread so that the browser doesn't starve.100::Sleep(FIND_ELEMENT_WAIT_TIME_IN_MILLISECONDS);101} while (clock() < end);102103// This code is executed when no elements where found and no errors occurred.104if (status_code == WD_SUCCESS) {105response->SetSuccessResponse(found_elements);106} else {107response->SetErrorResponse(status_code,108"Finding elements with " + mechanism + " == " + value +109"returned an unexpected error");110}111}112113} // namespace webdriver114115116