| 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 28 matching lines...) Expand all Loading... |
| 39 #include "third_party/webdriver/atoms.h" | 39 #include "third_party/webdriver/atoms.h" |
| 40 #include "ui/gfx/point.h" | 40 #include "ui/gfx/point.h" |
| 41 | 41 |
| 42 namespace webdriver { | 42 namespace webdriver { |
| 43 | 43 |
| 44 Session::Session() | 44 Session::Session() |
| 45 : id_(GenerateRandomID()), | 45 : id_(GenerateRandomID()), |
| 46 thread_(id_.c_str()), | 46 thread_(id_.c_str()), |
| 47 implicit_wait_(0), | 47 implicit_wait_(0), |
| 48 current_frame_xpath_(""), | 48 current_frame_xpath_(""), |
| 49 current_window_id_(0) { | 49 current_window_id_(0), |
| 50 use_native_events_(false) { |
| 50 SessionManager::GetInstance()->Add(this); | 51 SessionManager::GetInstance()->Add(this); |
| 51 } | 52 } |
| 52 | 53 |
| 53 Session::~Session() { | 54 Session::~Session() { |
| 54 SessionManager::GetInstance()->Remove(id_); | 55 SessionManager::GetInstance()->Remove(id_); |
| 55 } | 56 } |
| 56 | 57 |
| 57 bool Session::Init(const FilePath& browser_dir) { | 58 bool Session::Init(const FilePath& browser_dir) { |
| 58 bool success = false; | 59 bool success = false; |
| 59 if (thread_.Start()) { | 60 if (thread_.Start()) { |
| (...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 155 } | 156 } |
| 156 | 157 |
| 157 ErrorCode Session::ExecuteScript(const std::string& script, | 158 ErrorCode Session::ExecuteScript(const std::string& script, |
| 158 const ListValue* const args, | 159 const ListValue* const args, |
| 159 Value** value) { | 160 Value** value) { |
| 160 return ExecuteScript( | 161 return ExecuteScript( |
| 161 current_window_id_, current_frame_xpath_, script, args, value); | 162 current_window_id_, current_frame_xpath_, script, args, value); |
| 162 } | 163 } |
| 163 | 164 |
| 164 ErrorCode Session::SendKeys(const WebElementId& element, const string16& keys) { | 165 ErrorCode Session::SendKeys(const WebElementId& element, const string16& keys) { |
| 165 ListValue args; | 166 // This method will first check if the element we want to send the keys to is |
| 166 args.Append(element.ToValue()); | 167 // already focused, if not it will try to focus on it first. |
| 168 scoped_ptr<ListValue> args(new ListValue); |
| 169 args->Append(element.ToValue()); |
| 167 // TODO(jleyba): Update this to use the correct atom. | 170 // TODO(jleyba): Update this to use the correct atom. |
| 168 std::string script = "document.activeElement.blur();arguments[0].focus();"; | 171 std::string script = "if(document.activeElement!=arguments[0])" |
| 172 "{if(document.activeElement)" |
| 173 "document.activeElement.blur();arguments[0].focus();}"; |
| 169 Value* unscoped_result = NULL; | 174 Value* unscoped_result = NULL; |
| 170 ErrorCode code = ExecuteScript(script, &args, &unscoped_result); | 175 ErrorCode code = ExecuteScript(script, args.get(), &unscoped_result); |
| 171 scoped_ptr<Value> result(unscoped_result); | |
| 172 if (code != kSuccess) { | 176 if (code != kSuccess) { |
| 173 LOG(ERROR) << "Failed to focus element before sending keys"; | 177 LOG(ERROR) << "Failed to get or set focus element before sending keys"; |
| 174 return code; | 178 return code; |
| 175 } | 179 } |
| 176 | |
| 177 bool success = false; | 180 bool success = false; |
| 178 RunSessionTask(NewRunnableMethod( | 181 RunSessionTask(NewRunnableMethod( |
| 179 this, | 182 this, |
| 180 &Session::SendKeysOnSessionThread, | 183 &Session::SendKeysOnSessionThread, |
| 181 keys, | 184 keys, |
| 182 &success)); | 185 &success)); |
| 183 if (!success) | 186 if (!success) |
| 184 return kUnknownError; | 187 return kUnknownError; |
| 185 return kSuccess; | 188 return kSuccess; |
| 186 } | 189 } |
| (...skipping 417 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 604 } | 607 } |
| 605 | 608 |
| 606 void Session::SendKeysOnSessionThread(const string16& keys, | 609 void Session::SendKeysOnSessionThread(const string16& keys, |
| 607 bool* success) { | 610 bool* success) { |
| 608 *success = true; | 611 *success = true; |
| 609 std::vector<WebKeyEvent> key_events; | 612 std::vector<WebKeyEvent> key_events; |
| 610 ConvertKeysToWebKeyEvents(keys, &key_events); | 613 ConvertKeysToWebKeyEvents(keys, &key_events); |
| 611 for (size_t i = 0; i < key_events.size(); ++i) { | 614 for (size_t i = 0; i < key_events.size(); ++i) { |
| 612 bool key_success = false; | 615 bool key_success = false; |
| 613 automation_->SendWebKeyEvent( | 616 automation_->SendWebKeyEvent( |
| 614 current_window_id_, key_events[i], &key_success); | 617 current_window_id_, key_events[i], use_native_events_, &key_success); |
| 615 if (!key_success) { | 618 if (!key_success) { |
| 616 LOG(ERROR) << "Failed to send key event. Event details:\n" | 619 LOG(ERROR) << "Failed to send key event. Event details:\n" |
| 617 << "Type: " << key_events[i].type << "\n" | 620 << "Type: " << key_events[i].type << "\n" |
| 618 << "KeyCode: " << key_events[i].key_code << "\n" | 621 << "KeyCode: " << key_events[i].key_code << "\n" |
| 619 << "UnmodifiedText: " << key_events[i].unmodified_text << "\n" | 622 << "UnmodifiedText: " << key_events[i].unmodified_text << "\n" |
| 620 << "ModifiedText: " << key_events[i].modified_text << "\n" | 623 << "ModifiedText: " << key_events[i].modified_text << "\n" |
| 621 << "Modifiers: " << key_events[i].modifiers << "\n"; | 624 << "Modifiers: " << key_events[i].modifiers << "\n" |
| 622 *success = false; | 625 << "Was a native event: " << use_native_events_ << "\n"; |
| 626 *success = false; |
| 623 } | 627 } |
| 624 } | 628 } |
| 625 } | 629 } |
| 626 | 630 |
| 627 ErrorCode Session::SwitchToFrameWithJavaScriptLocatedFrame( | 631 ErrorCode Session::SwitchToFrameWithJavaScriptLocatedFrame( |
| 628 const std::string& script, | 632 const std::string& script, |
| 629 ListValue* args) { | 633 ListValue* args) { |
| 630 Value* unscoped_result = NULL; | 634 Value* unscoped_result = NULL; |
| 631 ErrorCode code = ExecuteScript(script, args, &unscoped_result); | 635 ErrorCode code = ExecuteScript(script, args, &unscoped_result); |
| 632 scoped_ptr<Value> result(unscoped_result); | 636 scoped_ptr<Value> result(unscoped_result); |
| (...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 760 } | 764 } |
| 761 } else { | 765 } else { |
| 762 LOG(ERROR) << "Location atom returned non-dictionary type"; | 766 LOG(ERROR) << "Location atom returned non-dictionary type"; |
| 763 code = kUnknownError; | 767 code = kUnknownError; |
| 764 } | 768 } |
| 765 } | 769 } |
| 766 return code; | 770 return code; |
| 767 } | 771 } |
| 768 | 772 |
| 769 } // namespace webdriver | 773 } // namespace webdriver |
| OLD | NEW |