| 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/webelement_command.h" | 5 #include "chrome/test/webdriver/commands/webelement_commands.h" |
| 6 | 6 |
| 7 #include "base/scoped_ptr.h" |
| 8 #include "base/third_party/icu/icu_utf.h" |
| 9 #include "base/values.h" |
| 10 #include "chrome/test/webdriver/commands/response.h" |
| 11 #include "chrome/test/webdriver/error_codes.h" |
| 12 #include "chrome/test/webdriver/session.h" |
| 13 #include "chrome/test/webdriver/utility_functions.h" |
| 7 #include "third_party/webdriver/atoms.h" | 14 #include "third_party/webdriver/atoms.h" |
| 8 #include "chrome/test/webdriver/error_codes.h" | |
| 9 #include "chrome/test/webdriver/utility_functions.h" | |
| 10 | 15 |
| 11 namespace webdriver { | 16 namespace webdriver { |
| 12 | 17 |
| 13 bool WebElementCommand::Init(Response* const response) { | 18 bool WebElementCommand::Init(Response* const response) { |
| 14 if (WebDriverCommand::Init(response)) { | 19 if (!WebDriverCommand::Init(response)) |
| 15 SET_WEBDRIVER_ERROR(response, "Failure on Init for web element command", | |
| 16 kInternalServerError); | |
| 17 return false; | 20 return false; |
| 18 } | |
| 19 | 21 |
| 20 // There should be at least 5 segments to match | 22 // There should be at least 5 segments to match |
| 21 // "/session/$session/element/$id" | 23 // "/session/$session/element/$id" |
| 22 if (path_segments_.size() < 5) { | 24 if (path_segments_.size() < 5) { |
| 23 SET_WEBDRIVER_ERROR(response, "Path segments is less than 5", | 25 SET_WEBDRIVER_ERROR(response, "Path segments is less than 5", |
| 24 kBadRequest); | 26 kBadRequest); |
| 25 return false; | 27 return false; |
| 26 } | 28 } |
| 27 | 29 |
| 28 // We cannot verify the ID is valid until we execute the command and | 30 // We cannot verify the ID is valid until we execute the command and |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 75 LOG(ERROR) << "Expected JavaScript atom to return " | 77 LOG(ERROR) << "Expected JavaScript atom to return " |
| 76 << "{width:number, height:number} dictionary." << std::endl; | 78 << "{width:number, height:number} dictionary." << std::endl; |
| 77 return false; | 79 return false; |
| 78 } | 80 } |
| 79 | 81 |
| 80 DictionaryValue* dict = static_cast<DictionaryValue*>(result); | 82 DictionaryValue* dict = static_cast<DictionaryValue*>(result); |
| 81 return dict->GetInteger("width", width) && | 83 return dict->GetInteger("width", width) && |
| 82 dict->GetInteger("height", height); | 84 dict->GetInteger("height", height); |
| 83 } | 85 } |
| 84 | 86 |
| 87 void ElementValueCommand::ExecuteGet(Response* const response) { |
| 88 Value* unscoped_result = NULL; |
| 89 ListValue args; |
| 90 std::string script = "return arguments[0]['value']"; |
| 91 args.Append(GetElementIdAsDictionaryValue(element_id)); |
| 92 ErrorCode code = |
| 93 session_->ExecuteScript(script, &args, &unscoped_result); |
| 94 scoped_ptr<Value> result(unscoped_result); |
| 95 if (code != kSuccess) { |
| 96 SET_WEBDRIVER_ERROR(response, "Failed to execute script", code); |
| 97 return; |
| 98 } |
| 99 if (!result->IsType(Value::TYPE_STRING) && |
| 100 !result->IsType(Value::TYPE_NULL)) { |
| 101 SET_WEBDRIVER_ERROR(response, |
| 102 "Result is not string or null type", |
| 103 kInternalServerError); |
| 104 return; |
| 105 } |
| 106 response->set_status(kSuccess); |
| 107 response->set_value(result.release()); |
| 108 } |
| 109 |
| 110 void ElementValueCommand::ExecutePost(Response* const response) { |
| 111 ListValue* key_list; |
| 112 if (!GetListParameter("value", &key_list)) { |
| 113 SET_WEBDRIVER_ERROR(response, |
| 114 "Missing or invalid 'value' parameter", |
| 115 kBadRequest); |
| 116 return; |
| 117 } |
| 118 // Flatten the given array of strings into one. |
| 119 string16 keys; |
| 120 for (size_t i = 0; i < key_list->GetSize(); ++i) { |
| 121 string16 keys_list_part; |
| 122 key_list->GetString(i, &keys_list_part); |
| 123 for (size_t j = 0; j < keys_list_part.size(); ++j) { |
| 124 if (CBU16_IS_SURROGATE(keys_list_part[j])) { |
| 125 SET_WEBDRIVER_ERROR( |
| 126 response, |
| 127 "ChromeDriver only supports characters in the BMP", |
| 128 kBadRequest); |
| 129 return; |
| 130 } |
| 131 } |
| 132 keys.append(keys_list_part); |
| 133 } |
| 134 |
| 135 ErrorCode code = |
| 136 session_->SendKeys(GetElementIdAsDictionaryValue(element_id), keys); |
| 137 if (code != kSuccess) { |
| 138 SET_WEBDRIVER_ERROR(response, |
| 139 "Internal SendKeys error", |
| 140 code); |
| 141 return; |
| 142 } |
| 143 response->set_status(kSuccess); |
| 144 } |
| 145 |
| 146 void ElementTextCommand::ExecuteGet(Response* const response) { |
| 147 Value* unscoped_result = NULL; |
| 148 ListValue args; |
| 149 // TODO(jleyba): Use a real javascript atom. |
| 150 std::string script = |
| 151 "function getText(element) {" |
| 152 " if (element instanceof Text) {" |
| 153 " return element.textContent.replace(/^\\s+|\\s+$/g, '');" |
| 154 " }" |
| 155 " var childrenText = '';" |
| 156 " for (var i = 0; i < element.childNodes.length; i++) {" |
| 157 " childrenText += getText(element.childNodes[i]);" |
| 158 " }" |
| 159 " return childrenText;" |
| 160 "}" |
| 161 "return getText(arguments[0]);"; |
| 162 args.Append(GetElementIdAsDictionaryValue(element_id)); |
| 163 ErrorCode code = |
| 164 session_->ExecuteScript(script, &args, &unscoped_result); |
| 165 scoped_ptr<Value> result(unscoped_result); |
| 166 if (code != kSuccess) { |
| 167 SET_WEBDRIVER_ERROR(response, "Failed to execute script", code); |
| 168 return; |
| 169 } |
| 170 if (!result->IsType(Value::TYPE_STRING)) { |
| 171 SET_WEBDRIVER_ERROR(response, |
| 172 "Result is not string type", |
| 173 kInternalServerError); |
| 174 return; |
| 175 } |
| 176 response->set_status(kSuccess); |
| 177 response->set_value(result.release()); |
| 178 } |
| 179 |
| 85 } // namespace webdriver | 180 } // namespace webdriver |
| OLD | NEW |