Path: blob/trunk/cpp/iedriver/CommandHandlers/FindChildElementsCommandHandler.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 "FindChildElementsCommandHandler.h"17#include <ctime>18#include "errorcodes.h"19#include "../Browser.h"20#include "../IECommandExecutor.h"21#include "../WebDriverConstants.h"2223namespace webdriver {2425FindChildElementsCommandHandler::FindChildElementsCommandHandler(void) {26}2728FindChildElementsCommandHandler::~FindChildElementsCommandHandler(void) {29}3031void FindChildElementsCommandHandler::ExecuteInternal(32const IECommandExecutor& executor,33const ParametersMap& command_parameters,34Response* response) {35ParametersMap::const_iterator id_parameter_iterator = command_parameters.find("id");36ParametersMap::const_iterator using_parameter_iterator = command_parameters.find("using");37ParametersMap::const_iterator value_parameter_iterator = command_parameters.find("value");38if (id_parameter_iterator == command_parameters.end()) {39response->SetErrorResponse(ERROR_INVALID_ARGUMENT, "Missing parameter in URL: id");40return;41}42if (using_parameter_iterator == command_parameters.end()) {43response->SetErrorResponse(ERROR_INVALID_ARGUMENT, "Missing parameter: using");44return;45}46if (!using_parameter_iterator->second.isString()) {47response->SetErrorResponse(ERROR_INVALID_ARGUMENT, "using parameter must be a string");48return;49}50if (value_parameter_iterator == command_parameters.end()) {51response->SetErrorResponse(ERROR_INVALID_ARGUMENT, "Missing parameter: value");52return;53}54if (!value_parameter_iterator->second.isString()) {55response->SetErrorResponse(ERROR_INVALID_ARGUMENT, "value parameter must be a string");56return;57}5859std::string mechanism = using_parameter_iterator->second.asString();60std::string value = value_parameter_iterator->second.asString();61std::string element_id = id_parameter_iterator->second.asString();6263if (mechanism != "css selector" &&64mechanism != "tag name" &&65mechanism != "link text" &&66mechanism != "partial link text" &&67mechanism != "xpath") {68response->SetErrorResponse(ERROR_INVALID_ARGUMENT, "using parameter value '" + mechanism + "' is not a valid value");69return;70}7172BrowserHandle browser_wrapper;73int status_code = executor.GetCurrentBrowser(&browser_wrapper);74if (status_code != WD_SUCCESS) {75response->SetErrorResponse(status_code, "Currently focused window has been closed.");76return;77}7879ElementHandle parent_element_wrapper;80status_code = this->GetElement(executor,81element_id,82&parent_element_wrapper);8384if (status_code == WD_SUCCESS) {85Json::Value found_elements(Json::arrayValue);8687int timeout = static_cast<int>(executor.implicit_wait_timeout());88clock_t end = clock() + (timeout / 1000 * CLOCKS_PER_SEC);89if (timeout > 0 && timeout < 1000) {90end += 1 * CLOCKS_PER_SEC;91}9293do {94status_code = executor.LocateElements(parent_element_wrapper,95mechanism,96value,97&found_elements);98if (status_code == WD_SUCCESS) {99if (found_elements.isArray() && found_elements.size() > 0) {100response->SetSuccessResponse(found_elements);101return;102}103} else if (status_code == ENOSUCHWINDOW) {104response->SetErrorResponse(ERROR_NO_SUCH_WINDOW,105"Unable to find elements on closed window");106return;107} else {108response->SetErrorResponse(status_code, found_elements.asString());109return;110}111112// Release the thread so that the browser doesn't starve.113::Sleep(FIND_ELEMENT_WAIT_TIME_IN_MILLISECONDS);114} while (clock() < end);115116// This code is executed when no elements where found and no errors occurred.117if (status_code == WD_SUCCESS) {118response->SetSuccessResponse(found_elements);119} else {120response->SetErrorResponse(status_code,121"Finding elements with " + mechanism + " == " + value +122"returned an unexpected error");123}124} else {125if (status_code == EOBSOLETEELEMENT) {126response->SetErrorResponse(ERROR_STALE_ELEMENT_REFERENCE,127"Specified parent element is no longer attached to the DOM");128} else {129response->SetErrorResponse(ERROR_INVALID_ARGUMENT,130"Element is no longer valid");131}132}133}134135} // namespace webdriver136137138