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" |
kkania
2011/02/17 23:37:47
since you're fixing up headers, how about add one
Jason Leyba
2011/02/18 00:08:00
Done.
| |
8 | |
9 #include "chrome/test/webdriver/commands/execute_command.h" | |
10 | 10 |
11 namespace webdriver { | 11 namespace webdriver { |
12 | 12 |
13 const char kArgs[] = "args"; | 13 const char kArgs[] = "args"; |
14 const char kScript[] = "script"; | 14 const char kScript[] = "script"; |
15 | 15 |
16 ExecuteCommand::ExecuteCommand(const std::vector<std::string>& path_segments, | 16 ExecuteCommand::ExecuteCommand(const std::vector<std::string>& path_segments, |
17 const DictionaryValue* const parameters) | 17 const DictionaryValue* const parameters) |
18 : WebDriverCommand(path_segments, parameters) {} | 18 : WebDriverCommand(path_segments, parameters) {} |
19 | 19 |
20 ExecuteCommand::~ExecuteCommand() {} | 20 ExecuteCommand::~ExecuteCommand() {} |
21 | 21 |
22 | |
23 bool ExecuteCommand::Init(Response* const response) { | 22 bool ExecuteCommand::Init(Response* const response) { |
24 if (!WebDriverCommand::Init(response)) { | 23 if (!WebDriverCommand::Init(response)) { |
25 SET_WEBDRIVER_ERROR(response, "Failure on Init for execute command", | 24 SET_WEBDRIVER_ERROR(response, "Failure on Init for execute command", |
26 kInternalServerError); | 25 kInternalServerError); |
27 return false; | 26 return false; |
28 } | 27 } |
29 | 28 |
30 if (!GetStringParameter(kScript, &script_)) { | 29 if (!GetStringParameter(kScript, &script_)) { |
kkania
2011/02/17 23:37:47
Can you move the parsing of the JSON params out of
Jason Leyba
2011/02/18 00:08:00
Done.
| |
31 SET_WEBDRIVER_ERROR(response, "No script to execute specified", | 30 SET_WEBDRIVER_ERROR(response, "No script to execute specified", |
32 kBadRequest); | 31 kBadRequest); |
33 return false; | 32 return false; |
34 } | 33 } |
35 | 34 |
36 has_args_= GetStringASCIIParameter(kArgs, &args_); | 35 if (!GetListParameter(kArgs, &args_)) { |
36 SET_WEBDRIVER_ERROR(response, "No script arguments specified", | |
37 kBadRequest); | |
38 return false; | |
39 } | |
40 | |
37 return true; | 41 return true; |
38 } | 42 } |
39 | 43 |
40 bool ExecuteCommand::DoesPost() { | 44 bool ExecuteCommand::DoesPost() { |
41 return true; | 45 return true; |
42 } | 46 } |
43 | 47 |
44 void ExecuteCommand::ExecutePost(Response* const response) { | 48 void ExecuteCommand::ExecutePost(Response* const response) { |
45 int error_code = 0; | |
46 std::string error_msg; | |
47 Value* params = NULL; | |
48 Value* result = NULL; | 49 Value* result = NULL; |
49 | 50 ErrorCode status = session_->ExecuteScript(script_, args_, &result); |
50 if (has_args_) { | 51 response->set_status(status); |
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); | |
70 return; | |
71 } | |
72 | |
73 ListValue* script_args = static_cast<ListValue*>(params); | |
74 ErrorCode error = session_->ExecuteScript(script_, | |
75 script_args, &result); | |
76 | |
77 if (error != kSuccess) { | |
78 SET_WEBDRIVER_ERROR(response, "Failed to execute script", | |
79 kInternalServerError); | |
80 return; | |
81 } | |
82 | |
83 response->set_value(result); | 52 response->set_value(result); |
84 response->set_status(kSuccess); | |
85 } | 53 } |
86 | 54 |
87 bool ExecuteCommand::RequiresValidTab() { | 55 bool ExecuteCommand::RequiresValidTab() { |
88 return true; | 56 return true; |
89 } | 57 } |
90 | 58 |
91 } // namspace webdriver | 59 } // namspace webdriver |
92 | 60 |
OLD | NEW |