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 <signal.h> | 5 #include <signal.h> |
6 #include <stdlib.h> | 6 #include <stdlib.h> |
7 | 7 |
8 #if defined(OS_WIN) | 8 #if defined(OS_WIN) |
9 #include <windows.h> | 9 #include <windows.h> |
10 #endif | 10 #endif |
11 | 11 |
12 #include <iostream> | 12 #include <iostream> |
13 #include <fstream> | 13 #include <fstream> |
14 | 14 |
15 #include "base/at_exit.h" | 15 #include "base/at_exit.h" |
16 #include "base/command_line.h" | 16 #include "base/command_line.h" |
17 #include "base/file_path.h" | 17 #include "base/file_path.h" |
18 #include "base/file_util.h" | 18 #include "base/file_util.h" |
19 #include "base/logging.h" | 19 #include "base/logging.h" |
20 #include "base/memory/scoped_ptr.h" | 20 #include "base/memory/scoped_ptr.h" |
21 #include "base/string_number_conversions.h" | 21 #include "base/string_number_conversions.h" |
22 #include "base/string_split.h" | 22 #include "base/string_split.h" |
23 #include "base/string_util.h" | 23 #include "base/string_util.h" |
24 #include "base/synchronization/waitable_event.h" | 24 #include "base/synchronization/waitable_event.h" |
25 #include "base/test/test_timeouts.h" | 25 #include "base/test/test_timeouts.h" |
26 #include "base/threading/platform_thread.h" | 26 #include "base/threading/platform_thread.h" |
27 #include "base/utf_string_conversions.h" | 27 #include "base/utf_string_conversions.h" |
28 #include "chrome/common/chrome_paths.h" | 28 #include "chrome/common/chrome_paths.h" |
29 #include "chrome/common/chrome_switches.h" | 29 #include "chrome/common/chrome_switches.h" |
| 30 #include "chrome/test/webdriver/dispatch.h" |
| 31 #include "chrome/test/webdriver/session_manager.h" |
| 32 #include "chrome/test/webdriver/utility_functions.h" |
30 #include "chrome/test/webdriver/commands/alert_commands.h" | 33 #include "chrome/test/webdriver/commands/alert_commands.h" |
31 #include "chrome/test/webdriver/commands/cookie_commands.h" | 34 #include "chrome/test/webdriver/commands/cookie_commands.h" |
32 #include "chrome/test/webdriver/commands/create_session.h" | 35 #include "chrome/test/webdriver/commands/create_session.h" |
33 #include "chrome/test/webdriver/commands/execute_async_script_command.h" | 36 #include "chrome/test/webdriver/commands/execute_async_script_command.h" |
34 #include "chrome/test/webdriver/commands/execute_command.h" | 37 #include "chrome/test/webdriver/commands/execute_command.h" |
35 #include "chrome/test/webdriver/commands/find_element_commands.h" | 38 #include "chrome/test/webdriver/commands/find_element_commands.h" |
36 #include "chrome/test/webdriver/commands/navigate_commands.h" | 39 #include "chrome/test/webdriver/commands/navigate_commands.h" |
37 #include "chrome/test/webdriver/commands/mouse_commands.h" | 40 #include "chrome/test/webdriver/commands/mouse_commands.h" |
38 #include "chrome/test/webdriver/commands/screenshot_command.h" | 41 #include "chrome/test/webdriver/commands/screenshot_command.h" |
39 #include "chrome/test/webdriver/commands/session_with_id.h" | 42 #include "chrome/test/webdriver/commands/session_with_id.h" |
40 #include "chrome/test/webdriver/commands/set_timeout_commands.h" | 43 #include "chrome/test/webdriver/commands/set_timeout_commands.h" |
41 #include "chrome/test/webdriver/commands/source_command.h" | 44 #include "chrome/test/webdriver/commands/source_command.h" |
42 #include "chrome/test/webdriver/commands/target_locator_commands.h" | 45 #include "chrome/test/webdriver/commands/target_locator_commands.h" |
43 #include "chrome/test/webdriver/commands/title_command.h" | 46 #include "chrome/test/webdriver/commands/title_command.h" |
44 #include "chrome/test/webdriver/commands/url_command.h" | 47 #include "chrome/test/webdriver/commands/url_command.h" |
45 #include "chrome/test/webdriver/commands/webelement_commands.h" | 48 #include "chrome/test/webdriver/commands/webelement_commands.h" |
46 #include "chrome/test/webdriver/dispatch.h" | |
47 #include "chrome/test/webdriver/session_manager.h" | |
48 #include "chrome/test/webdriver/utility_functions.h" | |
49 #include "chrome/test/webdriver/webdriver_logging.h" | |
50 #include "third_party/mongoose/mongoose.h" | 49 #include "third_party/mongoose/mongoose.h" |
51 | 50 |
52 #if defined(OS_WIN) | 51 #if defined(OS_WIN) |
53 #include <time.h> | 52 #include <time.h> |
54 #elif defined(OS_POSIX) | 53 #elif defined(OS_POSIX) |
55 #include <errno.h> | 54 #include <errno.h> |
56 #include <sys/time.h> | 55 #include <sys/time.h> |
57 #include <sys/types.h> | 56 #include <sys/types.h> |
58 #include <sys/wait.h> | 57 #include <sys/wait.h> |
59 #endif | 58 #endif |
60 | 59 |
61 namespace webdriver { | 60 namespace webdriver { |
62 | 61 |
63 void InitCallbacks(struct mg_context* ctx, Dispatcher* dispatcher, | 62 void InitCallbacks(struct mg_context* ctx, Dispatcher* dispatcher, |
64 base::WaitableEvent* shutdown_event, | 63 base::WaitableEvent* shutdown_event, |
65 bool forbid_other_requests) { | 64 bool forbid_other_requests) { |
66 dispatcher->AddShutdown("/shutdown", shutdown_event); | 65 dispatcher->AddShutdown("/shutdown", shutdown_event); |
67 dispatcher->AddHealthz("/healthz"); | 66 dispatcher->AddStatus("/healthz"); |
68 dispatcher->AddLog("/log"); | |
69 | 67 |
70 dispatcher->Add<CreateSession>("/session"); | 68 dispatcher->Add<CreateSession>("/session"); |
71 | 69 |
72 // WebElement commands | 70 // WebElement commands |
73 dispatcher->Add<FindOneElementCommand>( "/session/*/element"); | 71 dispatcher->Add<FindOneElementCommand>( "/session/*/element"); |
74 dispatcher->Add<FindManyElementsCommand>("/session/*/elements"); | 72 dispatcher->Add<FindManyElementsCommand>("/session/*/elements"); |
75 dispatcher->Add<ActiveElementCommand>( "/session/*/element/active"); | 73 dispatcher->Add<ActiveElementCommand>( "/session/*/element/active"); |
76 dispatcher->Add<FindOneElementCommand>( "/session/*/element/*/element"); | 74 dispatcher->Add<FindOneElementCommand>( "/session/*/element/*/element"); |
77 dispatcher->Add<FindManyElementsCommand>("/session/*/elements/*/elements"); | 75 dispatcher->Add<FindManyElementsCommand>("/session/*/elements/*/elements"); |
78 dispatcher->Add<ElementAttributeCommand>("/session/*/element/*/attribute/*"); | 76 dispatcher->Add<ElementAttributeCommand>("/session/*/element/*/attribute/*"); |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
138 // Since the /session/* is a wild card that would match the above URIs, this | 136 // Since the /session/* is a wild card that would match the above URIs, this |
139 // line MUST be after all other webdriver command callbacks. | 137 // line MUST be after all other webdriver command callbacks. |
140 dispatcher->Add<SessionWithID>("/session/*"); | 138 dispatcher->Add<SessionWithID>("/session/*"); |
141 | 139 |
142 if (forbid_other_requests) | 140 if (forbid_other_requests) |
143 dispatcher->ForbidAllOtherRequests(); | 141 dispatcher->ForbidAllOtherRequests(); |
144 } | 142 } |
145 | 143 |
146 } // namespace webdriver | 144 } // namespace webdriver |
147 | 145 |
| 146 // Initializes logging for ChromeDriver. |
| 147 void InitChromeDriverLogging(const CommandLine& command_line) { |
| 148 bool success = InitLogging( |
| 149 FILE_PATH_LITERAL("chromedriver.log"), |
| 150 logging::LOG_TO_BOTH_FILE_AND_SYSTEM_DEBUG_LOG, |
| 151 logging::LOCK_LOG_FILE, |
| 152 logging::DELETE_OLD_LOG_FILE, |
| 153 logging::DISABLE_DCHECK_FOR_NON_OFFICIAL_RELEASE_BUILDS); |
| 154 if (!success) { |
| 155 PLOG(ERROR) << "Unable to initialize logging"; |
| 156 } |
| 157 logging::SetLogItems(false, // enable_process_id |
| 158 false, // enable_thread_id |
| 159 true, // enable_timestamp |
| 160 false); // enable_tickcount |
| 161 if (command_line.HasSwitch(switches::kLoggingLevel)) { |
| 162 std::string log_level = command_line.GetSwitchValueASCII( |
| 163 switches::kLoggingLevel); |
| 164 int level = 0; |
| 165 if (base::StringToInt(log_level, &level)) { |
| 166 logging::SetMinLogLevel(level); |
| 167 } else { |
| 168 LOG(WARNING) << "Bad log level: " << log_level; |
| 169 } |
| 170 } |
| 171 } |
| 172 |
148 // Configures mongoose according to the given command line flags. | 173 // Configures mongoose according to the given command line flags. |
149 // Returns true on success. | 174 // Returns true on success. |
150 bool SetMongooseOptions(struct mg_context* ctx, | 175 bool SetMongooseOptions(struct mg_context* ctx, |
151 const std::string& port, | 176 const std::string& port, |
152 const std::string& root) { | 177 const std::string& root) { |
153 if (!mg_set_option(ctx, "ports", port.c_str())) { | 178 if (!mg_set_option(ctx, "ports", port.c_str())) { |
154 std::cout << "ChromeDriver cannot bind to port (" | 179 std::cout << "ChromeDriver cannot bind to port (" |
155 << port.c_str() << ")" << std::endl; | 180 << port.c_str() << ")" << std::endl; |
156 return false; | 181 return false; |
157 } | 182 } |
(...skipping 19 matching lines...) Expand all Loading... |
177 | 202 |
178 #if defined(OS_POSIX) | 203 #if defined(OS_POSIX) |
179 signal(SIGPIPE, SIG_IGN); | 204 signal(SIGPIPE, SIG_IGN); |
180 #endif | 205 #endif |
181 srand((unsigned int)time(NULL)); | 206 srand((unsigned int)time(NULL)); |
182 | 207 |
183 // Register Chrome's path provider so that the AutomationProxy will find our | 208 // Register Chrome's path provider so that the AutomationProxy will find our |
184 // built Chrome. | 209 // built Chrome. |
185 chrome::RegisterPathProvider(); | 210 chrome::RegisterPathProvider(); |
186 TestTimeouts::Initialize(); | 211 TestTimeouts::Initialize(); |
| 212 InitChromeDriverLogging(*cmd_line); |
187 | 213 |
188 // Parse command line flags. | 214 // Parse command line flags. |
189 std::string port = "9515"; | 215 std::string port = "9515"; |
190 std::string root; | 216 std::string root; |
191 std::string url_base; | 217 std::string url_base; |
192 bool verbose = false; | |
193 if (cmd_line->HasSwitch("port")) | 218 if (cmd_line->HasSwitch("port")) |
194 port = cmd_line->GetSwitchValueASCII("port"); | 219 port = cmd_line->GetSwitchValueASCII("port"); |
195 // The 'root' flag allows the user to specify a location to serve files from. | 220 // The 'root' flag allows the user to specify a location to serve files from. |
196 // If it is not given, a callback will be registered to forbid all file | 221 // If it is not given, a callback will be registered to forbid all file |
197 // requests. | 222 // requests. |
198 if (cmd_line->HasSwitch("root")) | 223 if (cmd_line->HasSwitch("root")) |
199 root = cmd_line->GetSwitchValueASCII("root"); | 224 root = cmd_line->GetSwitchValueASCII("root"); |
200 if (cmd_line->HasSwitch("url-base")) | 225 if (cmd_line->HasSwitch("url-base")) |
201 url_base = cmd_line->GetSwitchValueASCII("url-base"); | 226 url_base = cmd_line->GetSwitchValueASCII("url-base"); |
202 // Whether or not to do verbose logging. | |
203 if (cmd_line->HasSwitch("verbose")) | |
204 verbose = true; | |
205 | |
206 webdriver::InitWebDriverLogging( | |
207 verbose ? logging::LOG_INFO : logging::LOG_WARNING); | |
208 | 227 |
209 webdriver::SessionManager* manager = webdriver::SessionManager::GetInstance(); | 228 webdriver::SessionManager* manager = webdriver::SessionManager::GetInstance(); |
210 manager->set_port(port); | 229 manager->set_port(port); |
211 manager->set_url_base(url_base); | 230 manager->set_url_base(url_base); |
212 | 231 |
213 // Initialize SHTTPD context. | 232 // Initialize SHTTPD context. |
214 // Listen on port 9515 or port specified on command line. | 233 // Listen on port 9515 or port specified on command line. |
215 // TODO(jmikhail) Maybe add port 9516 as a secure connection. | 234 // TODO(jmikhail) Maybe add port 9516 as a secure connection. |
216 ctx = mg_start(); | 235 ctx = mg_start(); |
217 if (!SetMongooseOptions(ctx, port, root)) { | 236 if (!SetMongooseOptions(ctx, port, root)) { |
(...skipping 19 matching lines...) Expand all Loading... |
237 | 256 |
238 // Run until we receive command to shutdown. | 257 // Run until we receive command to shutdown. |
239 shutdown_event.Wait(); | 258 shutdown_event.Wait(); |
240 | 259 |
241 // We should not reach here since the service should never quit. | 260 // We should not reach here since the service should never quit. |
242 // TODO(jmikhail): register a listener for SIGTERM and break the | 261 // TODO(jmikhail): register a listener for SIGTERM and break the |
243 // message loop gracefully. | 262 // message loop gracefully. |
244 mg_stop(ctx); | 263 mg_stop(ctx); |
245 return (EXIT_SUCCESS); | 264 return (EXIT_SUCCESS); |
246 } | 265 } |
OLD | NEW |