| 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 29 matching lines...) Expand all Loading... |
| 40 #include "third_party/webdriver/atoms.h" | 40 #include "third_party/webdriver/atoms.h" |
| 41 #include "ui/gfx/point.h" | 41 #include "ui/gfx/point.h" |
| 42 | 42 |
| 43 namespace webdriver { | 43 namespace webdriver { |
| 44 | 44 |
| 45 Session::Session() | 45 Session::Session() |
| 46 : id_(GenerateRandomID()), | 46 : id_(GenerateRandomID()), |
| 47 thread_(id_.c_str()), | 47 thread_(id_.c_str()), |
| 48 implicit_wait_(0), | 48 implicit_wait_(0), |
| 49 current_frame_xpath_(""), | 49 current_frame_xpath_(""), |
| 50 current_window_id_(0) { | 50 current_window_id_(0), |
| 51 use_native_events_(false) { |
| 51 SessionManager::GetInstance()->Add(this); | 52 SessionManager::GetInstance()->Add(this); |
| 52 } | 53 } |
| 53 | 54 |
| 54 Session::~Session() { | 55 Session::~Session() { |
| 55 SessionManager::GetInstance()->Remove(id_); | 56 SessionManager::GetInstance()->Remove(id_); |
| 56 } | 57 } |
| 57 | 58 |
| 58 bool Session::Init(const FilePath& browser_dir) { | 59 bool Session::Init(const FilePath& browser_dir) { |
| 59 bool success = false; | 60 bool success = false; |
| 60 if (thread_.Start()) { | 61 if (thread_.Start()) { |
| (...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 156 } | 157 } |
| 157 | 158 |
| 158 ErrorCode Session::ExecuteScript(const std::string& script, | 159 ErrorCode Session::ExecuteScript(const std::string& script, |
| 159 const ListValue* const args, | 160 const ListValue* const args, |
| 160 Value** value) { | 161 Value** value) { |
| 161 return ExecuteScript( | 162 return ExecuteScript( |
| 162 current_window_id_, current_frame_xpath_, script, args, value); | 163 current_window_id_, current_frame_xpath_, script, args, value); |
| 163 } | 164 } |
| 164 | 165 |
| 165 ErrorCode Session::SendKeys(const WebElementId& element, const string16& keys) { | 166 ErrorCode Session::SendKeys(const WebElementId& element, const string16& keys) { |
| 167 // This method will first check if the element we want to send the keys to is |
| 168 // already focused, if not it will try to focus on it first. |
| 166 ListValue args; | 169 ListValue args; |
| 167 args.Append(element.ToValue()); | 170 args.Append(element.ToValue()); |
| 168 // TODO(jleyba): Update this to use the correct atom. | 171 // TODO(jleyba): Update this to use the correct atom. |
| 169 std::string script = "document.activeElement.blur();arguments[0].focus();"; | 172 std::string script = "if(document.activeElement!=arguments[0]){" |
| 173 " if(document.activeElement)" |
| 174 " document.activeElement.blur();" |
| 175 " arguments[0].focus();" |
| 176 "}"; |
| 170 Value* unscoped_result = NULL; | 177 Value* unscoped_result = NULL; |
| 171 ErrorCode code = ExecuteScript(script, &args, &unscoped_result); | 178 ErrorCode code = ExecuteScript(script, &args, &unscoped_result); |
| 172 scoped_ptr<Value> result(unscoped_result); | |
| 173 if (code != kSuccess) { | 179 if (code != kSuccess) { |
| 174 LOG(ERROR) << "Failed to focus element before sending keys"; | 180 LOG(ERROR) << "Failed to get or set focus element before sending keys"; |
| 175 return code; | 181 return code; |
| 176 } | 182 } |
| 177 | |
| 178 bool success = false; | 183 bool success = false; |
| 179 RunSessionTask(NewRunnableMethod( | 184 RunSessionTask(NewRunnableMethod( |
| 180 this, | 185 this, |
| 181 &Session::SendKeysOnSessionThread, | 186 &Session::SendKeysOnSessionThread, |
| 182 keys, | 187 keys, |
| 183 &success)); | 188 &success)); |
| 184 if (!success) | 189 if (!success) |
| 185 return kUnknownError; | 190 return kUnknownError; |
| 186 return kSuccess; | 191 return kSuccess; |
| 187 } | 192 } |
| (...skipping 428 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 616 } | 621 } |
| 617 | 622 |
| 618 void Session::SendKeysOnSessionThread(const string16& keys, | 623 void Session::SendKeysOnSessionThread(const string16& keys, |
| 619 bool* success) { | 624 bool* success) { |
| 620 *success = true; | 625 *success = true; |
| 621 std::vector<WebKeyEvent> key_events; | 626 std::vector<WebKeyEvent> key_events; |
| 622 ConvertKeysToWebKeyEvents(keys, &key_events); | 627 ConvertKeysToWebKeyEvents(keys, &key_events); |
| 623 for (size_t i = 0; i < key_events.size(); ++i) { | 628 for (size_t i = 0; i < key_events.size(); ++i) { |
| 624 bool key_success = false; | 629 bool key_success = false; |
| 625 automation_->SendWebKeyEvent( | 630 automation_->SendWebKeyEvent( |
| 626 current_window_id_, key_events[i], &key_success); | 631 current_window_id_, key_events[i], use_native_events_, &key_success); |
| 627 if (!key_success) { | 632 if (!key_success) { |
| 628 LOG(ERROR) << "Failed to send key event. Event details:\n" | 633 LOG(ERROR) << "Failed to send key event. Event details:\n" |
| 629 << "Type: " << key_events[i].type << "\n" | 634 << "Type: " << key_events[i].type << "\n" |
| 630 << "KeyCode: " << key_events[i].key_code << "\n" | 635 << "KeyCode: " << key_events[i].key_code << "\n" |
| 631 << "UnmodifiedText: " << key_events[i].unmodified_text << "\n" | 636 << "UnmodifiedText: " << key_events[i].unmodified_text << "\n" |
| 632 << "ModifiedText: " << key_events[i].modified_text << "\n" | 637 << "ModifiedText: " << key_events[i].modified_text << "\n" |
| 633 << "Modifiers: " << key_events[i].modifiers << "\n"; | 638 << "Modifiers: " << key_events[i].modifiers << "\n" |
| 634 *success = false; | 639 << "Was a native event: " << use_native_events_ << "\n"; |
| 640 *success = false; |
| 635 } | 641 } |
| 636 } | 642 } |
| 637 } | 643 } |
| 638 | 644 |
| 639 ErrorCode Session::SwitchToFrameWithJavaScriptLocatedFrame( | 645 ErrorCode Session::SwitchToFrameWithJavaScriptLocatedFrame( |
| 640 const std::string& script, | 646 const std::string& script, |
| 641 ListValue* args) { | 647 ListValue* args) { |
| 642 Value* unscoped_result = NULL; | 648 Value* unscoped_result = NULL; |
| 643 ErrorCode code = ExecuteScript(script, args, &unscoped_result); | 649 ErrorCode code = ExecuteScript(script, args, &unscoped_result); |
| 644 scoped_ptr<Value> result(unscoped_result); | 650 scoped_ptr<Value> result(unscoped_result); |
| (...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 772 } | 778 } |
| 773 } else { | 779 } else { |
| 774 LOG(ERROR) << "Location atom returned non-dictionary type"; | 780 LOG(ERROR) << "Location atom returned non-dictionary type"; |
| 775 code = kUnknownError; | 781 code = kUnknownError; |
| 776 } | 782 } |
| 777 } | 783 } |
| 778 return code; | 784 return code; |
| 779 } | 785 } |
| 780 | 786 |
| 781 } // namespace webdriver | 787 } // namespace webdriver |
| OLD | NEW |