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/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/callback.h" | 13 #include "base/callback.h" |
13 #include "base/command_line.h" | 14 #include "base/command_line.h" |
14 #include "base/environment.h" | 15 #include "base/environment.h" |
15 #include "base/file_path.h" | 16 #include "base/file_path.h" |
16 #include "base/file_util.h" | 17 #include "base/file_util.h" |
17 #include "base/json/json_writer.h" | 18 #include "base/json/json_writer.h" |
18 #include "base/logging.h" | 19 #include "base/logging.h" |
| 20 #include "base/memory/ref_counted.h" |
19 #include "base/path_service.h" | 21 #include "base/path_service.h" |
20 #include "base/string_number_conversions.h" | 22 #include "base/string_number_conversions.h" |
21 #include "base/string_split.h" | 23 #include "base/string_split.h" |
22 #include "base/stringprintf.h" | 24 #include "base/stringprintf.h" |
23 #include "base/synchronization/waitable_event.h" | 25 #include "base/synchronization/waitable_event.h" |
24 #include "base/utf_string_conversions.h" | 26 #include "base/utf_string_conversions.h" |
25 #include "base/values.h" | 27 #include "base/values.h" |
26 #include "chrome/common/automation_constants.h" | 28 #include "chrome/common/automation_constants.h" |
27 #include "chrome/common/chrome_constants.h" | 29 #include "chrome/common/chrome_constants.h" |
28 #include "chrome/common/chrome_switches.h" | 30 #include "chrome/common/chrome_switches.h" |
29 #include "chrome/common/url_constants.h" | 31 #include "chrome/common/url_constants.h" |
30 #include "chrome/test/automation/automation_json_requests.h" | 32 #include "chrome/test/automation/automation_json_requests.h" |
31 #include "chrome/test/automation/automation_proxy.h" | 33 #include "chrome/test/automation/automation_proxy.h" |
| 34 #include "chrome/test/automation/browser_proxy.h" |
32 #include "chrome/test/automation/extension_proxy.h" | 35 #include "chrome/test/automation/extension_proxy.h" |
33 #include "chrome/test/automation/proxy_launcher.h" | 36 #include "chrome/test/automation/proxy_launcher.h" |
| 37 #include "chrome/test/automation/tab_proxy.h" |
34 #include "chrome/test/webdriver/frame_path.h" | 38 #include "chrome/test/webdriver/frame_path.h" |
| 39 #include "chrome/test/webdriver/utility_functions.h" |
35 #include "chrome/test/webdriver/webdriver_error.h" | 40 #include "chrome/test/webdriver/webdriver_error.h" |
36 #include "ui/gfx/point.h" | 41 #include "ui/gfx/point.h" |
37 | 42 |
38 #if defined(OS_WIN) | 43 #if defined(OS_WIN) |
39 #include "base/win/registry.h" | 44 #include "base/win/registry.h" |
40 #include "base/win/windows_version.h" | 45 #include "base/win/windows_version.h" |
41 #endif | 46 #endif |
42 | 47 |
43 namespace { | 48 namespace { |
44 | 49 |
| 50 // Iterates through each browser executable path, and checks if the path exists |
| 51 // in any of the given locations. If found, returns true and sets |browser_exe|. |
| 52 bool CheckForChromeExe(const std::vector<FilePath>& browser_exes, |
| 53 const std::vector<FilePath>& locations, |
| 54 FilePath* browser_exe) { |
| 55 for (size_t i = 0; i < browser_exes.size(); ++i) { |
| 56 for (size_t j = 0; j < locations.size(); ++j) { |
| 57 FilePath path = locations[j].Append(browser_exes[i]); |
| 58 if (file_util::PathExists(path)) { |
| 59 *browser_exe = path; |
| 60 return true; |
| 61 } |
| 62 } |
| 63 } |
| 64 return false; |
| 65 } |
| 66 |
45 // Gets the path to the default Chrome executable. Returns true on success. | 67 // Gets the path to the default Chrome executable. Returns true on success. |
46 bool GetDefaultChromeExe(FilePath* browser_exe) { | 68 bool GetDefaultChromeExe(FilePath* browser_exe) { |
| 69 // Instead of using chrome constants, we hardcode these constants here so |
| 70 // that we can locate chrome or chromium regardless of the branding |
| 71 // chromedriver is built with. It may be argued that then we need to keep |
| 72 // these in sync with chrome constants. However, if chrome constants changes, |
| 73 // we need to look for the previous and new versions to support some |
| 74 // backwards compatibility. |
| 75 #if defined(OS_WIN) |
| 76 FilePath browser_exes_array[] = { |
| 77 FilePath(L"chrome.exe") |
| 78 }; |
| 79 #elif defined(OS_MACOSX) |
| 80 FilePath browser_exes_array[] = { |
| 81 FilePath("Google Chrome.app/Contents/MacOS/Google Chrome"), |
| 82 FilePath("Chromium.app/Contents/MacOS/Chromium") |
| 83 }; |
| 84 #elif defined(OS_LINUX) |
| 85 FilePath browser_exes_array[] = { |
| 86 FilePath("google-chrome"), |
| 87 FilePath("chrome"), |
| 88 FilePath("chromium"), |
| 89 FilePath("chromium-browser") |
| 90 }; |
| 91 #endif |
| 92 std::vector<FilePath> browser_exes( |
| 93 browser_exes_array, browser_exes_array + arraysize(browser_exes_array)); |
| 94 |
| 95 // Step 1: Check the directory this module resides in. This is done |
| 96 // before all else so that the tests will pickup the built chrome. |
| 97 FilePath module_dir; |
| 98 if (PathService::Get(base::DIR_MODULE, &module_dir)) { |
| 99 for (size_t j = 0; j < browser_exes.size(); ++j) { |
| 100 FilePath path = module_dir.Append(browser_exes[j]); |
| 101 if (file_util::PathExists(path)) { |
| 102 *browser_exe = path; |
| 103 return true; |
| 104 } |
| 105 } |
| 106 } |
| 107 |
| 108 // Step 2: Add all possible install locations, in order they should be |
| 109 // searched. If a location can only hold a chromium install, add it to |
| 110 // |chromium_locations|. Since on some platforms we cannot tell by the binary |
| 111 // name whether it is chrome or chromium, we search these locations last. |
| 112 // We attempt to run chrome before chromium, if any install can be found. |
47 std::vector<FilePath> locations; | 113 std::vector<FilePath> locations; |
48 // Add the directory which this module resides in. | 114 std::vector<FilePath> chromium_locations; |
49 FilePath module_dir; | |
50 if (PathService::Get(base::DIR_MODULE, &module_dir)) | |
51 locations.push_back(module_dir); | |
52 | |
53 #if defined(OS_WIN) | 115 #if defined(OS_WIN) |
54 // Add the App Paths registry key location. | 116 // Add the App Paths registry key location. |
55 const wchar_t kSubKey[] = | 117 const wchar_t kSubKey[] = |
56 L"Software\\Microsoft\\Windows\\CurrentVersion\\App Paths\\chrome.exe"; | 118 L"Software\\Microsoft\\Windows\\CurrentVersion\\App Paths\\chrome.exe"; |
57 base::win::RegKey key(HKEY_CURRENT_USER, kSubKey, KEY_READ); | 119 base::win::RegKey key(HKEY_CURRENT_USER, kSubKey, KEY_READ); |
58 std::wstring path; | 120 std::wstring path; |
59 if (key.ReadValue(L"path", &path) == ERROR_SUCCESS) | 121 if (key.ReadValue(L"path", &path) == ERROR_SUCCESS) |
60 locations.push_back(FilePath(path)); | 122 locations.push_back(FilePath(path)); |
61 base::win::RegKey sys_key(HKEY_LOCAL_MACHINE, kSubKey, KEY_READ); | 123 base::win::RegKey sys_key(HKEY_LOCAL_MACHINE, kSubKey, KEY_READ); |
62 if (sys_key.ReadValue(L"path", &path) == ERROR_SUCCESS) | 124 if (sys_key.ReadValue(L"path", &path) == ERROR_SUCCESS) |
63 locations.push_back(FilePath(path)); | 125 locations.push_back(FilePath(path)); |
64 | 126 |
65 // Add the user-level location for Chrome. | 127 // Add the user-level location for Chrome. |
66 FilePath app_from_google(L"Google\\Chrome\\Application"); | 128 FilePath app_from_google(L"Google\\Chrome\\Application"); |
| 129 FilePath app_from_chromium(L"Chromium\\Application"); |
67 scoped_ptr<base::Environment> env(base::Environment::Create()); | 130 scoped_ptr<base::Environment> env(base::Environment::Create()); |
68 std::string home_dir; | 131 std::string home_dir; |
69 if (env->GetVar("userprofile", &home_dir)) { | 132 if (env->GetVar("userprofile", &home_dir)) { |
70 FilePath default_location(UTF8ToWide(home_dir)); | 133 FilePath default_location(UTF8ToWide(home_dir)); |
71 if (base::win::GetVersion() < base::win::VERSION_VISTA) { | 134 if (base::win::GetVersion() < base::win::VERSION_VISTA) { |
72 default_location = default_location.Append( | 135 default_location = default_location.Append( |
73 L"Local Settings\\Application Data"); | 136 L"Local Settings\\Application Data"); |
74 } else { | 137 } else { |
75 default_location = default_location.Append(L"AppData\\Local"); | 138 default_location = default_location.Append(L"AppData\\Local"); |
76 } | 139 } |
77 locations.push_back(default_location.Append(app_from_google)); | 140 locations.push_back(default_location.Append(app_from_google)); |
| 141 chromium_locations.push_back(default_location.Append(app_from_chromium)); |
78 } | 142 } |
79 | 143 |
80 // Add the system-level location for Chrome. | 144 // Add the system-level location for Chrome. |
81 std::string program_dir; | 145 std::string program_dir; |
82 if (env->GetVar("ProgramFiles", &program_dir)) { | 146 if (env->GetVar("ProgramFiles", &program_dir)) { |
83 locations.push_back(FilePath(UTF8ToWide(program_dir)) | 147 locations.push_back(FilePath(UTF8ToWide(program_dir)) |
84 .Append(app_from_google)); | 148 .Append(app_from_google)); |
| 149 chromium_locations.push_back(FilePath(UTF8ToWide(program_dir)) |
| 150 .Append(app_from_chromium)); |
85 } | 151 } |
86 if (env->GetVar("ProgramFiles(x86)", &program_dir)) { | 152 if (env->GetVar("ProgramFiles(x86)", &program_dir)) { |
87 locations.push_back(FilePath(UTF8ToWide(program_dir)) | 153 locations.push_back(FilePath(UTF8ToWide(program_dir)) |
88 .Append(app_from_google)); | 154 .Append(app_from_google)); |
| 155 chromium_locations.push_back(FilePath(UTF8ToWide(program_dir)) |
| 156 .Append(app_from_chromium)); |
89 } | 157 } |
90 #elif defined(OS_MACOSX) | 158 #elif defined(OS_MACOSX) |
91 locations.push_back(FilePath("/Applications")); | 159 std::vector<FilePath> app_dirs; |
| 160 webdriver::GetApplicationDirs(&app_dirs); |
| 161 locations.insert(locations.end(), app_dirs.begin(), app_dirs.end()); |
92 #elif defined(OS_LINUX) | 162 #elif defined(OS_LINUX) |
93 // Proxy launcher doesn't check for google-chrome, only chrome. | 163 locations.push_back(FilePath("/opt/google/chrome")); |
94 FilePath chrome_sym_link("/usr/bin/google-chrome"); | 164 locations.push_back(FilePath("/usr/local/bin")); |
95 if (file_util::PathExists(chrome_sym_link)) { | 165 locations.push_back(FilePath("/usr/local/sbin")); |
96 FilePath chrome; | 166 locations.push_back(FilePath("/usr/bin")); |
97 if (file_util::ReadSymbolicLink(chrome_sym_link, &chrome)) { | 167 locations.push_back(FilePath("/usr/sbin")); |
98 locations.push_back(chrome.DirName()); | 168 locations.push_back(FilePath("/bin")); |
99 } | 169 locations.push_back(FilePath("/sbin")); |
100 } | |
101 #endif | 170 #endif |
102 | 171 |
103 // Add the current directory. | 172 // Add the current directory. |
104 FilePath current_dir; | 173 FilePath current_dir; |
105 if (file_util::GetCurrentDirectory(¤t_dir)) | 174 if (file_util::GetCurrentDirectory(¤t_dir)) |
106 locations.push_back(current_dir); | 175 locations.push_back(current_dir); |
107 | 176 |
108 // Determine the default directory. | 177 // Step 3: For each browser exe path, check each location to see if the |
109 for (size_t i = 0; i < locations.size(); ++i) { | 178 // browser is installed there. Check the chromium locations lastly. |
110 FilePath path = locations[i].Append(chrome::kBrowserProcessExecutablePath); | 179 return CheckForChromeExe(browser_exes, locations, browser_exe) || |
111 if (file_util::PathExists(path)) { | 180 CheckForChromeExe(browser_exes, chromium_locations, browser_exe); |
112 *browser_exe = path; | |
113 return true; | |
114 } | |
115 } | |
116 return false; | |
117 } | 181 } |
118 | 182 |
119 } // namespace | 183 } // namespace |
120 | 184 |
121 namespace webdriver { | 185 namespace webdriver { |
122 | 186 |
123 Automation::Automation() {} | 187 Automation::Automation() {} |
124 | 188 |
125 Automation::~Automation() {} | 189 Automation::~Automation() {} |
126 | 190 |
(...skipping 296 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
423 url, 1, &navigate_response, | 487 url, 1, &navigate_response, |
424 &error_msg)) { | 488 &error_msg)) { |
425 *error = new Error(kUnknownError, error_msg); | 489 *error = new Error(kUnknownError, error_msg); |
426 return; | 490 return; |
427 } | 491 } |
428 // TODO(kkania): Do not rely on this enum. | 492 // TODO(kkania): Do not rely on this enum. |
429 if (navigate_response == AUTOMATION_MSG_NAVIGATION_ERROR) | 493 if (navigate_response == AUTOMATION_MSG_NAVIGATION_ERROR) |
430 *error = new Error(kUnknownError, "Navigation error occurred"); | 494 *error = new Error(kUnknownError, "Navigation error occurred"); |
431 } | 495 } |
432 | 496 |
| 497 void Automation::NavigateToURLAsync(int tab_id, |
| 498 const std::string& url, |
| 499 Error** error) { |
| 500 int windex = 0, tab_index = 0; |
| 501 *error = GetIndicesForTab(tab_id, &windex, &tab_index); |
| 502 if (*error) |
| 503 return; |
| 504 |
| 505 scoped_refptr<BrowserProxy> browser = automation()->GetBrowserWindow(windex); |
| 506 if (!browser) { |
| 507 *error = new Error(kUnknownError, "Couldn't obtain browser proxy"); |
| 508 return; |
| 509 } |
| 510 scoped_refptr<TabProxy> tab = browser->GetTab(tab_index); |
| 511 if (!tab) { |
| 512 *error = new Error(kUnknownError, "Couldn't obtain tab proxy"); |
| 513 return; |
| 514 } |
| 515 if (!tab->NavigateToURLAsync(GURL(url))) { |
| 516 *error = new Error(kUnknownError, "Unable to navigate to url"); |
| 517 return; |
| 518 } |
| 519 } |
| 520 |
433 void Automation::GoForward(int tab_id, Error** error) { | 521 void Automation::GoForward(int tab_id, Error** error) { |
434 int windex = 0, tab_index = 0; | 522 int windex = 0, tab_index = 0; |
435 *error = GetIndicesForTab(tab_id, &windex, &tab_index); | 523 *error = GetIndicesForTab(tab_id, &windex, &tab_index); |
436 if (*error) | 524 if (*error) |
437 return; | 525 return; |
438 | 526 |
439 std::string error_msg; | 527 std::string error_msg; |
440 if (!SendGoForwardJSONRequest( | 528 if (!SendGoForwardJSONRequest( |
441 automation(), windex, tab_index, &error_msg)) { | 529 automation(), windex, tab_index, &error_msg)) { |
442 *error = new Error(kUnknownError, error_msg); | 530 *error = new Error(kUnknownError, error_msg); |
(...skipping 189 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
632 768, 0, "Alerts are not supported for this version of Chrome"); | 720 768, 0, "Alerts are not supported for this version of Chrome"); |
633 } | 721 } |
634 | 722 |
635 Error* Automation::CheckAdvancedInteractionsSupported() { | 723 Error* Automation::CheckAdvancedInteractionsSupported() { |
636 const char* message = | 724 const char* message = |
637 "Advanced user interactions are not supported for this version of Chrome"; | 725 "Advanced user interactions are not supported for this version of Chrome"; |
638 return CheckVersion(750, 0, message); | 726 return CheckVersion(750, 0, message); |
639 } | 727 } |
640 | 728 |
641 } // namespace webdriver | 729 } // namespace webdriver |
OLD | NEW |