| 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 #include "base/command_line.h" | 7 #include "base/command_line.h" |
| 8 #include "base/json/json_writer.h" | 8 #include "base/json/json_writer.h" |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/utf_string_conversions.h" | 10 #include "base/utf_string_conversions.h" |
| 11 #include "base/values.h" | 11 #include "base/values.h" |
| 12 #include "chrome/common/chrome_switches.h" | 12 #include "chrome/common/chrome_switches.h" |
| 13 #include "chrome/test/automation/automation_proxy.h" |
| 13 #include "chrome/test/automation/browser_proxy.h" | 14 #include "chrome/test/automation/browser_proxy.h" |
| 14 #include "chrome/test/automation/tab_proxy.h" | 15 #include "chrome/test/automation/tab_proxy.h" |
| 15 #include "chrome/test/test_launcher_utils.h" | 16 #include "chrome/test/test_launcher_utils.h" |
| 16 #include "googleurl/src/gurl.h" | 17 #include "googleurl/src/gurl.h" |
| 17 | 18 |
| 18 namespace webdriver { | 19 namespace webdriver { |
| 19 | 20 |
| 20 Automation::Automation() {} | 21 Automation::Automation() {} |
| 21 | 22 |
| 22 Automation::~Automation() {} | 23 Automation::~Automation() {} |
| 23 | 24 |
| 24 void Automation::Init(bool* success) { | 25 void Automation::Init(bool* success) { |
| 25 *success = false; | 26 *success = false; |
| 26 | 27 |
| 27 // Create a temp directory for the new profile. | 28 // Create a temp directory for the new profile. |
| 28 if (!profile_dir_.CreateUniqueTempDir()) { | 29 if (!profile_dir_.CreateUniqueTempDir()) { |
| 29 LOG(ERROR) << "Could not make a temp profile directory"; | 30 LOG(ERROR) << "Could not make a temp profile directory"; |
| 30 return; | 31 return; |
| 31 } | 32 } |
| 32 // TODO(kkania): See if these are still needed. | 33 // TODO(kkania): See if these are still needed. |
| 33 test_launcher_utils::PrepareBrowserCommandLineForTests(&launch_arguments_); | 34 test_launcher_utils::PrepareBrowserCommandLineForTests(&launch_arguments_); |
| 34 launch_arguments_.AppendSwitch(switches::kDomAutomationController); | 35 launch_arguments_.AppendSwitch(switches::kDomAutomationController); |
| 35 launch_arguments_.AppendSwitch(switches::kFullMemoryCrashReport); | 36 launch_arguments_.AppendSwitch(switches::kFullMemoryCrashReport); |
| 36 | 37 |
| 37 launch_arguments_.AppendSwitchPath(switches::kUserDataDir, | 38 launch_arguments_.AppendSwitchPath(switches::kUserDataDir, |
| 38 profile_dir_.path()); | 39 profile_dir_.path()); |
| 39 UITestBase::SetUp(); | 40 UITestBase::SetUp(); |
| 40 browser_ = automation()->GetBrowserWindow(0); | |
| 41 if (!browser_.get()) { | |
| 42 Terminate(); | |
| 43 return; | |
| 44 } | |
| 45 tab_ = browser_->GetActiveTab(); | |
| 46 if (!tab_.get()) { | |
| 47 Terminate(); | |
| 48 return; | |
| 49 } | |
| 50 *success = true; | 41 *success = true; |
| 51 } | 42 } |
| 52 | 43 |
| 53 void Automation::Terminate() { | 44 void Automation::Terminate() { |
| 54 QuitBrowser(); | 45 QuitBrowser(); |
| 55 } | 46 } |
| 56 | 47 |
| 57 void Automation::ExecuteScript(const std::string& frame_xpath, | 48 void Automation::ExecuteScript(int tab_id, |
| 49 const std::string& frame_xpath, |
| 58 const std::string& script, | 50 const std::string& script, |
| 59 std::string* result, | 51 std::string* result, |
| 60 bool* success) { | 52 bool* success) { |
| 53 TabProxy* tab = GetTabById(tab_id); |
| 54 if (!tab) { |
| 55 *success = false; |
| 56 return; |
| 57 } |
| 61 std::wstring wide_xpath = UTF8ToWide(frame_xpath); | 58 std::wstring wide_xpath = UTF8ToWide(frame_xpath); |
| 62 std::wstring wide_script = UTF8ToWide(script); | 59 std::wstring wide_script = UTF8ToWide(script); |
| 63 std::wstring wide_result; | 60 std::wstring wide_result; |
| 64 *success = tab_->ExecuteAndExtractString( | 61 *success = tab->ExecuteAndExtractString( |
| 65 wide_xpath, wide_script, &wide_result); | 62 wide_xpath, wide_script, &wide_result); |
| 66 if (*success) | 63 if (*success) |
| 67 *result = WideToUTF8(wide_result); | 64 *result = WideToUTF8(wide_result); |
| 68 } | 65 } |
| 69 | 66 |
| 70 void Automation::SendWebKeyEvent(const WebKeyEvent& key_event, | 67 void Automation::SendWebKeyEvent(int tab_id, |
| 68 const WebKeyEvent& key_event, |
| 71 bool* success) { | 69 bool* success) { |
| 70 TabProxy* tab = GetTabById(tab_id); |
| 71 if (!tab) { |
| 72 LOG(ERROR) << "No such tab"; |
| 73 *success = false; |
| 74 return; |
| 75 } |
| 76 int tab_index = 0; |
| 77 if (!tab->GetTabIndex(&tab_index)) { |
| 78 LOG(ERROR) << "Could not get tab index"; |
| 79 *success = false; |
| 80 return; |
| 81 } |
| 82 scoped_refptr<BrowserProxy> browser = tab->GetParentBrowser(); |
| 83 if (!browser.get()) { |
| 84 LOG(ERROR) << "Could not get parent browser of tab"; |
| 85 *success = false; |
| 86 return; |
| 87 } |
| 88 if (!browser->ActivateTab(tab_index)) { |
| 89 LOG(ERROR) << "Could not activate tab to send keys"; |
| 90 *success = false; |
| 91 return; |
| 92 } |
| 72 scoped_ptr<DictionaryValue> dict(new DictionaryValue); | 93 scoped_ptr<DictionaryValue> dict(new DictionaryValue); |
| 73 dict->SetString("command", "SendKeyEventToActiveTab"); | 94 dict->SetString("command", "SendKeyEventToActiveTab"); |
| 74 dict->SetInteger("type", key_event.type); | 95 dict->SetInteger("type", key_event.type); |
| 75 dict->SetInteger("nativeKeyCode", key_event.key_code); | 96 dict->SetInteger("nativeKeyCode", key_event.key_code); |
| 76 dict->SetInteger("windowsKeyCode", key_event.key_code); | 97 dict->SetInteger("windowsKeyCode", key_event.key_code); |
| 77 dict->SetString("unmodifiedText", key_event.unmodified_text); | 98 dict->SetString("unmodifiedText", key_event.unmodified_text); |
| 78 dict->SetString("text", key_event.modified_text); | 99 dict->SetString("text", key_event.modified_text); |
| 79 dict->SetInteger("modifiers", key_event.modifiers); | 100 dict->SetInteger("modifiers", key_event.modifiers); |
| 80 dict->SetBoolean("isSystemKey", false); | 101 dict->SetBoolean("isSystemKey", false); |
| 81 std::string request; | 102 std::string request; |
| 82 base::JSONWriter::Write(dict.get(), false, &request); | 103 base::JSONWriter::Write(dict.get(), false, &request); |
| 83 std::string reply; | 104 std::string reply; |
| 84 *success = browser_->SendJSONRequest(request, &reply); | 105 *success = browser->SendJSONRequest(request, &reply); |
| 85 if (!*success) { | 106 if (!*success) { |
| 86 LOG(ERROR) << "Could not send web key event. Reply: " << reply; | 107 LOG(ERROR) << "Could not send web key event. Reply: " << reply; |
| 87 } | 108 } |
| 88 } | 109 } |
| 89 | 110 |
| 90 void Automation::NavigateToURL(const std::string& url, | 111 void Automation::NavigateToURL(int tab_id, |
| 112 const std::string& url, |
| 91 bool* success) { | 113 bool* success) { |
| 92 *success = tab_->NavigateToURL(GURL(url)); | 114 TabProxy* tab = GetTabById(tab_id); |
| 115 if (!tab) { |
| 116 *success = false; |
| 117 return; |
| 118 } |
| 119 *success = tab->NavigateToURL(GURL(url)); |
| 93 } | 120 } |
| 94 | 121 |
| 95 void Automation::GoForward(bool* success) { | 122 void Automation::GoForward(int tab_id, bool* success) { |
| 96 *success = tab_->GoForward(); | 123 TabProxy* tab = GetTabById(tab_id); |
| 124 if (!tab) { |
| 125 *success = false; |
| 126 return; |
| 127 } |
| 128 *success = tab->GoForward(); |
| 97 } | 129 } |
| 98 | 130 |
| 99 void Automation::GoBack(bool* success) { | 131 void Automation::GoBack(int tab_id, bool* success) { |
| 100 *success = tab_->GoBack(); | 132 TabProxy* tab = GetTabById(tab_id); |
| 133 if (!tab) { |
| 134 *success = false; |
| 135 return; |
| 136 } |
| 137 *success = tab->GoBack(); |
| 101 } | 138 } |
| 102 | 139 |
| 103 void Automation::Reload(bool* success) { | 140 void Automation::Reload(int tab_id, bool* success) { |
| 104 *success = tab_->Reload(); | 141 TabProxy* tab = GetTabById(tab_id); |
| 142 if (!tab) { |
| 143 *success = false; |
| 144 return; |
| 145 } |
| 146 *success = tab->Reload(); |
| 105 } | 147 } |
| 106 | 148 |
| 107 void Automation::GetURL(std::string* url, | 149 void Automation::GetURL(int tab_id, |
| 150 std::string* url, |
| 108 bool* success) { | 151 bool* success) { |
| 152 TabProxy* tab = GetTabById(tab_id); |
| 153 if (!tab) { |
| 154 *success = false; |
| 155 return; |
| 156 } |
| 109 GURL gurl; | 157 GURL gurl; |
| 110 *success = tab_->GetCurrentURL(&gurl); | 158 *success = tab->GetCurrentURL(&gurl); |
| 111 if (*success) | 159 if (*success) |
| 112 *url = gurl.possibly_invalid_spec(); | 160 *url = gurl.possibly_invalid_spec(); |
| 113 } | 161 } |
| 114 | 162 |
| 115 void Automation::GetGURL(GURL* gurl, | 163 void Automation::GetGURL(int tab_id, |
| 164 GURL* gurl, |
| 116 bool* success) { | 165 bool* success) { |
| 117 *success = tab_->GetCurrentURL(gurl); | 166 TabProxy* tab = GetTabById(tab_id); |
| 167 if (!tab) { |
| 168 *success = false; |
| 169 return; |
| 170 } |
| 171 *success = tab->GetCurrentURL(gurl); |
| 118 } | 172 } |
| 119 | 173 |
| 120 void Automation::GetTabTitle(std::string* tab_title, | 174 void Automation::GetTabTitle(int tab_id, |
| 175 std::string* tab_title, |
| 121 bool* success) { | 176 bool* success) { |
| 177 TabProxy* tab = GetTabById(tab_id); |
| 178 if (!tab) { |
| 179 *success = false; |
| 180 return; |
| 181 } |
| 122 std::wstring wide_title; | 182 std::wstring wide_title; |
| 123 *success = tab_->GetTabTitle(&wide_title); | 183 *success = tab->GetTabTitle(&wide_title); |
| 124 if (*success) | 184 if (*success) |
| 125 *tab_title = WideToUTF8(wide_title); | 185 *tab_title = WideToUTF8(wide_title); |
| 126 } | 186 } |
| 127 | 187 |
| 128 void Automation::GetCookies(const GURL& gurl, | 188 void Automation::GetCookies(int tab_id, |
| 189 const GURL& gurl, |
| 129 std::string* cookies, | 190 std::string* cookies, |
| 130 bool* success) { | 191 bool* success) { |
| 131 *success = tab_->GetCookies(gurl, cookies); | 192 TabProxy* tab = GetTabById(tab_id); |
| 193 if (!tab) { |
| 194 *success = false; |
| 195 return; |
| 196 } |
| 197 *success = tab->GetCookies(gurl, cookies); |
| 132 } | 198 } |
| 133 | 199 |
| 134 void Automation::GetCookieByName(const GURL& gurl, | 200 void Automation::GetCookieByName(int tab_id, |
| 201 const GURL& gurl, |
| 135 const std::string& cookie_name, | 202 const std::string& cookie_name, |
| 136 std::string* cookie, | 203 std::string* cookie, |
| 137 bool* success) { | 204 bool* success) { |
| 138 *success = tab_->GetCookieByName(gurl, cookie_name, cookie); | 205 TabProxy* tab = GetTabById(tab_id); |
| 206 if (!tab) { |
| 207 *success = false; |
| 208 return; |
| 209 } |
| 210 *success = tab->GetCookieByName(gurl, cookie_name, cookie); |
| 139 } | 211 } |
| 140 | 212 |
| 141 void Automation::DeleteCookie(const GURL& gurl, | 213 void Automation::DeleteCookie(int tab_id, |
| 214 const GURL& gurl, |
| 142 const std::string& cookie_name, | 215 const std::string& cookie_name, |
| 143 bool* success) { | 216 bool* success) { |
| 144 *success = tab_->DeleteCookie(gurl, cookie_name); | 217 TabProxy* tab = GetTabById(tab_id); |
| 218 if (!tab) { |
| 219 *success = false; |
| 220 return; |
| 221 } |
| 222 *success = tab->DeleteCookie(gurl, cookie_name); |
| 145 } | 223 } |
| 146 | 224 |
| 147 void Automation::SetCookie(const GURL& gurl, | 225 void Automation::SetCookie(int tab_id, |
| 226 const GURL& gurl, |
| 148 const std::string& cookie, | 227 const std::string& cookie, |
| 149 bool* success) { | 228 bool* success) { |
| 150 *success = tab_->SetCookie(gurl, cookie); | 229 TabProxy* tab = GetTabById(tab_id); |
| 230 if (!tab) { |
| 231 *success = false; |
| 232 return; |
| 233 } |
| 234 *success = tab->SetCookie(gurl, cookie); |
| 235 } |
| 236 |
| 237 void Automation::GetTabIds(std::vector<int>* tab_ids, |
| 238 bool* success) { |
| 239 *success = false; |
| 240 int browser_count = 0; |
| 241 if (!automation()->GetBrowserWindowCount(&browser_count)) { |
| 242 LOG(ERROR) << "Failed to get browser window count"; |
| 243 return; |
| 244 } |
| 245 TabIdMap tab_id_map; |
| 246 for (int browser_index = 0; browser_index < browser_count; ++browser_index) { |
| 247 scoped_refptr<BrowserProxy> browser = |
| 248 automation()->GetBrowserWindow(browser_index); |
| 249 if (!browser.get()) |
| 250 continue; |
| 251 int tab_count = 0; |
| 252 if (!browser->GetTabCount(&tab_count)) |
| 253 continue; |
| 254 |
| 255 for (int tab_index = 0; tab_index < tab_count; ++tab_index) { |
| 256 scoped_refptr<TabProxy> tab = browser->GetTab(tab_index); |
| 257 if (!tab.get()) |
| 258 continue; |
| 259 tab_ids->push_back(tab->handle()); |
| 260 tab_id_map.insert(std::make_pair(tab->handle(), tab)); |
| 261 } |
| 262 } |
| 263 |
| 264 tab_id_map_ = tab_id_map; |
| 265 *success = true; |
| 266 } |
| 267 |
| 268 void Automation::DoesTabExist(int tab_id, bool* does_exist) { |
| 269 TabProxy* tab = GetTabById(tab_id); |
| 270 *does_exist = tab && tab->is_valid(); |
| 271 } |
| 272 |
| 273 void Automation::CloseTab(int tab_id, bool* success) { |
| 274 TabProxy* tab = GetTabById(tab_id); |
| 275 if (!tab) { |
| 276 *success = false; |
| 277 return; |
| 278 } |
| 279 *success = tab->Close(true); |
| 280 } |
| 281 |
| 282 TabProxy* Automation::GetTabById(int tab_id) { |
| 283 TabIdMap::const_iterator iter = tab_id_map_.find(tab_id); |
| 284 if (iter != tab_id_map_.end()) { |
| 285 return iter->second.get(); |
| 286 } |
| 287 return NULL; |
| 151 } | 288 } |
| 152 | 289 |
| 153 } // namespace webdriver | 290 } // namespace webdriver |
| OLD | NEW |