Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(29)

Side by Side Diff: chrome/test/webdriver/automation.cc

Issue 7634031: Let pyauto create an attached webdriver instance to manipulate web pages. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: forgot to re-add the previously added files Created 9 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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/automation.h" 5 #include "chrome/test/webdriver/automation.h"
6 6
7 #if defined(OS_WIN) 7 #if defined(OS_WIN)
8 #include <windows.h> 8 #include <windows.h>
9 #endif 9 #endif
10 10
11 #include "base/base_paths.h" 11 #include "base/base_paths.h"
12 #include "base/basictypes.h" 12 #include "base/basictypes.h"
13 #include "base/callback.h" 13 #include "base/callback.h"
14 #include "base/command_line.h"
15 #include "base/environment.h" 14 #include "base/environment.h"
16 #include "base/file_path.h" 15 #include "base/file_path.h"
17 #include "base/file_util.h" 16 #include "base/file_util.h"
18 #include "base/json/json_writer.h" 17 #include "base/json/json_writer.h"
19 #include "base/logging.h" 18 #include "base/logging.h"
20 #include "base/memory/ref_counted.h" 19 #include "base/memory/ref_counted.h"
21 #include "base/path_service.h" 20 #include "base/path_service.h"
22 #include "base/string_number_conversions.h" 21 #include "base/string_number_conversions.h"
23 #include "base/string_split.h" 22 #include "base/string_split.h"
24 #include "base/stringprintf.h" 23 #include "base/stringprintf.h"
(...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after
177 // Step 3: For each browser exe path, check each location to see if the 176 // Step 3: For each browser exe path, check each location to see if the
178 // browser is installed there. Check the chromium locations lastly. 177 // browser is installed there. Check the chromium locations lastly.
179 return CheckForChromeExe(browser_exes, locations, browser_exe) || 178 return CheckForChromeExe(browser_exes, locations, browser_exe) ||
180 CheckForChromeExe(browser_exes, chromium_locations, browser_exe); 179 CheckForChromeExe(browser_exes, chromium_locations, browser_exe);
181 } 180 }
182 181
183 } // namespace 182 } // namespace
184 183
185 namespace webdriver { 184 namespace webdriver {
186 185
186 Automation::BrowserOptions::BrowserOptions()
187 : command(CommandLine::NO_PROGRAM) {}
188
189 Automation::BrowserOptions::~BrowserOptions() {}
190
187 Automation::Automation() {} 191 Automation::Automation() {}
188 192
189 Automation::~Automation() {} 193 Automation::~Automation() {}
190 194
191 void Automation::Init(const CommandLine& options, 195 void Automation::Init(const BrowserOptions& options, Error** error) {
192 const FilePath& user_data_dir, 196 // Prepare Chrome's command line.
193 Error** error) { 197 CommandLine command(CommandLine::NO_PROGRAM);
194 FilePath browser_exe;
195 if (!GetDefaultChromeExe(&browser_exe)) {
196 *error = new Error(kUnknownError, "Could not find default Chrome binary");
197 return;
198 }
199
200 InitWithBrowserPath(browser_exe, user_data_dir, options, error);
201 }
202
203 void Automation::InitWithBrowserPath(const FilePath& browser_exe,
204 const FilePath& user_data_dir,
205 const CommandLine& options,
206 Error** error) {
207 if (!file_util::PathExists(browser_exe)) {
208 std::string message = base::StringPrintf(
209 "Could not find Chrome binary at: %" PRFilePath,
210 browser_exe.value().c_str());
211 *error = new Error(kUnknownError, message);
212 return;
213 }
214
215 CommandLine command(browser_exe);
216 command.AppendSwitch(switches::kDisableHangMonitor); 198 command.AppendSwitch(switches::kDisableHangMonitor);
217 command.AppendSwitch(switches::kDisablePromptOnRepost); 199 command.AppendSwitch(switches::kDisablePromptOnRepost);
218 command.AppendSwitch(switches::kDomAutomationController); 200 command.AppendSwitch(switches::kDomAutomationController);
219 command.AppendSwitch(switches::kFullMemoryCrashReport); 201 command.AppendSwitch(switches::kFullMemoryCrashReport);
220 command.AppendSwitch(switches::kNoDefaultBrowserCheck); 202 command.AppendSwitch(switches::kNoDefaultBrowserCheck);
221 command.AppendSwitch(switches::kNoFirstRun); 203 command.AppendSwitch(switches::kNoFirstRun);
222 command.AppendSwitchASCII(switches::kTestType, "webdriver"); 204 if (options.user_data_dir.empty())
223
224 if (user_data_dir.empty())
225 command.AppendSwitchASCII(switches::kHomePage, chrome::kAboutBlankURL); 205 command.AppendSwitchASCII(switches::kHomePage, chrome::kAboutBlankURL);
226 206
227 command.AppendArguments(options, false); 207 command.AppendArguments(options.command, true /* include_program */);
228 208
229 launcher_.reset(new AnonymousProxyLauncher(false)); 209 // Find the Chrome binary.
210 if (command.GetProgram().empty()) {
211 FilePath browser_exe;
212 if (!GetDefaultChromeExe(&browser_exe)) {
213 *error = new Error(kUnknownError, "Could not find default Chrome binary");
214 return;
215 }
216 command.SetProgram(browser_exe);
217 }
218 if (!file_util::PathExists(command.GetProgram())) {
219 std::string message = base::StringPrintf(
220 "Could not find Chrome binary at: %" PRFilePath,
221 command.GetProgram().value().c_str());
222 *error = new Error(kUnknownError, message);
223 return;
224 }
225 std::string chrome_details = base::StringPrintf(
226 "Using Chrome binary at: %" PRFilePath,
227 command.GetProgram().value().c_str());
228 LOG(INFO) << chrome_details;
229
230 // Create the ProxyLauncher and launch Chrome.
231 if (options.channel_id.empty()) {
232 launcher_.reset(new AnonymousProxyLauncher(false));
233 } else {
234 launcher_.reset(new NamedProxyLauncher(options.channel_id, false, false));
235 }
230 ProxyLauncher::LaunchState launch_props = { 236 ProxyLauncher::LaunchState launch_props = {
231 false, // clear_profile 237 false, // clear_profile
232 user_data_dir, // template_user_data 238 options.user_data_dir, // template_user_data
233 base::Closure(), 239 base::Closure(),
234 command, 240 command,
235 true, // include_testing_id 241 true, // include_testing_id
236 true // show_window 242 true // show_window
237 }; 243 };
238 244 if (!launcher_->InitializeConnection(launch_props, true)) {
239 std::string chrome_details = base::StringPrintf( 245 LOG(ERROR) << "Failed to initialize connection";
240 "Using Chrome binary at: %" PRFilePath,
241 browser_exe.value().c_str());
242 LOG(INFO) << chrome_details;
243
244 if (!launcher_->LaunchBrowserAndServer(launch_props, true)) {
245 *error = new Error( 246 *error = new Error(
246 kUnknownError, 247 kUnknownError,
247 "Unable to either launch or connect to Chrome. Please check that " 248 "Unable to either launch or connect to Chrome. Please check that "
248 "ChromeDriver is up-to-date. " + chrome_details); 249 "ChromeDriver is up-to-date. " + chrome_details);
249 return; 250 return;
250 } 251 }
251 252
252 launcher_->automation()->set_action_timeout_ms(base::kNoTimeout); 253 launcher_->automation()->set_action_timeout_ms(base::kNoTimeout);
253 LOG(INFO) << "Chrome launched successfully. Version: " 254 LOG(INFO) << "Chrome launched successfully. Version: "
254 << automation()->server_version(); 255 << automation()->server_version();
255 256
256 bool has_automation_version = false; 257 // Check the version of Chrome is compatible with this ChromeDriver.
257 *error = CompareVersion(730, 0, &has_automation_version); 258 chrome_details += ", version (" + automation()->server_version() + ")";
258 if (*error) 259 int version = 0;
260 std::string error_msg;
261 if (!SendGetChromeDriverAutomationVersion(
262 automation(), &version, &error_msg)) {
263 *error = new Error(kUnknownError, error_msg + " " + chrome_details);
259 return; 264 return;
260 265 }
261 chrome_details += ", version (" + automation()->server_version() + ")"; 266 if (version > automation::kChromeDriverAutomationVersion) {
262 if (has_automation_version) { 267 *error = new Error(
263 int version = 0; 268 kUnknownError,
264 std::string error_msg; 269 "ChromeDriver is not compatible with this version of Chrome. " +
265 if (!SendGetChromeDriverAutomationVersion( 270 chrome_details);
266 automation(), &version, &error_msg)) { 271 return;
267 *error = new Error(kUnknownError, error_msg + " " + chrome_details);
268 return;
269 }
270 if (version > automation::kChromeDriverAutomationVersion) {
271 *error = new Error(
272 kUnknownError,
273 "ChromeDriver is not compatible with this version of Chrome. " +
274 chrome_details);
275 return;
276 }
277 } 272 }
278 } 273 }
279 274
280 void Automation::Terminate() { 275 void Automation::Terminate() {
281 if (launcher_.get() && launcher_->process() != base::kNullProcessHandle) { 276 if (launcher_.get() && launcher_->process() != base::kNullProcessHandle) {
282 launcher_->QuitBrowser(); 277 launcher_->QuitBrowser();
283 } 278 }
284 } 279 }
285 280
286 void Automation::ExecuteScript(int tab_id, 281 void Automation::ExecuteScript(int tab_id,
(...skipping 433 matching lines...) Expand 10 before | Expand all | Expand 10 after
720 768, 0, "Alerts are not supported for this version of Chrome"); 715 768, 0, "Alerts are not supported for this version of Chrome");
721 } 716 }
722 717
723 Error* Automation::CheckAdvancedInteractionsSupported() { 718 Error* Automation::CheckAdvancedInteractionsSupported() {
724 const char* message = 719 const char* message =
725 "Advanced user interactions are not supported for this version of Chrome"; 720 "Advanced user interactions are not supported for this version of Chrome";
726 return CheckVersion(750, 0, message); 721 return CheckVersion(750, 0, message);
727 } 722 }
728 723
729 } // namespace webdriver 724 } // namespace webdriver
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698