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 = NULL; | |
53 if (!GetTab(tab_id, &tab, success)) | |
54 return; | |
61 std::wstring wide_xpath = UTF8ToWide(frame_xpath); | 55 std::wstring wide_xpath = UTF8ToWide(frame_xpath); |
62 std::wstring wide_script = UTF8ToWide(script); | 56 std::wstring wide_script = UTF8ToWide(script); |
63 std::wstring wide_result; | 57 std::wstring wide_result; |
64 *success = tab_->ExecuteAndExtractString( | 58 *success = tab->ExecuteAndExtractString( |
65 wide_xpath, wide_script, &wide_result); | 59 wide_xpath, wide_script, &wide_result); |
66 if (*success) | 60 if (*success) |
67 *result = WideToUTF8(wide_result); | 61 *result = WideToUTF8(wide_result); |
68 } | 62 } |
69 | 63 |
70 void Automation::SendWebKeyEvent(const WebKeyEvent& key_event, | 64 void Automation::SendWebKeyEvent(int tab_id, |
65 const WebKeyEvent& key_event, | |
71 bool* success) { | 66 bool* success) { |
67 TabProxy* tab = NULL; | |
68 if (!GetTab(tab_id, &tab, success)) | |
69 return; | |
70 int tab_index = 0; | |
71 int browser_count = 0; | |
72 if (!tab->GetTabIndex(&tab_index) || | |
73 !automation()->GetBrowserWindowCount(&browser_count)) { | |
74 *success = false; | |
75 return; | |
76 } | |
77 scoped_refptr<BrowserProxy> browser_with_tab; | |
78 for (int i = 0; i < browser_count; ++i) { | |
Paweł Hajdan Jr.
2011/02/14 09:29:43
This loop looks worrying. So far no automation cod
kkania
2011/02/14 17:42:12
Yes, I do not like this code either. I am keeping
| |
79 scoped_refptr<BrowserProxy> browser = automation()->GetBrowserWindow(i); | |
80 if (browser.get()) { | |
81 scoped_refptr<TabProxy> tab_at_index = browser->GetTab(tab_index); | |
82 if (tab_at_index->handle() == tab->handle()) { | |
83 browser_with_tab = browser; | |
84 break; | |
85 } | |
86 } | |
87 } | |
88 if (!browser_with_tab.get() || !browser_with_tab->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_with_tab->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 = NULL; |
115 if (!GetTab(tab_id, &tab, success)) | |
116 return; | |
117 *success = tab->NavigateToURL(GURL(url)); | |
93 } | 118 } |
94 | 119 |
95 void Automation::GoForward(bool* success) { | 120 void Automation::GoForward(int tab_id, bool* success) { |
96 *success = tab_->GoForward(); | 121 TabProxy* tab = NULL; |
122 if (!GetTab(tab_id, &tab, success)) | |
123 return; | |
124 *success = tab->GoForward(); | |
97 } | 125 } |
98 | 126 |
99 void Automation::GoBack(bool* success) { | 127 void Automation::GoBack(int tab_id, bool* success) { |
100 *success = tab_->GoBack(); | 128 TabProxy* tab = NULL; |
129 if (!GetTab(tab_id, &tab, success)) | |
130 return; | |
131 *success = tab->GoBack(); | |
101 } | 132 } |
102 | 133 |
103 void Automation::Reload(bool* success) { | 134 void Automation::Reload(int tab_id, bool* success) { |
104 *success = tab_->Reload(); | 135 TabProxy* tab = NULL; |
136 if (!GetTab(tab_id, &tab, success)) | |
137 return; | |
138 *success = tab->Reload(); | |
105 } | 139 } |
106 | 140 |
107 void Automation::GetURL(std::string* url, | 141 void Automation::GetURL(int tab_id, |
142 std::string* url, | |
108 bool* success) { | 143 bool* success) { |
144 TabProxy* tab = NULL; | |
145 if (!GetTab(tab_id, &tab, success)) | |
146 return; | |
109 GURL gurl; | 147 GURL gurl; |
110 *success = tab_->GetCurrentURL(&gurl); | 148 *success = tab->GetCurrentURL(&gurl); |
111 if (*success) | 149 if (*success) |
112 *url = gurl.possibly_invalid_spec(); | 150 *url = gurl.possibly_invalid_spec(); |
113 } | 151 } |
114 | 152 |
115 void Automation::GetTabTitle(std::string* tab_title, | 153 void Automation::GetTabTitle(int tab_id, |
154 std::string* tab_title, | |
116 bool* success) { | 155 bool* success) { |
156 TabProxy* tab = NULL; | |
157 if (!GetTab(tab_id, &tab, success)) | |
158 return; | |
117 std::wstring wide_title; | 159 std::wstring wide_title; |
118 *success = tab_->GetTabTitle(&wide_title); | 160 *success = tab->GetTabTitle(&wide_title); |
119 if (*success) | 161 if (*success) |
120 *tab_title = WideToUTF8(wide_title); | 162 *tab_title = WideToUTF8(wide_title); |
121 } | 163 } |
122 | 164 |
165 void Automation::GetTabIds(std::vector<int>* tab_ids, | |
Paweł Hajdan Jr.
2011/02/14 09:29:43
What is this method trying to do, and why? I can i
kkania
2011/02/14 17:42:12
Will add appropriate comments. This method impleme
| |
166 bool* success) { | |
167 *success = false; | |
168 std::vector<scoped_refptr<TabProxy> > tabs; | |
169 int browser_count = 0; | |
170 if (!automation()->GetBrowserWindowCount(&browser_count)) { | |
171 LOG(ERROR) << "Failed to get browser window count"; | |
172 return; | |
173 } | |
174 for (int browser_index = 0; browser_index < browser_count; ++browser_index) { | |
175 scoped_refptr<BrowserProxy> browser = | |
176 automation()->GetBrowserWindow(browser_index); | |
177 if (!browser.get()) | |
178 continue; | |
179 int tab_count = 0; | |
180 if (!browser->GetTabCount(&tab_count)) | |
181 continue; | |
182 | |
183 for (int tab_index = 0; tab_index < tab_count; ++tab_index) { | |
184 scoped_refptr<TabProxy> tab = browser->GetTab(tab_index); | |
185 if (!tab.get()) | |
186 continue; | |
187 tabs.push_back(tab); | |
188 } | |
189 } | |
190 | |
191 tabs_ = tabs; | |
Paweł Hajdan Jr.
2011/02/14 09:29:43
I guess bad things happen if a tab gets opened or
kkania
2011/02/14 17:42:12
It is ok if tabs_ does not get updated. We need t
| |
192 for (size_t i = 0; i < tabs_.size(); ++i) | |
193 tab_ids->push_back(tabs_[i]->handle()); | |
194 *success = true; | |
195 } | |
196 | |
197 void Automation::CloseTab(int tab_id, bool* success) { | |
198 TabProxy* tab = NULL; | |
199 if (!GetTab(tab_id, &tab, success)) | |
200 return; | |
201 *success = tab->Close(true); | |
202 } | |
203 | |
204 bool Automation::GetTab(int tab_id, TabProxy** tab, bool* success) { | |
205 for (size_t i = 0; i < tabs_.size(); ++i) { | |
Paweł Hajdan Jr.
2011/02/14 09:29:43
A linear scan doesn't seem to be an optimal soluti
kkania
2011/02/14 17:42:12
Will use a map
| |
206 if (tabs_[i]->handle() == tab_id) { | |
207 *tab = tabs_[i]; | |
208 return true; | |
Paweł Hajdan Jr.
2011/02/14 09:29:43
Now this is weird. We have _both_ a return value a
kkania
2011/02/14 17:42:12
Yes, this is a bit of a shortcut and can be confus
| |
209 } | |
210 } | |
211 *success = false; | |
212 return false; | |
213 } | |
214 | |
123 } // namespace webdriver | 215 } // namespace webdriver |
OLD | NEW |