| 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.h" | 5 #include "chrome/test/webdriver/session.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 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 47 : window_id(window_id), | 47 : window_id(window_id), |
| 48 frame_path(frame_path) { | 48 frame_path(frame_path) { |
| 49 } | 49 } |
| 50 | 50 |
| 51 FrameId& FrameId::operator=(const FrameId& other) { | 51 FrameId& FrameId::operator=(const FrameId& other) { |
| 52 window_id = other.window_id; | 52 window_id = other.window_id; |
| 53 frame_path = other.frame_path; | 53 frame_path = other.frame_path; |
| 54 return *this; | 54 return *this; |
| 55 } | 55 } |
| 56 | 56 |
| 57 Session::Session() | 57 Session::Options::Options() |
| 58 : use_native_events(false), |
| 59 load_async(false) { |
| 60 } |
| 61 |
| 62 Session::Options::~Options() { |
| 63 } |
| 64 |
| 65 Session::Session(const Options& options) |
| 58 : id_(GenerateRandomID()), | 66 : id_(GenerateRandomID()), |
| 59 current_target_(FrameId(0, FramePath())), | 67 current_target_(FrameId(0, FramePath())), |
| 60 thread_(id_.c_str()), | 68 thread_(id_.c_str()), |
| 61 async_script_timeout_(0), | 69 async_script_timeout_(0), |
| 62 implicit_wait_(0), | 70 implicit_wait_(0), |
| 63 screenshot_on_error_(false), | 71 has_alert_prompt_text_(false), |
| 64 use_native_events_(false), | 72 options_(options) { |
| 65 has_alert_prompt_text_(false) { | |
| 66 SessionManager::GetInstance()->Add(this); | 73 SessionManager::GetInstance()->Add(this); |
| 67 } | 74 } |
| 68 | 75 |
| 69 Session::~Session() { | 76 Session::~Session() { |
| 70 SessionManager::GetInstance()->Remove(id_); | 77 SessionManager::GetInstance()->Remove(id_); |
| 71 } | 78 } |
| 72 | 79 |
| 73 Error* Session::Init(const FilePath& browser_exe, | 80 Error* Session::Init(const FilePath& browser_exe, |
| 74 const FilePath& user_data_dir, | 81 const FilePath& user_data_dir, |
| 75 const CommandLine& options) { | 82 const CommandLine& options) { |
| 76 if (!thread_.Start()) { | 83 if (!thread_.Start()) { |
| 77 delete this; | 84 delete this; |
| 78 return new Error(kUnknownError, "Cannot start session thread"); | 85 return new Error(kUnknownError, "Cannot start session thread"); |
| 79 } | 86 } |
| 80 | 87 |
| 81 Error* error = NULL; | 88 Error* error = NULL; |
| 82 RunSessionTask(NewRunnableMethod( | 89 RunSessionTask(NewRunnableMethod( |
| 83 this, | 90 this, |
| 84 &Session::InitOnSessionThread, | 91 &Session::InitOnSessionThread, |
| 85 browser_exe, | 92 browser_exe, |
| 86 user_data_dir, | 93 user_data_dir, |
| 87 options, | 94 options, |
| 88 &error)); | 95 &error)); |
| 89 if (error) | 96 if (error) |
| 90 Terminate(); | 97 Terminate(); |
| 91 return error; | 98 return error; |
| 92 } | 99 } |
| 93 | 100 |
| 101 Error* Session::BeforeExecuteCommand() { |
| 102 Error* error = NULL; |
| 103 if (!options_.load_async) { |
| 104 LOG(INFO) << "Waiting for the page to stop loading"; |
| 105 error = WaitForAllTabsToStopLoading(); |
| 106 LOG(INFO) << "Done waiting for the page to stop loading"; |
| 107 } |
| 108 if (!error) { |
| 109 error = SwitchToTopFrameIfCurrentFrameInvalid(); |
| 110 } |
| 111 return error; |
| 112 } |
| 113 |
| 94 void Session::Terminate() { | 114 void Session::Terminate() { |
| 95 RunSessionTask(NewRunnableMethod( | 115 RunSessionTask(NewRunnableMethod( |
| 96 this, | 116 this, |
| 97 &Session::TerminateOnSessionThread)); | 117 &Session::TerminateOnSessionThread)); |
| 98 delete this; | 118 delete this; |
| 99 } | 119 } |
| 100 | 120 |
| 101 Error* Session::ExecuteScript(const FrameId& frame_id, | 121 Error* Session::ExecuteScript(const FrameId& frame_id, |
| 102 const std::string& script, | 122 const std::string& script, |
| 103 const ListValue* const args, | 123 const ListValue* const args, |
| (...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 221 &Automation::DragAndDropFilePaths, | 241 &Automation::DragAndDropFilePaths, |
| 222 current_target_.window_id, | 242 current_target_.window_id, |
| 223 location, | 243 location, |
| 224 paths, | 244 paths, |
| 225 &error)); | 245 &error)); |
| 226 return error; | 246 return error; |
| 227 } | 247 } |
| 228 | 248 |
| 229 Error* Session::NavigateToURL(const std::string& url) { | 249 Error* Session::NavigateToURL(const std::string& url) { |
| 230 Error* error = NULL; | 250 Error* error = NULL; |
| 231 RunSessionTask(NewRunnableMethod( | 251 if (options_.load_async) { |
| 232 automation_.get(), | 252 RunSessionTask(NewRunnableMethod( |
| 233 &Automation::NavigateToURL, | 253 automation_.get(), |
| 234 current_target_.window_id, | 254 &Automation::NavigateToURLAsync, |
| 235 url, | 255 current_target_.window_id, |
| 236 &error)); | 256 url, |
| 257 &error)); |
| 258 } else { |
| 259 RunSessionTask(NewRunnableMethod( |
| 260 automation_.get(), |
| 261 &Automation::NavigateToURL, |
| 262 current_target_.window_id, |
| 263 url, |
| 264 &error)); |
| 265 } |
| 237 return error; | 266 return error; |
| 238 } | 267 } |
| 239 | 268 |
| 240 Error* Session::GoForward() { | 269 Error* Session::GoForward() { |
| 241 Error* error = NULL; | 270 Error* error = NULL; |
| 242 RunSessionTask(NewRunnableMethod( | 271 RunSessionTask(NewRunnableMethod( |
| 243 automation_.get(), | 272 automation_.get(), |
| 244 &Automation::GoForward, | 273 &Automation::GoForward, |
| 245 current_target_.window_id, | 274 current_target_.window_id, |
| 246 &error)); | 275 &error)); |
| (...skipping 784 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1031 } | 1060 } |
| 1032 | 1061 |
| 1033 void Session::set_implicit_wait(int timeout_ms) { | 1062 void Session::set_implicit_wait(int timeout_ms) { |
| 1034 implicit_wait_ = timeout_ms; | 1063 implicit_wait_ = timeout_ms; |
| 1035 } | 1064 } |
| 1036 | 1065 |
| 1037 int Session::implicit_wait() const { | 1066 int Session::implicit_wait() const { |
| 1038 return implicit_wait_; | 1067 return implicit_wait_; |
| 1039 } | 1068 } |
| 1040 | 1069 |
| 1041 void Session::set_screenshot_on_error(bool error) { | |
| 1042 screenshot_on_error_ = error; | |
| 1043 } | |
| 1044 | |
| 1045 bool Session::screenshot_on_error() const { | |
| 1046 return screenshot_on_error_; | |
| 1047 } | |
| 1048 | |
| 1049 void Session::set_use_native_events(bool use_native_events) { | |
| 1050 use_native_events_ = use_native_events; | |
| 1051 } | |
| 1052 | |
| 1053 bool Session::use_native_events() const { | |
| 1054 return use_native_events_; | |
| 1055 } | |
| 1056 | |
| 1057 const gfx::Point& Session::get_mouse_position() const { | 1070 const gfx::Point& Session::get_mouse_position() const { |
| 1058 return mouse_position_; | 1071 return mouse_position_; |
| 1059 } | 1072 } |
| 1060 | 1073 |
| 1074 const Session::Options& Session::options() const { |
| 1075 return options_; |
| 1076 } |
| 1077 |
| 1061 void Session::RunSessionTask(Task* task) { | 1078 void Session::RunSessionTask(Task* task) { |
| 1062 base::WaitableEvent done_event(false, false); | 1079 base::WaitableEvent done_event(false, false); |
| 1063 thread_.message_loop_proxy()->PostTask(FROM_HERE, NewRunnableMethod( | 1080 thread_.message_loop_proxy()->PostTask(FROM_HERE, NewRunnableMethod( |
| 1064 this, | 1081 this, |
| 1065 &Session::RunSessionTaskOnSessionThread, | 1082 &Session::RunSessionTaskOnSessionThread, |
| 1066 task, | 1083 task, |
| 1067 &done_event)); | 1084 &done_event)); |
| 1068 done_event.Wait(); | 1085 done_event.Wait(); |
| 1069 } | 1086 } |
| 1070 | 1087 |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1158 } | 1175 } |
| 1159 | 1176 |
| 1160 void Session::SendKeysOnSessionThread(const string16& keys, Error** error) { | 1177 void Session::SendKeysOnSessionThread(const string16& keys, Error** error) { |
| 1161 std::vector<WebKeyEvent> key_events; | 1178 std::vector<WebKeyEvent> key_events; |
| 1162 std::string error_msg; | 1179 std::string error_msg; |
| 1163 if (!ConvertKeysToWebKeyEvents(keys, &key_events, &error_msg)) { | 1180 if (!ConvertKeysToWebKeyEvents(keys, &key_events, &error_msg)) { |
| 1164 *error = new Error(kUnknownError, error_msg); | 1181 *error = new Error(kUnknownError, error_msg); |
| 1165 return; | 1182 return; |
| 1166 } | 1183 } |
| 1167 for (size_t i = 0; i < key_events.size(); ++i) { | 1184 for (size_t i = 0; i < key_events.size(); ++i) { |
| 1168 if (use_native_events_) { | 1185 if (options_.use_native_events) { |
| 1169 // The automation provider will generate up/down events for us, we | 1186 // The automation provider will generate up/down events for us, we |
| 1170 // only need to call it once as compared to the WebKeyEvent method. | 1187 // only need to call it once as compared to the WebKeyEvent method. |
| 1171 // Hence we filter events by their types, keeping only rawkeydown. | 1188 // Hence we filter events by their types, keeping only rawkeydown. |
| 1172 if (key_events[i].type != automation::kRawKeyDownType) | 1189 if (key_events[i].type != automation::kRawKeyDownType) |
| 1173 continue; | 1190 continue; |
| 1174 automation_->SendNativeKeyEvent( | 1191 automation_->SendNativeKeyEvent( |
| 1175 current_target_.window_id, | 1192 current_target_.window_id, |
| 1176 key_events[i].key_code, | 1193 key_events[i].key_code, |
| 1177 key_events[i].modifiers, | 1194 key_events[i].modifiers, |
| 1178 error); | 1195 error); |
| (...skipping 263 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1442 path, | 1459 path, |
| 1443 &error)); | 1460 &error)); |
| 1444 if (error) | 1461 if (error) |
| 1445 return error; | 1462 return error; |
| 1446 if (!file_util::ReadFileToString(path, png)) | 1463 if (!file_util::ReadFileToString(path, png)) |
| 1447 return new Error(kUnknownError, "Could not read screenshot file"); | 1464 return new Error(kUnknownError, "Could not read screenshot file"); |
| 1448 return NULL; | 1465 return NULL; |
| 1449 } | 1466 } |
| 1450 | 1467 |
| 1451 } // namespace webdriver | 1468 } // namespace webdriver |
| OLD | NEW |