Path: blob/trunk/cpp/iedriver/CommandHandlers/GetElementAttributeCommandHandler.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 "GetElementAttributeCommandHandler.h"17#include "errorcodes.h"18#include "../Browser.h"19#include "../Element.h"20#include "../IECommandExecutor.h"21#include "../VariantUtilities.h"2223namespace webdriver {2425GetElementAttributeCommandHandler::GetElementAttributeCommandHandler(void) {26}2728GetElementAttributeCommandHandler::~GetElementAttributeCommandHandler(void) {29}3031void GetElementAttributeCommandHandler::ExecuteInternal(32const IECommandExecutor& executor,33const ParametersMap& command_parameters,34Response* response) {35ParametersMap::const_iterator id_parameter_iterator = command_parameters.find("id");36ParametersMap::const_iterator name_parameter_iterator = command_parameters.find("name");37if (id_parameter_iterator == command_parameters.end()) {38response->SetErrorResponse(ERROR_INVALID_ARGUMENT, "Missing parameter in URL: id");39return;40} else if (name_parameter_iterator == command_parameters.end()) {41response->SetErrorResponse(ERROR_INVALID_ARGUMENT, "Missing parameter in URL: name");42return;43} else {44std::string element_id = id_parameter_iterator->second.asString();45std::string name = name_parameter_iterator->second.asString();4647BrowserHandle browser_wrapper;48int status_code = executor.GetCurrentBrowser(&browser_wrapper);49if (status_code != WD_SUCCESS) {50response->SetErrorResponse(ERROR_NO_SUCH_WINDOW, "Unable to get browser");51return;52}5354ElementHandle element_wrapper;55status_code = this->GetElement(executor, element_id, &element_wrapper);56if (status_code == WD_SUCCESS) {57CComVariant attribute_value;58IECommandExecutor& mutable_executor = const_cast<IECommandExecutor&>(executor);59Json::Value value;60status_code = element_wrapper->GetAttributeValue(name,61&attribute_value);62VariantUtilities::VariantAsJsonValue(mutable_executor.element_manager(),63attribute_value,64&value);65if (status_code != WD_SUCCESS) {66response->SetErrorResponse(status_code, "Unable to get attribute");67return;68} else {69response->SetSuccessResponse(value);70return;71}72} else if (status_code == ENOSUCHELEMENT) {73response->SetErrorResponse(ERROR_NO_SUCH_ELEMENT, "Invalid internal element ID requested: " + element_id);74return;75} else {76response->SetErrorResponse(ERROR_STALE_ELEMENT_REFERENCE, "Element is no longer valid");77return;78}79}80}8182} // namespace webdriver838485