Chromium Code Reviews| 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" |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 30 return; | 30 return; |
| 31 } | 31 } |
| 32 // TODO(kkania): See if these are still needed. | 32 // TODO(kkania): See if these are still needed. |
| 33 test_launcher_utils::PrepareBrowserCommandLineForTests(&launch_arguments_); | 33 test_launcher_utils::PrepareBrowserCommandLineForTests(&launch_arguments_); |
| 34 launch_arguments_.AppendSwitch(switches::kDomAutomationController); | 34 launch_arguments_.AppendSwitch(switches::kDomAutomationController); |
| 35 launch_arguments_.AppendSwitch(switches::kFullMemoryCrashReport); | 35 launch_arguments_.AppendSwitch(switches::kFullMemoryCrashReport); |
| 36 | 36 |
| 37 launch_arguments_.AppendSwitchPath(switches::kUserDataDir, | 37 launch_arguments_.AppendSwitchPath(switches::kUserDataDir, |
| 38 profile_dir_.path()); | 38 profile_dir_.path()); |
| 39 UITestBase::SetUp(); | 39 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; | 40 *success = true; |
| 51 } | 41 } |
| 52 | 42 |
| 53 void Automation::Terminate() { | 43 void Automation::Terminate() { |
| 54 QuitBrowser(); | 44 QuitBrowser(); |
| 55 } | 45 } |
| 56 | 46 |
| 57 void Automation::ExecuteScript(const std::string& frame_xpath, | 47 void Automation::ExecuteScript(int tab_id, |
| 48 const std::string& frame_xpath, | |
| 58 const std::string& script, | 49 const std::string& script, |
| 59 std::string* result, | 50 std::string* result, |
| 60 bool* success) { | 51 bool* success) { |
| 52 TabProxy* tab = GetTabById(tab_id); | |
| 53 if (!tab) { | |
| 54 *success = false; | |
| 55 return; | |
| 56 } | |
| 61 std::wstring wide_xpath = UTF8ToWide(frame_xpath); | 57 std::wstring wide_xpath = UTF8ToWide(frame_xpath); |
| 62 std::wstring wide_script = UTF8ToWide(script); | 58 std::wstring wide_script = UTF8ToWide(script); |
| 63 std::wstring wide_result; | 59 std::wstring wide_result; |
| 64 *success = tab_->ExecuteAndExtractString( | 60 *success = tab->ExecuteAndExtractString( |
| 65 wide_xpath, wide_script, &wide_result); | 61 wide_xpath, wide_script, &wide_result); |
| 66 if (*success) | 62 if (*success) |
| 67 *result = WideToUTF8(wide_result); | 63 *result = WideToUTF8(wide_result); |
| 68 } | 64 } |
| 69 | 65 |
| 70 void Automation::SendWebKeyEvent(const WebKeyEvent& key_event, | 66 void Automation::SendWebKeyEvent(int tab_id, |
| 67 const WebKeyEvent& key_event, | |
| 71 bool* success) { | 68 bool* success) { |
| 69 TabProxy* tab = GetTabById(tab_id); | |
| 70 if (!tab) { | |
| 71 *success = false; | |
| 72 return; | |
| 73 } | |
| 74 int tab_index = 0; | |
| 75 if (!tab->GetTabIndex(&tab_index)) { | |
| 76 *success = false; | |
| 77 return; | |
| 78 } | |
| 79 scoped_refptr<BrowserProxy> browser = tab->GetParentBrowser(); | |
| 80 if (!browser.get()) { | |
| 81 LOG(ERROR) << "Could not get parent browser of tab"; | |
| 82 *success = false; | |
| 83 return; | |
| 84 } | |
| 85 if (!browser->ActivateTab(tab_index)) { | |
| 86 LOG(ERROR) << "Could not activate tab to send keys"; | |
| 87 *success = false; | |
| 88 return; | |
| 89 } | |
| 72 scoped_ptr<DictionaryValue> dict(new DictionaryValue); | 90 scoped_ptr<DictionaryValue> dict(new DictionaryValue); |
| 73 dict->SetString("command", "SendKeyEventToActiveTab"); | 91 dict->SetString("command", "SendKeyEventToActiveTab"); |
| 74 dict->SetInteger("type", key_event.type); | 92 dict->SetInteger("type", key_event.type); |
| 75 dict->SetInteger("nativeKeyCode", key_event.key_code); | 93 dict->SetInteger("nativeKeyCode", key_event.key_code); |
| 76 dict->SetInteger("windowsKeyCode", key_event.key_code); | 94 dict->SetInteger("windowsKeyCode", key_event.key_code); |
| 77 dict->SetString("unmodifiedText", key_event.unmodified_text); | 95 dict->SetString("unmodifiedText", key_event.unmodified_text); |
| 78 dict->SetString("text", key_event.modified_text); | 96 dict->SetString("text", key_event.modified_text); |
| 79 dict->SetInteger("modifiers", key_event.modifiers); | 97 dict->SetInteger("modifiers", key_event.modifiers); |
| 80 dict->SetBoolean("isSystemKey", false); | 98 dict->SetBoolean("isSystemKey", false); |
| 81 std::string request; | 99 std::string request; |
| 82 base::JSONWriter::Write(dict.get(), false, &request); | 100 base::JSONWriter::Write(dict.get(), false, &request); |
| 83 std::string reply; | 101 std::string reply; |
| 84 *success = browser_->SendJSONRequest(request, &reply); | 102 *success = browser->SendJSONRequest(request, &reply); |
| 85 if (!*success) { | 103 if (!*success) { |
| 86 LOG(ERROR) << "Could not send web key event. Reply: " << reply; | 104 LOG(ERROR) << "Could not send web key event. Reply: " << reply; |
| 87 } | 105 } |
| 88 } | 106 } |
| 89 | 107 |
| 90 void Automation::NavigateToURL(const std::string& url, | 108 void Automation::NavigateToURL(int tab_id, |
| 109 const std::string& url, | |
| 91 bool* success) { | 110 bool* success) { |
| 92 *success = tab_->NavigateToURL(GURL(url)); | 111 TabProxy* tab = GetTabById(tab_id); |
| 112 if (!tab) { | |
| 113 *success = false; | |
| 114 return; | |
| 115 } | |
| 116 *success = tab->NavigateToURL(GURL(url)); | |
| 93 } | 117 } |
| 94 | 118 |
| 95 void Automation::GoForward(bool* success) { | 119 void Automation::GoForward(int tab_id, bool* success) { |
| 96 *success = tab_->GoForward(); | 120 TabProxy* tab = GetTabById(tab_id); |
| 121 if (!tab) { | |
| 122 *success = false; | |
| 123 return; | |
| 124 } | |
| 125 *success = tab->GoForward(); | |
| 97 } | 126 } |
| 98 | 127 |
| 99 void Automation::GoBack(bool* success) { | 128 void Automation::GoBack(int tab_id, bool* success) { |
| 100 *success = tab_->GoBack(); | 129 TabProxy* tab = GetTabById(tab_id); |
| 130 if (!tab) { | |
| 131 *success = false; | |
| 132 return; | |
| 133 } | |
| 134 *success = tab->GoBack(); | |
| 101 } | 135 } |
| 102 | 136 |
| 103 void Automation::Reload(bool* success) { | 137 void Automation::Reload(int tab_id, bool* success) { |
| 104 *success = tab_->Reload(); | 138 TabProxy* tab = GetTabById(tab_id); |
| 139 if (!tab) { | |
| 140 *success = false; | |
| 141 return; | |
| 142 } | |
| 143 *success = tab->Reload(); | |
| 105 } | 144 } |
| 106 | 145 |
| 107 void Automation::GetURL(std::string* url, | 146 void Automation::GetURL(int tab_id, |
| 147 std::string* url, | |
| 108 bool* success) { | 148 bool* success) { |
| 149 TabProxy* tab = GetTabById(tab_id); | |
| 150 if (!tab) { | |
| 151 *success = false; | |
| 152 return; | |
| 153 } | |
| 109 GURL gurl; | 154 GURL gurl; |
| 110 *success = tab_->GetCurrentURL(&gurl); | 155 *success = tab->GetCurrentURL(&gurl); |
| 111 if (*success) | 156 if (*success) |
| 112 *url = gurl.possibly_invalid_spec(); | 157 *url = gurl.possibly_invalid_spec(); |
| 113 } | 158 } |
| 114 | 159 |
| 115 void Automation::GetTabTitle(std::string* tab_title, | 160 void Automation::GetTabTitle(int tab_id, |
| 161 std::string* tab_title, | |
| 116 bool* success) { | 162 bool* success) { |
| 163 TabProxy* tab = GetTabById(tab_id); | |
| 164 if (!tab) { | |
| 165 *success = false; | |
| 166 return; | |
| 167 } | |
| 117 std::wstring wide_title; | 168 std::wstring wide_title; |
| 118 *success = tab_->GetTabTitle(&wide_title); | 169 *success = tab->GetTabTitle(&wide_title); |
| 119 if (*success) | 170 if (*success) |
| 120 *tab_title = WideToUTF8(wide_title); | 171 *tab_title = WideToUTF8(wide_title); |
| 121 } | 172 } |
| 122 | 173 |
| 174 void Automation::GetTabIds(std::vector<int>* tab_ids, | |
| 175 bool* success) { | |
| 176 *success = false; | |
| 177 TabIdMap tab_id_map; | |
| 178 int browser_count = 0; | |
| 179 if (!automation()->GetBrowserWindowCount(&browser_count)) { | |
| 180 LOG(ERROR) << "Failed to get browser window count"; | |
| 181 return; | |
| 182 } | |
| 183 for (int browser_index = 0; browser_index < browser_count; ++browser_index) { | |
| 184 scoped_refptr<BrowserProxy> browser = | |
| 185 automation()->GetBrowserWindow(browser_index); | |
| 186 if (!browser.get()) | |
| 187 continue; | |
| 188 int tab_count = 0; | |
| 189 if (!browser->GetTabCount(&tab_count)) | |
| 190 continue; | |
| 191 | |
| 192 for (int tab_index = 0; tab_index < tab_count; ++tab_index) { | |
| 193 scoped_refptr<TabProxy> tab = browser->GetTab(tab_index); | |
| 194 if (!tab.get()) | |
| 195 continue; | |
| 196 tab_id_map.insert(std::make_pair(tab->handle(), tab)); | |
| 197 } | |
| 198 } | |
| 199 | |
| 200 tab_id_map_ = tab_id_map; | |
| 201 for (TabIdMap::const_iterator iter = tab_id_map_.begin(); | |
| 202 iter != tab_id_map_.end(); | |
| 203 ++iter) { | |
| 204 tab_ids->push_back(iter->second->handle()); | |
|
Jason Leyba
2011/02/14 21:00:23
iter->first?
Also, why not build this list as you
kkania
2011/02/14 22:10:09
Done.
| |
| 205 } | |
| 206 *success = true; | |
| 207 } | |
| 208 | |
| 209 void Automation::DoesTabExist(int tab_id, bool* does_exist) { | |
| 210 TabProxy* tab = GetTabById(tab_id); | |
| 211 *does_exist = tab && tab->is_valid(); | |
| 212 } | |
| 213 | |
| 214 void Automation::CloseTab(int tab_id, bool* success) { | |
| 215 TabProxy* tab = GetTabById(tab_id); | |
| 216 if (!tab) { | |
| 217 *success = false; | |
| 218 return; | |
| 219 } | |
| 220 *success = tab->Close(true); | |
| 221 } | |
| 222 | |
| 223 TabProxy* Automation::GetTabById(int tab_id) { | |
| 224 TabIdMap::const_iterator iter = tab_id_map_.find(tab_id); | |
| 225 if (iter != tab_id_map_.end()) { | |
| 226 return iter->second.get(); | |
| 227 } | |
| 228 return NULL; | |
| 229 } | |
| 230 | |
| 123 } // namespace webdriver | 231 } // namespace webdriver |
| OLD | NEW |