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

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

Issue 6614023: Convert ChromeDriver to use only the JSON automation interface. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: ... Created 9 years, 9 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/automation/automation_json_requests.h" 5 #include "chrome/test/automation/automation_json_requests.h"
6 6
7 #include "base/scoped_ptr.h" 7 #include "base/scoped_ptr.h"
8 #include "base/values.h" 8 #include "base/values.h"
9 #include "base/json/json_reader.h" 9 #include "base/json/json_reader.h"
10 #include "base/json/json_writer.h" 10 #include "base/json/json_writer.h"
11 #include "chrome/common/automation_messages.h" 11 #include "chrome/common/automation_messages.h"
12 #include "chrome/common/json_value_serializer.h"
12 #include "chrome/test/automation/automation_proxy.h" 13 #include "chrome/test/automation/automation_proxy.h"
13 14
14 namespace { 15 namespace {
15 16
16 bool SendAutomationJSONRequest(AutomationMessageSender* sender, 17 bool SendAutomationJSONRequest(AutomationMessageSender* sender,
17 const DictionaryValue& request_dict, 18 const DictionaryValue& request_dict,
18 DictionaryValue* reply_dict) { 19 DictionaryValue* reply_dict) {
19 std::string request, reply; 20 std::string request, reply;
20 base::JSONWriter::Write(&request_dict, false, &request); 21 base::JSONWriter::Write(&request_dict, false, &request);
21 bool success = false; 22 bool success = false;
22 if (!SendAutomationJSONRequest(sender, request, &reply, &success)) 23 if (!SendAutomationJSONRequest(sender, request, &reply, &success))
23 return false; 24 return false;
24 if (!success) { 25 scoped_ptr<Value> value(base::JSONReader::Read(reply, true));
26 if (!value.get() || !value->IsType(Value::TYPE_DICTIONARY)) {
25 std::string command; 27 std::string command;
26 request_dict.GetString("command", &command); 28 request_dict.GetString("command", &command);
27 LOG(ERROR) << "JSON request failed: " << command; 29 LOG(ERROR) << "JSON request did not return dict: " << command << "\n";
28 return false; 30 return false;
29 } 31 }
30 scoped_ptr<Value> value(base::JSONReader::Read(reply, true)); 32 DictionaryValue* dict = static_cast<DictionaryValue*>(value.get());
31 if (!value.get() || !value->IsType(Value::TYPE_DICTIONARY)) 33 if (!success) {
34 std::string command, error;
35 request_dict.GetString("command", &command);
36 dict->GetString("error", &error);
37 LOG(ERROR) << "JSON request failed: " << command << "\n"
38 << " with error: " << error;
32 return false; 39 return false;
33 reply_dict->MergeDictionary(static_cast<DictionaryValue*>(value.get())); 40 }
41 reply_dict->MergeDictionary(dict);
34 return true; 42 return true;
35 } 43 }
36 44
37 } // namespace 45 } // namespace
38 46
47 WebKeyEvent::WebKeyEvent(automation::KeyEventTypes type,
48 ui::KeyboardCode key_code,
49 const std::string& unmodified_text,
50 const std::string& modified_text,
51 int modifiers)
52 : type(type),
53 key_code(key_code),
54 unmodified_text(unmodified_text),
55 modified_text(modified_text),
56 modifiers(modifiers) {}
57
39 bool SendAutomationJSONRequest(AutomationMessageSender* sender, 58 bool SendAutomationJSONRequest(AutomationMessageSender* sender,
40 const std::string& request, 59 const std::string& request,
41 std::string* reply, 60 std::string* reply,
42 bool* success) { 61 bool* success) {
43 return sender->Send(new AutomationMsg_SendJSONRequest( 62 return sender->Send(new AutomationMsg_SendJSONRequest(
44 -1, request, reply, success)); 63 -1, request, reply, success));
45 } 64 }
46 65
47 bool SendGetIndicesFromTabJSONRequest( 66 bool SendGetIndicesFromTabJSONRequest(
48 AutomationMessageSender* sender, 67 AutomationMessageSender* sender,
49 int handle, 68 int id_or_handle,
69 bool is_id,
50 int* browser_index, 70 int* browser_index,
51 int* tab_index) { 71 int* tab_index) {
52 DictionaryValue request_dict; 72 DictionaryValue request_dict;
53 request_dict.SetString("command", "GetIndicesFromTab"); 73 request_dict.SetString("command", "GetIndicesFromTab");
54 request_dict.SetInteger("tab_handle", handle); 74 if (is_id)
75 request_dict.SetInteger("tab_id", id_or_handle);
76 else
77 request_dict.SetInteger("tab_handle", id_or_handle);
55 DictionaryValue reply_dict; 78 DictionaryValue reply_dict;
56 if (!SendAutomationJSONRequest(sender, request_dict, &reply_dict)) 79 if (!SendAutomationJSONRequest(sender, request_dict, &reply_dict))
57 return false; 80 return false;
58 if (!reply_dict.GetInteger("windex", browser_index)) 81 if (!reply_dict.GetInteger("windex", browser_index))
59 return false; 82 return false;
60 if (!reply_dict.GetInteger("tab_index", tab_index)) 83 if (!reply_dict.GetInteger("tab_index", tab_index))
61 return false; 84 return false;
62 return true; 85 return true;
63 } 86 }
64 87
(...skipping 12 matching lines...) Expand all
77 dict.SetInteger("navigation_count", navigation_count); 100 dict.SetInteger("navigation_count", navigation_count);
78 DictionaryValue reply_dict; 101 DictionaryValue reply_dict;
79 if (!SendAutomationJSONRequest(sender, dict, &reply_dict)) 102 if (!SendAutomationJSONRequest(sender, dict, &reply_dict))
80 return false; 103 return false;
81 int response = 0; 104 int response = 0;
82 if (!reply_dict.GetInteger("result", &response)) 105 if (!reply_dict.GetInteger("result", &response))
83 return false; 106 return false;
84 *nav_response = static_cast<AutomationMsg_NavigationResponseValues>(response); 107 *nav_response = static_cast<AutomationMsg_NavigationResponseValues>(response);
85 return true; 108 return true;
86 } 109 }
110
111 bool SendExecuteJavascriptJSONRequest(
112 AutomationMessageSender* sender,
113 int browser_index,
114 int tab_index,
115 const std::string& frame_xpath,
116 const std::string& javascript,
117 Value** result) {
118 DictionaryValue dict;
119 dict.SetString("command", "ExecuteJavascript");
120 dict.SetInteger("windex", browser_index);
121 dict.SetInteger("tab_index", tab_index);
122 dict.SetString("frame_xpath", frame_xpath);
123 dict.SetString("javascript", javascript);
124 DictionaryValue reply_dict;
125 if (!SendAutomationJSONRequest(sender, dict, &reply_dict))
126 return false;
127
128 std::string json;
129 if (!reply_dict.GetString("result", &json)) {
130 LOG(ERROR) << "Executed javascript but received no 'result'";
131 return false;
132 }
133 // Wrap |json| in an array before deserializing because valid JSON has an
134 // array or an object as the root.
135 json.insert(0, "[");
136 json.append("]");
137
138 JSONStringValueSerializer deserializer(json);
139 Value* value = deserializer.Deserialize(NULL, NULL);
140 if (!value || !value->IsType(Value::TYPE_LIST)) {
141 LOG(ERROR) << "Unable to deserialize returned JSON";
142 return false;
143 }
144 scoped_ptr<ListValue> list(static_cast<ListValue*>(value));
145 return list->Remove(0, result);
146 }
147
148 bool SendGoForwardJSONRequest(
149 AutomationMessageSender* sender,
150 int browser_index,
151 int tab_index) {
152 DictionaryValue dict;
153 dict.SetString("command", "GoForward");
154 dict.SetInteger("windex", browser_index);
155 dict.SetInteger("tab_index", tab_index);
156 DictionaryValue reply_dict;
157 return SendAutomationJSONRequest(sender, dict, &reply_dict);
158 }
159
160 bool SendGoBackJSONRequest(
161 AutomationMessageSender* sender,
162 int browser_index,
163 int tab_index) {
164 DictionaryValue dict;
165 dict.SetString("command", "GoBack");
166 dict.SetInteger("windex", browser_index);
167 dict.SetInteger("tab_index", tab_index);
168 DictionaryValue reply_dict;
169 return SendAutomationJSONRequest(sender, dict, &reply_dict);
170 }
171
172 bool SendReloadJSONRequest(
173 AutomationMessageSender* sender,
174 int browser_index,
175 int tab_index) {
176 DictionaryValue dict;
177 dict.SetString("command", "Reload");
178 dict.SetInteger("windex", browser_index);
179 dict.SetInteger("tab_index", tab_index);
180 DictionaryValue reply_dict;
181 return SendAutomationJSONRequest(sender, dict, &reply_dict);
182 }
183
184 bool SendGetTabURLJSONRequest(
185 AutomationMessageSender* sender,
186 int browser_index,
187 int tab_index,
188 std::string* url) {
189 DictionaryValue dict;
190 dict.SetString("command", "GetTabURL");
191 dict.SetInteger("windex", browser_index);
192 dict.SetInteger("tab_index", tab_index);
193 DictionaryValue reply_dict;
194 if (!SendAutomationJSONRequest(sender, dict, &reply_dict))
195 return false;
196 return reply_dict.GetString("url", url);
197 }
198
199 bool SendGetTabTitleJSONRequest(
200 AutomationMessageSender* sender,
201 int browser_index,
202 int tab_index,
203 std::string* tab_title) {
204 DictionaryValue dict;
205 dict.SetString("command", "GetTabTitle");
206 dict.SetInteger("windex", browser_index);
207 dict.SetInteger("tab_index", tab_index);
208 DictionaryValue reply_dict;
209 if (!SendAutomationJSONRequest(sender, dict, &reply_dict))
210 return false;
211 return reply_dict.GetString("title", tab_title);
212 }
213
214 bool SendGetCookiesJSONRequest(
215 AutomationMessageSender* sender,
216 int browser_index,
217 const std::string& url,
218 std::string* cookies) {
219 DictionaryValue dict;
220 dict.SetString("command", "GetCookies");
221 dict.SetInteger("windex", browser_index);
222 dict.SetString("url", url);
223 DictionaryValue reply_dict;
224 if (!SendAutomationJSONRequest(sender, dict, &reply_dict))
225 return false;
226 return reply_dict.GetString("cookies", cookies);
227 }
228
229 bool SendDeleteCookieJSONRequest(
230 AutomationMessageSender* sender,
231 int browser_index,
232 const std::string& url,
233 const std::string& cookie_name) {
234 DictionaryValue dict;
235 dict.SetString("command", "DeleteCookie");
236 dict.SetInteger("windex", browser_index);
237 dict.SetString("url", url);
238 dict.SetString("name", cookie_name);
239 DictionaryValue reply_dict;
240 return SendAutomationJSONRequest(sender, dict, &reply_dict);
241 }
242
243 bool SendSetCookieJSONRequest(
244 AutomationMessageSender* sender,
245 int browser_index,
246 const std::string& url,
247 const std::string& cookie) {
248 DictionaryValue dict;
249 dict.SetString("command", "SetCookie");
250 dict.SetInteger("windex", browser_index);
251 dict.SetString("url", url);
252 dict.SetString("cookie", cookie);
253 DictionaryValue reply_dict;
254 return SendAutomationJSONRequest(sender, dict, &reply_dict);
255 }
256
257 bool SendGetTabIdsJSONRequest(
258 AutomationMessageSender* sender, std::vector<int>* tab_ids) {
259 DictionaryValue dict;
260 dict.SetString("command", "GetTabIds");
261 DictionaryValue reply_dict;
262 if (!SendAutomationJSONRequest(sender, dict, &reply_dict))
263 return false;
264 ListValue* id_list;
265 if (!reply_dict.GetList("ids", &id_list)) {
266 LOG(ERROR) << "Returned 'ids' key is missing or invalid";
267 return false;
268 }
269 std::vector<int> temp_ids;
270 for (size_t i = 0; i < id_list->GetSize(); ++i) {
271 int id;
272 if (!id_list->GetInteger(i, &id)) {
273 LOG(ERROR) << "Returned 'ids' key contains non-integer values";
274 return false;
275 }
276 temp_ids.push_back(id);
277 }
278 *tab_ids = temp_ids;
279 return true;
280 }
281
282 bool SendIsTabIdValidJSONRequest(
283 AutomationMessageSender* sender, int tab_id, bool* is_valid) {
284 DictionaryValue dict;
285 dict.SetString("command", "IsTabIdValid");
286 dict.SetInteger("id", tab_id);
287 DictionaryValue reply_dict;
288 if (!SendAutomationJSONRequest(sender, dict, &reply_dict))
289 return false;
290 return reply_dict.GetBoolean("is_valid", is_valid);
291 }
292
293 bool SendCloseTabJSONRequest(
294 AutomationMessageSender* sender, int browser_index, int tab_index) {
295 DictionaryValue dict;
296 dict.SetString("command", "CloseTab");
297 dict.SetInteger("windex", browser_index);
298 dict.SetInteger("tab_index", tab_index);
299 DictionaryValue reply_dict;
300 return SendAutomationJSONRequest(sender, dict, &reply_dict);
301 }
302
303 bool SendMouseMoveJSONRequest(
304 AutomationMessageSender* sender,
305 int browser_index,
306 int tab_index,
307 int x,
308 int y) {
309 DictionaryValue dict;
310 dict.SetString("command", "WebkitMouseMove");
311 dict.SetInteger("windex", browser_index);
312 dict.SetInteger("tab_index", tab_index);
313 dict.SetInteger("x", x);
314 dict.SetInteger("y", y);
315 DictionaryValue reply_dict;
316 return SendAutomationJSONRequest(sender, dict, &reply_dict);
317 }
318
319 bool SendMouseClickJSONRequest(
320 AutomationMessageSender* sender,
321 int browser_index,
322 int tab_index,
323 automation::MouseButton button,
324 int x,
325 int y) {
326 // TODO get rid of the evil flags.
327 DictionaryValue dict;
328 dict.SetString("command", "WebkitMouseClick");
329 dict.SetInteger("windex", browser_index);
330 dict.SetInteger("tab_index", tab_index);
331 dict.SetInteger("button", button);
332 dict.SetInteger("x", x);
333 dict.SetInteger("y", y);
334 DictionaryValue reply_dict;
335 return SendAutomationJSONRequest(sender, dict, &reply_dict);
336 }
337
338 bool SendMouseDragJSONRequest(
339 AutomationMessageSender* sender,
340 int browser_index,
341 int tab_index,
342 int start_x,
343 int start_y,
344 int end_x,
345 int end_y) {
346 DictionaryValue dict;
347 dict.SetString("command", "WebkitMouseDrag");
348 dict.SetInteger("windex", browser_index);
349 dict.SetInteger("tab_index", tab_index);
350 dict.SetInteger("start_x", start_x);
351 dict.SetInteger("start_y", start_y);
352 dict.SetInteger("end_x", end_x);
353 dict.SetInteger("end_y", end_y);
354 DictionaryValue reply_dict;
355 return SendAutomationJSONRequest(sender, dict, &reply_dict);
356 }
357
358 bool SendWebKeyEventJSONRequest(
359 AutomationMessageSender* sender,
360 int browser_index,
361 int tab_index,
362 const WebKeyEvent& key_event) {
363 DictionaryValue dict;
364 dict.SetString("command", "SendWebkitKeyEvent");
365 dict.SetInteger("windex", browser_index);
366 dict.SetInteger("tab_index", tab_index);
367 dict.SetInteger("type", key_event.type);
368 dict.SetInteger("nativeKeyCode", key_event.key_code);
369 dict.SetInteger("windowsKeyCode", key_event.key_code);
370 dict.SetString("unmodifiedText", key_event.unmodified_text);
371 dict.SetString("text", key_event.modified_text);
372 dict.SetInteger("modifiers", key_event.modifiers);
373 dict.SetBoolean("isSystemKey", false);
374 DictionaryValue reply_dict;
375 return SendAutomationJSONRequest(sender, dict, &reply_dict);
376 }
377
378 bool SendWaitForAllTabsToStopLoadingJSONRequest(
379 AutomationMessageSender* sender) {
380 DictionaryValue dict;
381 dict.SetString("command", "WaitForAllTabsToStopLoading");
382 DictionaryValue reply_dict;
383 return SendAutomationJSONRequest(sender, dict, &reply_dict);
384 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698