| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/test/webdriver/commands/execute_command.h" |
| 6 |
| 5 #include <string> | 7 #include <string> |
| 6 | 8 |
| 7 #include "base/json/json_reader.h" | 9 #include "base/values.h" |
| 8 | 10 #include "chrome/test/webdriver/error_codes.h" |
| 9 #include "chrome/test/webdriver/commands/execute_command.h" | 11 #include "chrome/test/webdriver/session.h" |
| 12 #include "chrome/test/webdriver/commands/response.h" |
| 10 | 13 |
| 11 namespace webdriver { | 14 namespace webdriver { |
| 12 | 15 |
| 13 const char kArgs[] = "args"; | 16 const char kArgs[] = "args"; |
| 14 const char kScript[] = "script"; | 17 const char kScript[] = "script"; |
| 15 | 18 |
| 16 ExecuteCommand::ExecuteCommand(const std::vector<std::string>& path_segments, | 19 ExecuteCommand::ExecuteCommand(const std::vector<std::string>& path_segments, |
| 17 const DictionaryValue* const parameters) | 20 const DictionaryValue* const parameters) |
| 18 : WebDriverCommand(path_segments, parameters) {} | 21 : WebDriverCommand(path_segments, parameters) {} |
| 19 | 22 |
| 20 ExecuteCommand::~ExecuteCommand() {} | 23 ExecuteCommand::~ExecuteCommand() {} |
| 21 | 24 |
| 22 | |
| 23 bool ExecuteCommand::Init(Response* const response) { | |
| 24 if (!WebDriverCommand::Init(response)) { | |
| 25 SET_WEBDRIVER_ERROR(response, "Failure on Init for execute command", | |
| 26 kInternalServerError); | |
| 27 return false; | |
| 28 } | |
| 29 | |
| 30 if (!GetStringParameter(kScript, &script_)) { | |
| 31 SET_WEBDRIVER_ERROR(response, "No script to execute specified", | |
| 32 kBadRequest); | |
| 33 return false; | |
| 34 } | |
| 35 | |
| 36 has_args_= GetStringASCIIParameter(kArgs, &args_); | |
| 37 return true; | |
| 38 } | |
| 39 | |
| 40 bool ExecuteCommand::DoesPost() { | 25 bool ExecuteCommand::DoesPost() { |
| 41 return true; | 26 return true; |
| 42 } | 27 } |
| 43 | 28 |
| 44 void ExecuteCommand::ExecutePost(Response* const response) { | 29 void ExecuteCommand::ExecutePost(Response* const response) { |
| 45 int error_code = 0; | 30 std::string script; |
| 46 std::string error_msg; | 31 if (!GetStringParameter(kScript, &script)) { |
| 47 Value* params = NULL; | 32 SET_WEBDRIVER_ERROR(response, "No script to execute specified", |
| 48 Value* result = NULL; | |
| 49 | |
| 50 if (has_args_) { | |
| 51 params = base::JSONReader::ReadAndReturnError(args_, true, | |
| 52 &error_code, &error_msg); | |
| 53 if (error_code != 0) { | |
| 54 LOG(INFO) << "Could not parse the JSON arguments passed in " | |
| 55 << "got error code: " << error_code << ", " << error_msg; | |
| 56 SET_WEBDRIVER_ERROR(response, "Arguments are not valid json objects", | |
| 57 kBadRequest); | |
| 58 return; | |
| 59 } | |
| 60 } else { | |
| 61 LOG(INFO) << "No args for this script"; | |
| 62 // If there are no args required just use an empty list. | |
| 63 params = new ListValue(); | |
| 64 } | |
| 65 | |
| 66 if (params->GetType() != Value::TYPE_LIST) { | |
| 67 LOG(INFO) << "Data passed in to script must be a json list"; | |
| 68 SET_WEBDRIVER_ERROR(response, "Arguments are not in list format", | |
| 69 kBadRequest); | 33 kBadRequest); |
| 70 return; | 34 return; |
| 71 } | 35 } |
| 72 | 36 |
| 73 ListValue* script_args = static_cast<ListValue*>(params); | 37 ListValue* args; |
| 74 ErrorCode error = session_->ExecuteScript(script_, | 38 if (!GetListParameter(kArgs, &args)) { |
| 75 script_args, &result); | 39 SET_WEBDRIVER_ERROR(response, "No script arguments specified", |
| 76 | 40 kBadRequest); |
| 77 if (error != kSuccess) { | |
| 78 SET_WEBDRIVER_ERROR(response, "Failed to execute script", | |
| 79 kInternalServerError); | |
| 80 return; | 41 return; |
| 81 } | 42 } |
| 82 | 43 |
| 44 Value* result = NULL; |
| 45 ErrorCode status = session_->ExecuteScript(script, args, &result); |
| 46 response->set_status(status); |
| 83 response->set_value(result); | 47 response->set_value(result); |
| 84 response->set_status(kSuccess); | |
| 85 } | 48 } |
| 86 | 49 |
| 87 bool ExecuteCommand::RequiresValidTab() { | 50 bool ExecuteCommand::RequiresValidTab() { |
| 88 return true; | 51 return true; |
| 89 } | 52 } |
| 90 | 53 |
| 91 } // namspace webdriver | 54 } // namspace webdriver |
| 92 | 55 |
| OLD | NEW |