Path: blob/trunk/cpp/iedriver/CommandHandlers/FindChildElementCommandHandler.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.151617#include "FindChildElementCommandHandler.h"18#include <ctime>19#include "errorcodes.h"20#include "../Browser.h"21#include "../IECommandExecutor.h"22#include "../WebDriverConstants.h"2324namespace webdriver {2526FindChildElementCommandHandler::FindChildElementCommandHandler(void) {27}2829FindChildElementCommandHandler::~FindChildElementCommandHandler(void) {30}3132void FindChildElementCommandHandler::ExecuteInternal(33const IECommandExecutor& executor,34const ParametersMap& command_parameters,35Response* response) {36ParametersMap::const_iterator id_parameter_iterator = command_parameters.find("id");37ParametersMap::const_iterator using_parameter_iterator = command_parameters.find("using");38ParametersMap::const_iterator value_parameter_iterator = command_parameters.find("value");39if (id_parameter_iterator == command_parameters.end()) {40response->SetErrorResponse(ERROR_INVALID_ARGUMENT, "Missing parameter in URL: id");41return;42}43if (using_parameter_iterator == command_parameters.end()) {44response->SetErrorResponse(ERROR_INVALID_ARGUMENT, "Missing parameter: using");45return;46}47if (!using_parameter_iterator->second.isString()) {48response->SetErrorResponse(ERROR_INVALID_ARGUMENT, "using parameter must be a string");49return;50}51if (value_parameter_iterator == command_parameters.end()) {52response->SetErrorResponse(ERROR_INVALID_ARGUMENT, "Missing parameter: value");53return;54}55if (!value_parameter_iterator->second.isString()) {56response->SetErrorResponse(ERROR_INVALID_ARGUMENT, "value parameter must be a string");57return;58}5960std::string mechanism = using_parameter_iterator->second.asString();61std::string value = value_parameter_iterator->second.asString();62std::string element_id = id_parameter_iterator->second.asString();6364if (mechanism != "css selector" &&65mechanism != "tag name" &&66mechanism != "link text" &&67mechanism != "partial link text" &&68mechanism != "xpath") {69response->SetErrorResponse(ERROR_INVALID_ARGUMENT, "using parameter value '" + mechanism + "' is not a valid value");70return;71}7273BrowserHandle browser_wrapper;74int status_code = executor.GetCurrentBrowser(&browser_wrapper);75if (status_code != WD_SUCCESS) {76response->SetErrorResponse(status_code, "Currently focused window has been closed.");77return;78}7980ElementHandle parent_element_wrapper;81status_code = this->GetElement(executor,82element_id,83&parent_element_wrapper);8485if (status_code == WD_SUCCESS) {86Json::Value found_element;8788int timeout = static_cast<int>(executor.implicit_wait_timeout());89clock_t end = clock() + (timeout / 1000 * CLOCKS_PER_SEC);90if (timeout > 0 && timeout < 1000) {91end += 1 * CLOCKS_PER_SEC;92}9394do {95status_code = executor.LocateElement(parent_element_wrapper,96mechanism,97value,98&found_element);99if (status_code == WD_SUCCESS) {100response->SetSuccessResponse(found_element);101return;102}103if (status_code == ENOSUCHWINDOW) {104response->SetErrorResponse(ERROR_NO_SUCH_WINDOW,105"Unable to find element on closed window");106return;107}108if (status_code != ENOSUCHELEMENT) {109response->SetErrorResponse(status_code, found_element.asString());110return;111}112113// Release the thread so that the browser doesn't starve.114::Sleep(FIND_ELEMENT_WAIT_TIME_IN_MILLISECONDS);115} while (clock() < end);116117// This code is executed when status_code == ENOSUCHELEMENT118response->SetErrorResponse(ERROR_NO_SUCH_ELEMENT,119"Unable to find element with " + mechanism + " == " + value);120} else {121if (status_code == EOBSOLETEELEMENT) {122response->SetErrorResponse(ERROR_STALE_ELEMENT_REFERENCE,123"Specified parent element is no longer attached to the DOM");124} else {125response->SetErrorResponse(ERROR_INVALID_ARGUMENT,126"Element is no longer valid");127}128}129}130131} // namespace webdriver132133134