1// Copyright (c) 2012 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef CHROME_TEST_WEBDRIVER_WEBDRIVER_AUTOMATION_H_
6#define CHROME_TEST_WEBDRIVER_WEBDRIVER_AUTOMATION_H_
7
8#include <map>
9#include <string>
10#include <vector>
11
12#include "base/command_line.h"
13#include "base/files/file_path.h"
14#include "base/memory/ref_counted.h"
15#include "base/memory/scoped_ptr.h"
16#include "chrome/common/automation_constants.h"
17#include "chrome/test/webdriver/webdriver_logging.h"
18#include "ui/base/keycodes/keyboard_codes.h"
19
20class AutomationId;
21class AutomationProxy;
22class ProxyLauncher;
23struct WebKeyEvent;
24struct WebMouseEvent;
25class WebViewId;
26struct WebViewInfo;
27class WebViewLocator;
28
29namespace base {
30class DictionaryValue;
31class ListValue;
32class Value;
33}
34
35namespace webdriver {
36
37class Error;
38class FramePath;
39class Point;
40class Rect;
41
42// Creates and controls the Chrome instance.
43// This class should be created and accessed on a single thread.
44// Note: All member functions are void because they are invoked
45// by posting a task.
46class Automation {
47 public:
48  struct BrowserOptions {
49    BrowserOptions();
50    ~BrowserOptions();
51
52    // The command line to use for launching the browser. If no program is
53    // specified, the default browser executable will be used.
54    CommandLine command;
55
56    // The user data directory to be copied and used. If empty, a temporary
57    // directory will be used.
58    base::FilePath user_data_dir;
59
60    // The channel ID of an already running browser to connect to. If empty,
61    // the browser will be launched with an anonymous channel.
62    std::string channel_id;
63
64    // True if the Chrome process should only be terminated if quit is called.
65    // If false, Chrome will also be terminated if this process is killed or
66    // shutdown.
67    bool detach_process;
68
69    // True if the browser should ignore certificate related errors.
70    bool ignore_certificate_errors;
71
72    // Set of switches to be excluded from default list when starting Chrome.
73    std::set<std::string> exclude_switches;
74  };
75
76  explicit Automation(const Logger& logger);
77  virtual ~Automation();
78
79  // Start the system's default Chrome binary.
80  void Init(const BrowserOptions& options, int* build_no, Error** error);
81
82  // Terminates this session and disconnects its automation proxy. After
83  // invoking this method, the Automation can safely be deleted.
84  void Terminate();
85
86  // Executes the given |script| in the specified frame of the current
87  // tab. |result| will be set to the JSON result. Returns true on success.
88  void ExecuteScript(const WebViewId& view_id,
89                     const FramePath& frame_path,
90                     const std::string& script,
91                     std::string* result,
92                     Error** error);
93
94  // Sends a webkit key event to the current browser. Waits until the key has
95  // been processed by the web page.
96  void SendWebKeyEvent(const WebViewId& view_id,
97                       const WebKeyEvent& key_event,
98                       Error** error);
99
100  // Sends a web mouse event to the given view. Waits until the event has
101  // been processed by the view.
102  void SendWebMouseEvent(const WebViewId& view_id,
103                         const WebMouseEvent& event,
104                         Error** error);
105
106  // Drag and drop the file paths to the given location.
107  void DragAndDropFilePaths(
108      const WebViewId& view_id,
109      const Point& location,
110      const std::vector<base::FilePath::StringType>& paths,
111      Error** error);
112
113  // Captures a snapshot of the tab to the specified path.  The  PNG will
114  // contain the entire page, including what is not in the current view
115  // on the  screen.
116  void CaptureEntirePageAsPNG(
117      const WebViewId& view_id, const base::FilePath& path, Error** error);
118
119#if !defined(NO_TCMALLOC) && (defined(OS_LINUX) || defined(OS_CHROMEOS))
120  // Dumps a heap profile of the process of the tab.
121  void HeapProfilerDump(
122      const WebViewId& view_id, const std::string& reason, Error** error);
123#endif  // !defined(NO_TCMALLOC) && (defined(OS_LINUX) || defined(OS_CHROMEOS))
124
125  void NavigateToURL(
126      const WebViewId& view_id, const std::string& url, Error** error);
127  void NavigateToURLAsync(
128      const WebViewId& view_id, const std::string& url, Error** error);
129  void GoForward(const WebViewId& view_id, Error** error);
130  void GoBack(const WebViewId& view_id, Error** error);
131  void Reload(const WebViewId& view_id, Error** error);
132
133  void GetCookies(const std::string& url,
134                  scoped_ptr<base::ListValue>* cookies,
135                  Error** error);
136  void DeleteCookie(const std::string& url,
137                    const std::string& cookie_name,
138                    Error** error);
139  void SetCookie(const std::string& url,
140                 base::DictionaryValue* cookie_dict,
141                 Error** error);
142
143  // TODO(kkania): All of these mouse commands are deprecated and should be
144  // removed when chrome build 1002 is no longer supported.
145  // Use SendWebMouseEvent instead.
146  void MouseMoveDeprecated(const WebViewId& view_id,
147                           const Point& p,
148                           Error** error);
149  void MouseClickDeprecated(const WebViewId& view_id,
150                            const Point& p,
151                            automation::MouseButton button,
152                            Error** error);
153  void MouseDragDeprecated(const WebViewId& view_id,
154                           const Point& start,
155                           const Point& end,
156                           Error** error);
157  void MouseButtonDownDeprecated(const WebViewId& view_id,
158                                 const Point& p,
159                                 Error** error);
160  void MouseButtonUpDeprecated(const WebViewId& view_id,
161                               const Point& p,
162                               Error** error);
163  void MouseDoubleClickDeprecated(const WebViewId& view_id,
164                                  const Point& p,
165                                  Error** error);
166
167  // Get info for all views currently open.
168  void GetViews(std::vector<WebViewInfo>* views, Error** error);
169
170  // Check if the given view exists currently.
171  void DoesViewExist(const WebViewId& view_id, bool* does_exist, Error** error);
172
173  // Closes the given view.
174  void CloseView(const WebViewId& view_id, Error** error);
175
176  // Sets the bounds for the given view. The position should be in screen
177  // coordinates, while the size should be the desired size of the view.
178  void SetViewBounds(const WebViewId& view_id,
179                     const Rect& bounds,
180                     Error** error);
181
182  // Maximizes the given view.
183  void MaximizeView(const WebViewId& view_id, Error** error);
184
185  // Gets the active JavaScript modal dialog's message.
186  void GetAppModalDialogMessage(std::string* message, Error** error);
187
188  // Accepts or dismisses the active JavaScript modal dialog.
189  void AcceptOrDismissAppModalDialog(bool accept, Error** error);
190
191  // Accepts an active prompt JavaScript modal dialog, using the given
192  // prompt text as the result of the prompt.
193  void AcceptPromptAppModalDialog(const std::string& prompt_text,
194                                  Error** error);
195
196  // Gets the version of the runing browser.
197  void GetBrowserVersion(std::string* version);
198
199  // Gets the ChromeDriver automation version supported by the automation
200  // server.
201  void GetChromeDriverAutomationVersion(int* version, Error** error);
202
203  // Waits for all views to stop loading.
204  void WaitForAllViewsToStopLoading(Error** error);
205
206  // Install a packed or unpacked extension. If the path ends with '.crx',
207  // the extension is assumed to be packed.
208  void InstallExtension(const base::FilePath& path, std::string* extension_id,
209                        Error** error);
210
211  // Gets a list of dictionary information about all installed extensions.
212  void GetExtensionsInfo(base::ListValue* extensions_list, Error** error);
213
214  // Gets a list of dictionary information about all installed extensions.
215  void IsPageActionVisible(const WebViewId& tab_id,
216                           const std::string& extension_id,
217                           bool* is_visible,
218                           Error** error);
219
220  // Sets whether the extension is enabled or not.
221  void SetExtensionState(const std::string& extension_id,
222                         bool enable,
223                         Error** error);
224
225  // Clicks the extension action button. If |browser_action| is false, the
226  // page action will be clicked.
227  void ClickExtensionButton(const std::string& extension_id,
228                            bool browser_action,
229                            Error** error);
230
231  // Uninstalls the given extension.
232  void UninstallExtension(const std::string& extension_id, Error** error);
233
234  // Set a local state preference, which is not associated with any profile.
235  // Ownership of |value| is taken by this function.
236  void SetLocalStatePreference(const std::string& pref,
237                               base::Value* value,
238                               Error** error);
239
240  // Set a user preference, which is associated with the current profile.
241  // Ownership of |value| is taken by this fucntion.
242  void SetPreference(const std::string& pref,
243                     base::Value* value,
244                     Error** error);
245
246  // Gets the current geolocation.
247  void GetGeolocation(scoped_ptr<base::DictionaryValue>* geolocation,
248                      Error** error);
249
250  // Overrides the current geolocation.
251  void OverrideGeolocation(const base::DictionaryValue* geolocation,
252                           Error** error);
253
254 private:
255  AutomationProxy* automation() const;
256  Error* ConvertViewIdToLocator(const WebViewId& view_id,
257                                WebViewLocator* view_locator);
258  Error* DetermineBuildNumber();
259  Error* CheckVersion(int min_required_build_no,
260                      const std::string& error_msg);
261  Error* CheckAlertsSupported();
262  Error* CheckAdvancedInteractionsSupported();
263  Error* CheckNewExtensionInterfaceSupported();
264  Error* CheckGeolocationSupported();
265  Error* CheckMaximizeSupported();
266  Error* IsNewMouseApiSupported(bool* supports_new_api);
267
268  const Logger& logger_;
269  scoped_ptr<ProxyLauncher> launcher_;
270  int build_no_;
271  scoped_ptr<base::DictionaryValue> geolocation_;
272
273  DISALLOW_COPY_AND_ASSIGN(Automation);
274};
275
276}  // namespace webdriver
277
278#endif  // CHROME_TEST_WEBDRIVER_WEBDRIVER_AUTOMATION_H_
279