Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/session_manager.h" | 5 #include "chrome/test/webdriver/session_manager.h" |
| 6 | 6 |
| 7 #include <sstream> | 7 #include <sstream> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
| (...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 155 } | 155 } |
| 156 | 156 |
| 157 ErrorCode Session::ExecuteScript(const std::string& script, | 157 ErrorCode Session::ExecuteScript(const std::string& script, |
| 158 const ListValue* const args, | 158 const ListValue* const args, |
| 159 Value** value) { | 159 Value** value) { |
| 160 return ExecuteScript( | 160 return ExecuteScript( |
| 161 current_window_id_, current_frame_xpath_, script, args, value); | 161 current_window_id_, current_frame_xpath_, script, args, value); |
| 162 } | 162 } |
| 163 | 163 |
| 164 ErrorCode Session::SendKeys(const WebElementId& element, const string16& keys) { | 164 ErrorCode Session::SendKeys(const WebElementId& element, const string16& keys) { |
| 165 ListValue args; | 165 // This method will first check if the element we want to send the keys to is |
| 166 args.Append(element.ToValue()); | 166 // already focused, if not it will try to focus on it first. |
| 167 // TODO(jleyba): Update this to use the correct atom. | 167 scoped_ptr<ListValue> nullArgs(new ListValue); |
|
Jason Leyba
2011/03/04 19:25:12
This would be much simpler if you did it all with
timothe faudot
2011/03/07 02:45:26
Done.
| |
| 168 std::string script = "document.activeElement.blur();arguments[0].focus();"; | 168 Value* active_element = NULL; |
| 169 Value* unscoped_result = NULL; | 169 ErrorCode status = ExecuteScript( |
| 170 ErrorCode code = ExecuteScript(script, &args, &unscoped_result); | 170 "return document.activeElement;", nullArgs.get(), &active_element); |
| 171 scoped_ptr<Value> result(unscoped_result); | 171 if (status != kSuccess) { |
| 172 if (code != kSuccess) { | 172 LOG(ERROR) << "Failed to get which element was focused before sending keys"; |
| 173 LOG(ERROR) << "Failed to focus element before sending keys"; | 173 return status; |
| 174 return code; | |
| 175 } | 174 } |
| 176 | 175 if (active_element == NULL || |
| 176 active_element->GetType() != Value::TYPE_DICTIONARY) { | |
| 177 LOG(ERROR) << "Expected Japascript to return a dictionary"; | |
| 178 return kUnknownError; | |
| 179 } | |
| 180 std::string active_elem_str; | |
| 181 static_cast<DictionaryValue*>(active_element)->GetString("ELEMENT", | |
| 182 &active_elem_str); | |
| 183 std::string wanted_element; | |
| 184 DictionaryValue* element_id = | |
| 185 static_cast<DictionaryValue*>(element.ToValue()); | |
| 186 element_id->GetString("ELEMENT", &wanted_element); | |
| 187 // We focus on the element only if necessary. | |
| 188 if (active_elem_str != wanted_element) { | |
| 189 scoped_ptr<ListValue> args(new ListValue); | |
| 190 args->Append(element.ToValue()); | |
| 191 // TODO(jleyba): Update this to use the correct atom. | |
| 192 std::string script = "document.activeElement.blur();arguments[0].focus();"; | |
| 193 Value* unscoped_result = NULL; | |
| 194 ErrorCode code = ExecuteScript(script, args.get(), &unscoped_result); | |
| 195 if (code != kSuccess) { | |
| 196 LOG(ERROR) << "Failed to focus element before sending keys"; | |
| 197 return code; | |
| 198 } | |
| 199 } | |
| 177 bool success = false; | 200 bool success = false; |
| 178 RunSessionTask(NewRunnableMethod( | 201 RunSessionTask(NewRunnableMethod( |
| 179 this, | 202 this, |
| 180 &Session::SendKeysOnSessionThread, | 203 &Session::SendKeysOnSessionThread, |
| 181 keys, | 204 keys, |
| 182 &success)); | 205 &success)); |
| 183 if (!success) | 206 if (!success) |
| 184 return kUnknownError; | 207 return kUnknownError; |
| 185 return kSuccess; | 208 return kSuccess; |
| 186 } | 209 } |
| (...skipping 417 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 604 } | 627 } |
| 605 | 628 |
| 606 void Session::SendKeysOnSessionThread(const string16& keys, | 629 void Session::SendKeysOnSessionThread(const string16& keys, |
| 607 bool* success) { | 630 bool* success) { |
| 608 *success = true; | 631 *success = true; |
| 609 std::vector<WebKeyEvent> key_events; | 632 std::vector<WebKeyEvent> key_events; |
| 610 ConvertKeysToWebKeyEvents(keys, &key_events); | 633 ConvertKeysToWebKeyEvents(keys, &key_events); |
| 611 for (size_t i = 0; i < key_events.size(); ++i) { | 634 for (size_t i = 0; i < key_events.size(); ++i) { |
| 612 bool key_success = false; | 635 bool key_success = false; |
| 613 automation_->SendWebKeyEvent( | 636 automation_->SendWebKeyEvent( |
| 614 current_window_id_, key_events[i], &key_success); | 637 current_window_id_, key_events[i], |
| 615 if (!key_success) { | 638 SessionManager::GetInstance()->use_native_events(), &key_success); |
| 639 if (!key_success) { | |
| 616 LOG(ERROR) << "Failed to send key event. Event details:\n" | 640 LOG(ERROR) << "Failed to send key event. Event details:\n" |
| 617 << "Type: " << key_events[i].type << "\n" | 641 << "Type: " << key_events[i].type << "\n" |
| 618 << "KeyCode: " << key_events[i].key_code << "\n" | 642 << "KeyCode: " << key_events[i].key_code << "\n" |
| 619 << "UnmodifiedText: " << key_events[i].unmodified_text << "\n" | 643 << "UnmodifiedText: " << key_events[i].unmodified_text << "\n" |
| 620 << "ModifiedText: " << key_events[i].modified_text << "\n" | 644 << "ModifiedText: " << key_events[i].modified_text << "\n" |
| 621 << "Modifiers: " << key_events[i].modifiers << "\n"; | 645 << "Modifiers: " << key_events[i].modifiers << "\n"; |
| 622 *success = false; | 646 *success = false; |
| 623 } | 647 } |
| 624 } | 648 } |
| 625 } | 649 } |
| (...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 760 } | 784 } |
| 761 } else { | 785 } else { |
| 762 LOG(ERROR) << "Location atom returned non-dictionary type"; | 786 LOG(ERROR) << "Location atom returned non-dictionary type"; |
| 763 code = kUnknownError; | 787 code = kUnknownError; |
| 764 } | 788 } |
| 765 } | 789 } |
| 766 return code; | 790 return code; |
| 767 } | 791 } |
| 768 | 792 |
| 769 } // namespace webdriver | 793 } // namespace webdriver |
| OLD | NEW |