1/*
2 * Copyright (C) 2017 Igalia S.L.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#pragma once
27
28#include "Actions.h"
29#include "Capabilities.h"
30#include <wtf/Forward.h>
31#include <wtf/Function.h>
32#include <wtf/JSONValues.h>
33#include <wtf/OptionSet.h>
34#include <wtf/RefCounted.h>
35#include <wtf/text/WTFString.h>
36
37namespace WebDriver {
38
39class CommandResult;
40class SessionHost;
41
42class Session : public RefCounted<Session> {
43public:
44 static Ref<Session> create(std::unique_ptr<SessionHost>&& host)
45 {
46 return adoptRef(*new Session(WTFMove(host)));
47 }
48 ~Session();
49
50 const String& id() const;
51 const Capabilities& capabilities() const;
52 bool isConnected() const;
53 Seconds scriptTimeout() const { return m_scriptTimeout; }
54 Seconds pageLoadTimeout() const { return m_pageLoadTimeout; }
55 Seconds implicitWaitTimeout() const { return m_implicitWaitTimeout; }
56 static const String& webElementIdentifier();
57
58 enum class FindElementsMode { Single, Multiple };
59 enum class ExecuteScriptMode { Sync, Async };
60
61 struct Cookie {
62 String name;
63 String value;
64 Optional<String> path;
65 Optional<String> domain;
66 Optional<bool> secure;
67 Optional<bool> httpOnly;
68 Optional<uint64_t> expiry;
69 };
70
71 InputSource& getOrCreateInputSource(const String& id, InputSource::Type, Optional<PointerType>);
72
73 void waitForNavigationToComplete(Function<void (CommandResult&&)>&&);
74 void createTopLevelBrowsingContext(Function<void (CommandResult&&)>&&);
75 void close(Function<void (CommandResult&&)>&&);
76 void getTimeouts(Function<void (CommandResult&&)>&&);
77 void setTimeouts(const Timeouts&, Function<void (CommandResult&&)>&&);
78
79 void go(const String& url, Function<void (CommandResult&&)>&&);
80 void getCurrentURL(Function<void (CommandResult&&)>&&);
81 void back(Function<void (CommandResult&&)>&&);
82 void forward(Function<void (CommandResult&&)>&&);
83 void refresh(Function<void (CommandResult&&)>&&);
84 void getTitle(Function<void (CommandResult&&)>&&);
85 void getWindowHandle(Function<void (CommandResult&&)>&&);
86 void closeWindow(Function<void (CommandResult&&)>&&);
87 void switchToWindow(const String& windowHandle, Function<void (CommandResult&&)>&&);
88 void getWindowHandles(Function<void (CommandResult&&)>&&);
89 void switchToFrame(RefPtr<JSON::Value>&&, Function<void (CommandResult&&)>&&);
90 void switchToParentFrame(Function<void (CommandResult&&)>&&);
91 void getWindowRect(Function<void (CommandResult&&)>&&);
92 void setWindowRect(Optional<double> x, Optional<double> y, Optional<double> width, Optional<double> height, Function<void (CommandResult&&)>&&);
93 void maximizeWindow(Function<void (CommandResult&&)>&&);
94 void minimizeWindow(Function<void (CommandResult&&)>&&);
95 void fullscreenWindow(Function<void (CommandResult&&)>&&);
96 void findElements(const String& strategy, const String& selector, FindElementsMode, const String& rootElementID, Function<void (CommandResult&&)>&&);
97 void getActiveElement(Function<void (CommandResult&&)>&&);
98 void isElementSelected(const String& elementID, Function<void (CommandResult&&)>&&);
99 void getElementAttribute(const String& elementID, const String& attribute, Function<void (CommandResult&&)>&&);
100 void getElementProperty(const String& elementID, const String& attribute, Function<void (CommandResult&&)>&&);
101 void getElementCSSValue(const String& elementID, const String& cssProperty, Function<void (CommandResult&&)>&&);
102 void getElementText(const String& elementID, Function<void (CommandResult&&)>&&);
103 void getElementTagName(const String& elementID, Function<void (CommandResult&&)>&&);
104 void getElementRect(const String& elementID, Function<void (CommandResult&&)>&&);
105 void isElementEnabled(const String& elementID, Function<void (CommandResult&&)>&&);
106 void isElementDisplayed(const String& elementID, Function<void (CommandResult&&)>&&);
107 void elementClick(const String& elementID, Function<void (CommandResult&&)>&&);
108 void elementClear(const String& elementID, Function<void (CommandResult&&)>&&);
109 void elementSendKeys(const String& elementID, const String& text, Function<void (CommandResult&&)>&&);
110 void executeScript(const String& script, RefPtr<JSON::Array>&& arguments, ExecuteScriptMode, Function<void (CommandResult&&)>&&);
111 void getAllCookies(Function<void (CommandResult&&)>&&);
112 void getNamedCookie(const String& name, Function<void (CommandResult&&)>&&);
113 void addCookie(const Cookie&, Function<void (CommandResult&&)>&&);
114 void deleteCookie(const String& name, Function<void (CommandResult&&)>&&);
115 void deleteAllCookies(Function<void (CommandResult&&)>&&);
116 void performActions(Vector<Vector<Action>>&&, Function<void (CommandResult&&)>&&);
117 void releaseActions(Function<void (CommandResult&&)>&&);
118 void dismissAlert(Function<void (CommandResult&&)>&&);
119 void acceptAlert(Function<void (CommandResult&&)>&&);
120 void getAlertText(Function<void (CommandResult&&)>&&);
121 void sendAlertText(const String&, Function<void (CommandResult&&)>&&);
122 void takeScreenshot(Optional<String> elementID, Optional<bool> scrollIntoView, Function<void (CommandResult&&)>&&);
123
124private:
125 Session(std::unique_ptr<SessionHost>&&);
126
127 void switchToTopLevelBrowsingContext(Optional<String>);
128 void switchToBrowsingContext(Optional<String>);
129 void closeTopLevelBrowsingContext(const String& toplevelBrowsingContext, Function<void (CommandResult&&)>&&);
130 void closeAllToplevelBrowsingContexts(const String& toplevelBrowsingContext, Function<void (CommandResult&&)>&&);
131
132 void getToplevelBrowsingContextRect(Function<void (CommandResult&&)>&&);
133
134 Optional<String> pageLoadStrategyString() const;
135
136 void handleUserPrompts(Function<void (CommandResult&&)>&&);
137 void handleUnexpectedAlertOpen(Function<void (CommandResult&&)>&&);
138 void dismissAndNotifyAlert(Function<void (CommandResult&&)>&&);
139 void acceptAndNotifyAlert(Function<void (CommandResult&&)>&&);
140 void reportUnexpectedAlertOpen(Function<void (CommandResult&&)>&&);
141
142 RefPtr<JSON::Object> createElement(RefPtr<JSON::Value>&&);
143 RefPtr<JSON::Object> createElement(const String& elementID);
144 RefPtr<JSON::Object> extractElement(JSON::Value&);
145 String extractElementID(JSON::Value&);
146 RefPtr<JSON::Value> handleScriptResult(RefPtr<JSON::Value>&&);
147
148 struct Point {
149 int x { 0 };
150 int y { 0 };
151 };
152
153 struct Size {
154 int width { 0 };
155 int height { 0 };
156 };
157
158 struct Rect {
159 Point origin;
160 Size size;
161 };
162
163 enum class ElementLayoutOption {
164 ScrollIntoViewIfNeeded = 1 << 0,
165 UseViewportCoordinates = 1 << 1,
166 };
167 void computeElementLayout(const String& elementID, OptionSet<ElementLayoutOption>, Function<void (Optional<Rect>&&, Optional<Point>&&, bool, RefPtr<JSON::Object>&&)>&&);
168
169 void selectOptionElement(const String& elementID, Function<void (CommandResult&&)>&&);
170
171 enum class MouseInteraction { Move, Down, Up, SingleClick, DoubleClick };
172 void performMouseInteraction(int x, int y, MouseButton, MouseInteraction, Function<void (CommandResult&&)>&&);
173
174 enum class KeyboardInteractionType { KeyPress, KeyRelease, InsertByKey };
175 struct KeyboardInteraction {
176 KeyboardInteractionType type { KeyboardInteractionType::InsertByKey };
177 Optional<String> text;
178 Optional<String> key;
179 };
180 enum KeyModifier {
181 None = 0,
182 Shift = 1 << 0,
183 Control = 1 << 1,
184 Alternate = 1 << 2,
185 Meta = 1 << 3,
186 };
187 String virtualKeyForKey(UChar, KeyModifier&);
188 void performKeyboardInteractions(Vector<KeyboardInteraction>&&, Function<void (CommandResult&&)>&&);
189
190 struct InputSourceState {
191 enum class Type { Null, Key, Pointer };
192
193 Type type;
194 String subtype;
195 Optional<MouseButton> pressedButton;
196 Optional<String> pressedKey;
197 Optional<String> pressedVirtualKey;
198 };
199 InputSourceState& inputSourceState(const String& id);
200
201 std::unique_ptr<SessionHost> m_host;
202 Seconds m_scriptTimeout;
203 Seconds m_pageLoadTimeout;
204 Seconds m_implicitWaitTimeout;
205 Optional<String> m_toplevelBrowsingContext;
206 Optional<String> m_currentBrowsingContext;
207 HashMap<String, InputSource> m_activeInputSources;
208 HashMap<String, InputSourceState> m_inputStateTable;
209};
210
211} // WebDriver
212