WebDriverConnection.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. /*
  2. * Copyright (c) 2022, Florent Castelli <florent.castelli@gmail.com>
  3. * Copyright (c) 2022, Sam Atkins <atkinssj@serenityos.org>
  4. * Copyright (c) 2022, Tobias Christiansen <tobyase@serenityos.org>
  5. * Copyright (c) 2022, Linus Groh <linusg@serenityos.org>
  6. * Copyright (c) 2022, Tim Flynn <trflynn89@serenityos.org>
  7. *
  8. * SPDX-License-Identifier: BSD-2-Clause
  9. */
  10. #include <AK/JsonObject.h>
  11. #include <AK/JsonValue.h>
  12. #include <AK/Vector.h>
  13. #include <LibWeb/DOM/Document.h>
  14. #include <LibWeb/HTML/BrowsingContext.h>
  15. #include <LibWeb/Page/Page.h>
  16. #include <LibWeb/Platform/EventLoopPlugin.h>
  17. #include <LibWeb/Platform/Timer.h>
  18. #include <WebContent/ConnectionFromClient.h>
  19. #include <WebContent/PageHost.h>
  20. #include <WebContent/WebDriverConnection.h>
  21. namespace WebContent {
  22. static JsonValue make_success_response(JsonValue value)
  23. {
  24. JsonObject result;
  25. result.set("value", move(value));
  26. return result;
  27. }
  28. static JsonValue serialize_rect(Gfx::IntRect const& rect)
  29. {
  30. JsonObject serialized_rect = {};
  31. serialized_rect.set("x", rect.x());
  32. serialized_rect.set("y", rect.y());
  33. serialized_rect.set("width", rect.width());
  34. serialized_rect.set("height", rect.height());
  35. return make_success_response(move(serialized_rect));
  36. }
  37. static Gfx::IntRect compute_window_rect(Web::Page const& page)
  38. {
  39. return {
  40. page.window_position().x(),
  41. page.window_position().y(),
  42. page.window_size().width(),
  43. page.window_size().height()
  44. };
  45. }
  46. // https://w3c.github.io/webdriver/#dfn-get-or-create-a-web-element-reference
  47. static String get_or_create_a_web_element_reference(Web::DOM::Node const& element)
  48. {
  49. // FIXME: 1. For each known element of the current browsing context’s list of known elements:
  50. // FIXME: 1. If known element equals element, return success with known element’s web element reference.
  51. // FIXME: 2. Add element to the list of known elements of the current browsing context.
  52. // FIXME: 3. Return success with the element’s web element reference.
  53. return String::number(element.id());
  54. }
  55. // https://w3c.github.io/webdriver/#dfn-web-element-reference-object
  56. static JsonObject web_element_reference_object(Web::DOM::Node const& element)
  57. {
  58. // https://w3c.github.io/webdriver/#dfn-web-element-identifier
  59. static String const web_element_identifier = "element-6066-11e4-a52e-4f735466cecf"sv;
  60. // 1. Let identifier be the web element identifier.
  61. auto identifier = web_element_identifier;
  62. // 2. Let reference be the result of get or create a web element reference given element.
  63. auto reference = get_or_create_a_web_element_reference(element);
  64. // 3. Return a JSON Object initialized with a property with name identifier and value reference.
  65. JsonObject object;
  66. object.set("name"sv, identifier);
  67. object.set("value"sv, reference);
  68. return object;
  69. }
  70. static ErrorOr<String, Web::WebDriver::Error> get_property(JsonValue const& payload, StringView key)
  71. {
  72. if (!payload.is_object())
  73. return Web::WebDriver::Error::from_code(Web::WebDriver::ErrorCode::InvalidArgument, "Payload is not a JSON object");
  74. auto const* property = payload.as_object().get_ptr(key);
  75. if (!property)
  76. return Web::WebDriver::Error::from_code(Web::WebDriver::ErrorCode::InvalidArgument, String::formatted("No property called '{}' present", key));
  77. if (!property->is_string())
  78. return Web::WebDriver::Error::from_code(Web::WebDriver::ErrorCode::InvalidArgument, String::formatted("Property '{}' is not a String", key));
  79. return property->as_string();
  80. }
  81. ErrorOr<NonnullRefPtr<WebDriverConnection>> WebDriverConnection::connect(ConnectionFromClient& web_content_client, PageHost& page_host, String const& webdriver_ipc_path)
  82. {
  83. dbgln_if(WEBDRIVER_DEBUG, "Trying to connect to {}", webdriver_ipc_path);
  84. auto socket = TRY(Core::Stream::LocalSocket::connect(webdriver_ipc_path));
  85. dbgln_if(WEBDRIVER_DEBUG, "Connected to WebDriver");
  86. return adopt_nonnull_ref_or_enomem(new (nothrow) WebDriverConnection(move(socket), web_content_client, page_host));
  87. }
  88. WebDriverConnection::WebDriverConnection(NonnullOwnPtr<Core::Stream::LocalSocket> socket, ConnectionFromClient& web_content_client, PageHost& page_host)
  89. : IPC::ConnectionToServer<WebDriverClientEndpoint, WebDriverServerEndpoint>(*this, move(socket))
  90. , m_web_content_client(web_content_client)
  91. , m_page_host(page_host)
  92. {
  93. }
  94. // https://w3c.github.io/webdriver/#dfn-close-the-session
  95. void WebDriverConnection::close_session()
  96. {
  97. // 1. Set the webdriver-active flag to false.
  98. set_is_webdriver_active(false);
  99. // 2. An endpoint node must close any top-level browsing contexts associated with the session, without prompting to unload.
  100. m_page_host.page().top_level_browsing_context().close();
  101. }
  102. void WebDriverConnection::set_is_webdriver_active(bool is_webdriver_active)
  103. {
  104. m_page_host.set_is_webdriver_active(is_webdriver_active);
  105. }
  106. // 10.1 Navigate To, https://w3c.github.io/webdriver/#navigate-to
  107. Messages::WebDriverClient::NavigateToResponse WebDriverConnection::navigate_to(JsonValue const& payload)
  108. {
  109. dbgln_if(WEBDRIVER_DEBUG, "WebDriverConnection::navigate_to {}", payload);
  110. // 1. If the current top-level browsing context is no longer open, return error with error code no such window.
  111. TRY(ensure_open_top_level_browsing_context());
  112. // 2. Let url be the result of getting the property url from the parameters argument.
  113. if (!payload.is_object() || !payload.as_object().has_string("url"sv))
  114. return Web::WebDriver::Error::from_code(Web::WebDriver::ErrorCode::InvalidArgument, "Payload doesn't have a string `url`"sv);
  115. URL url(payload.as_object().get_ptr("url"sv)->as_string());
  116. // FIXME: 3. If url is not an absolute URL or is not an absolute URL with fragment or not a local scheme, return error with error code invalid argument.
  117. // FIXME: 4. Handle any user prompts and return its value if it is an error.
  118. // FIXME: 5. Let current URL be the current top-level browsing context’s active document’s URL.
  119. // FIXME: 6. If current URL and url do not have the same absolute URL:
  120. // FIXME: a. If timer has not been started, start a timer. If this algorithm has not completed before timer reaches the session’s session page load timeout in milliseconds, return an error with error code timeout.
  121. // 7. Navigate the current top-level browsing context to url.
  122. m_page_host.page().load(url);
  123. // FIXME: 8. If url is special except for file and current URL and URL do not have the same absolute URL:
  124. // FIXME: a. Try to wait for navigation to complete.
  125. // FIXME: b. Try to run the post-navigation checks.
  126. // FIXME: 9. Set the current browsing context with the current top-level browsing context.
  127. // FIXME: 10. If the current top-level browsing context contains a refresh state pragma directive of time 1 second or less, wait until the refresh timeout has elapsed, a new navigate has begun, and return to the first step of this algorithm.
  128. // 11. Return success with data null.
  129. return make_success_response({});
  130. }
  131. // 10.2 Get Current URL, https://w3c.github.io/webdriver/#get-current-url
  132. Messages::WebDriverClient::GetCurrentUrlResponse WebDriverConnection::get_current_url()
  133. {
  134. dbgln_if(WEBDRIVER_DEBUG, "WebDriverConnection::get_current_url");
  135. // 1. If the current top-level browsing context is no longer open, return error with error code no such window.
  136. TRY(ensure_open_top_level_browsing_context());
  137. // FIXME: 2. Handle any user prompts and return its value if it is an error.
  138. // 3. Let url be the serialization of the current top-level browsing context’s active document’s document URL.
  139. auto url = m_page_host.page().top_level_browsing_context().active_document()->url().to_string();
  140. // 4. Return success with data url.
  141. return make_success_response(url);
  142. }
  143. // 11.8.1 Get Window Rect, https://w3c.github.io/webdriver/#dfn-get-window-rect
  144. Messages::WebDriverClient::GetWindowRectResponse WebDriverConnection::get_window_rect()
  145. {
  146. // 1. If the current top-level browsing context is no longer open, return error with error code no such window.
  147. TRY(ensure_open_top_level_browsing_context());
  148. // FIXME: 2. Handle any user prompts and return its value if it is an error.
  149. // 3. Return success with data set to the WindowRect object for the current top-level browsing context.
  150. return serialize_rect(compute_window_rect(m_page_host.page()));
  151. }
  152. // 11.8.2 Set Window Rect, https://w3c.github.io/webdriver/#dfn-set-window-rect
  153. Messages::WebDriverClient::SetWindowRectResponse WebDriverConnection::set_window_rect(JsonValue const& payload)
  154. {
  155. if (!payload.is_object())
  156. return Web::WebDriver::Error::from_code(Web::WebDriver::ErrorCode::InvalidArgument, "Payload is not a JSON object");
  157. auto const& properties = payload.as_object();
  158. auto resolve_property = [](auto name, auto const* property, auto min, auto max) -> ErrorOr<Optional<i32>, Web::WebDriver::Error> {
  159. if (!property)
  160. return Optional<i32> {};
  161. if (!property->is_number())
  162. return Web::WebDriver::Error::from_code(Web::WebDriver::ErrorCode::InvalidArgument, String::formatted("Property '{}' is not a Number", name));
  163. auto number = property->template to_number<i64>();
  164. if (number < min)
  165. return Web::WebDriver::Error::from_code(Web::WebDriver::ErrorCode::InvalidArgument, String::formatted("Property '{}' value {} exceeds the minimum allowed value {}", name, number, min));
  166. if (number > max)
  167. return Web::WebDriver::Error::from_code(Web::WebDriver::ErrorCode::InvalidArgument, String::formatted("Property '{}' value {} exceeds the maximum allowed value {}", name, number, max));
  168. return static_cast<i32>(number);
  169. };
  170. // 1. Let width be the result of getting a property named width from the parameters argument, else let it be null.
  171. auto const* width_property = properties.get_ptr("width"sv);
  172. // 2. Let height be the result of getting a property named height from the parameters argument, else let it be null.
  173. auto const* height_property = properties.get_ptr("height"sv);
  174. // 3. Let x be the result of getting a property named x from the parameters argument, else let it be null.
  175. auto const* x_property = properties.get_ptr("x"sv);
  176. // 4. Let y be the result of getting a property named y from the parameters argument, else let it be null.
  177. auto const* y_property = properties.get_ptr("y"sv);
  178. // 5. If width or height is neither null nor a Number from 0 to 2^31 − 1, return error with error code invalid argument.
  179. auto width = TRY(resolve_property("width"sv, width_property, 0, NumericLimits<i32>::max()));
  180. auto height = TRY(resolve_property("height"sv, height_property, 0, NumericLimits<i32>::max()));
  181. // 6. If x or y is neither null nor a Number from −(2^31) to 2^31 − 1, return error with error code invalid argument.
  182. auto x = TRY(resolve_property("x"sv, x_property, NumericLimits<i32>::min(), NumericLimits<i32>::max()));
  183. auto y = TRY(resolve_property("y"sv, y_property, NumericLimits<i32>::min(), NumericLimits<i32>::max()));
  184. // 7. If the remote end does not support the Set Window Rect command for the current top-level browsing context for any reason, return error with error code unsupported operation.
  185. // 8. If the current top-level browsing context is no longer open, return error with error code no such window.
  186. TRY(ensure_open_top_level_browsing_context());
  187. // FIXME: 9. Handle any user prompts and return its value if it is an error.
  188. // FIXME: 10. Fully exit fullscreen.
  189. // 11. Restore the window.
  190. restore_the_window();
  191. Gfx::IntRect window_rect;
  192. // 11. If width and height are not null:
  193. if (width.has_value() && height.has_value()) {
  194. // a. Set the width, in CSS pixels, of the operating system window containing the current top-level browsing context, including any browser chrome and externally drawn window decorations to a value that is as close as possible to width.
  195. // b. Set the height, in CSS pixels, of the operating system window containing the current top-level browsing context, including any browser chrome and externally drawn window decorations to a value that is as close as possible to height.
  196. auto size = m_web_content_client.did_request_resize_window({ *width, *height });
  197. window_rect.set_size(size);
  198. } else {
  199. window_rect.set_size(m_page_host.page().window_size());
  200. }
  201. // 12. If x and y are not null:
  202. if (x.has_value() && y.has_value()) {
  203. // a. Run the implementation-specific steps to set the position of the operating system level window containing the current top-level browsing context to the position given by the x and y coordinates.
  204. auto position = m_web_content_client.did_request_reposition_window({ *x, *y });
  205. window_rect.set_location(position);
  206. } else {
  207. window_rect.set_location(m_page_host.page().window_position());
  208. }
  209. // 14. Return success with data set to the WindowRect object for the current top-level browsing context.
  210. return serialize_rect(window_rect);
  211. }
  212. // 11.8.3 Maximize Window, https://w3c.github.io/webdriver/#dfn-maximize-window
  213. Messages::WebDriverClient::MaximizeWindowResponse WebDriverConnection::maximize_window()
  214. {
  215. // 1. If the remote end does not support the Maximize Window command for the current top-level browsing context for any reason, return error with error code unsupported operation.
  216. // 2. If the current top-level browsing context is no longer open, return error with error code no such window.
  217. TRY(ensure_open_top_level_browsing_context());
  218. // FIXME: 3. Handle any user prompts and return its value if it is an error.
  219. // FIXME: 4. Fully exit fullscreen.
  220. // 5. Restore the window.
  221. restore_the_window();
  222. // 6. Maximize the window of the current top-level browsing context.
  223. auto window_rect = maximize_the_window();
  224. // 7. Return success with data set to the WindowRect object for the current top-level browsing context.
  225. return serialize_rect(window_rect);
  226. }
  227. // 11.8.4 Minimize Window, https://w3c.github.io/webdriver/#minimize-window
  228. Messages::WebDriverClient::MinimizeWindowResponse WebDriverConnection::minimize_window()
  229. {
  230. // 1. If the remote end does not support the Minimize Window command for the current top-level browsing context for any reason, return error with error code unsupported operation.
  231. // 2. If the current top-level browsing context is no longer open, return error with error code no such window.
  232. TRY(ensure_open_top_level_browsing_context());
  233. // FIXME: 3. Handle any user prompts and return its value if it is an error.
  234. // FIXME: 4. Fully exit fullscreen.
  235. // 5. Iconify the window.
  236. auto window_rect = iconify_the_window();
  237. // 6. Return success with data set to the WindowRect object for the current top-level browsing context.
  238. return serialize_rect(window_rect);
  239. }
  240. // 12.3.2 Find Element, https://w3c.github.io/webdriver/#dfn-find-element
  241. Messages::WebDriverClient::FindElementResponse WebDriverConnection::find_element(JsonValue const& payload)
  242. {
  243. // 1. Let location strategy be the result of getting a property called "using".
  244. auto location_strategy_string = TRY(get_property(payload, "using"sv));
  245. auto location_strategy = Web::WebDriver::location_strategy_from_string(location_strategy_string);
  246. // 2. If location strategy is not present as a keyword in the table of location strategies, return error with error code invalid argument.
  247. if (!location_strategy.has_value())
  248. return Web::WebDriver::Error::from_code(Web::WebDriver::ErrorCode::InvalidArgument, String::formatted("Location strategy '{}' is invalid", location_strategy_string));
  249. // 3. Let selector be the result of getting a property called "value".
  250. // 4. If selector is undefined, return error with error code invalid argument.
  251. auto selector = TRY(get_property(payload, "value"sv));
  252. // 5. If the current browsing context is no longer open, return error with error code no such window.
  253. TRY(ensure_open_top_level_browsing_context());
  254. // FIXME: 6. Handle any user prompts and return its value if it is an error.
  255. // 7. Let start node be the current browsing context’s document element.
  256. auto* start_node = m_page_host.page().top_level_browsing_context().active_document();
  257. // 8. If start node is null, return error with error code no such element.
  258. if (!start_node)
  259. return Web::WebDriver::Error::from_code(Web::WebDriver::ErrorCode::NoSuchElement, "document element does not exist"sv);
  260. // 9. Let result be the result of trying to Find with start node, location strategy, and selector.
  261. auto result = TRY(find(*start_node, *location_strategy, selector));
  262. // 10. If result is empty, return error with error code no such element. Otherwise, return the first element of result.
  263. if (result.is_empty())
  264. return Web::WebDriver::Error::from_code(Web::WebDriver::ErrorCode::NoSuchElement, "The requested element does not exist"sv);
  265. return make_success_response(result.at(0));
  266. }
  267. // https://w3c.github.io/webdriver/#dfn-no-longer-open
  268. ErrorOr<void, Web::WebDriver::Error> WebDriverConnection::ensure_open_top_level_browsing_context()
  269. {
  270. // A browsing context is said to be no longer open if it has been discarded.
  271. if (m_page_host.page().top_level_browsing_context().has_been_discarded())
  272. return Web::WebDriver::Error::from_code(Web::WebDriver::ErrorCode::NoSuchWindow, "Window not found"sv);
  273. return {};
  274. }
  275. // https://w3c.github.io/webdriver/#dfn-restore-the-window
  276. void WebDriverConnection::restore_the_window()
  277. {
  278. // To restore the window, given an operating system level window with an associated top-level browsing context, run implementation-specific steps to restore or unhide the window to the visible screen.
  279. m_web_content_client.async_did_request_restore_window();
  280. // Do not return from this operation until the visibility state of the top-level browsing context’s active document has reached the visible state, or until the operation times out.
  281. // FIXME: Implement timeouts.
  282. Web::Platform::EventLoopPlugin::the().spin_until([this]() {
  283. auto state = m_page_host.page().top_level_browsing_context().system_visibility_state();
  284. return state == Web::HTML::VisibilityState::Visible;
  285. });
  286. }
  287. // https://w3c.github.io/webdriver/#dfn-maximize-the-window
  288. Gfx::IntRect WebDriverConnection::maximize_the_window()
  289. {
  290. // To maximize the window, given an operating system level window with an associated top-level browsing context, run the implementation-specific steps to transition the operating system level window into the maximized window state.
  291. auto rect = m_web_content_client.did_request_maximize_window();
  292. // Return when the window has completed the transition, or within an implementation-defined timeout.
  293. return rect;
  294. }
  295. // https://w3c.github.io/webdriver/#dfn-iconify-the-window
  296. Gfx::IntRect WebDriverConnection::iconify_the_window()
  297. {
  298. // To iconify the window, given an operating system level window with an associated top-level browsing context, run implementation-specific steps to iconify, minimize, or hide the window from the visible screen.
  299. auto rect = m_web_content_client.did_request_minimize_window();
  300. // Do not return from this operation until the visibility state of the top-level browsing context’s active document has reached the hidden state, or until the operation times out.
  301. // FIXME: Implement timeouts.
  302. Web::Platform::EventLoopPlugin::the().spin_until([this]() {
  303. auto state = m_page_host.page().top_level_browsing_context().system_visibility_state();
  304. return state == Web::HTML::VisibilityState::Hidden;
  305. });
  306. return rect;
  307. }
  308. // https://w3c.github.io/webdriver/#dfn-find
  309. ErrorOr<JsonArray, Web::WebDriver::Error> WebDriverConnection::find(Web::DOM::ParentNode& start_node, Web::WebDriver::LocationStrategy using_, StringView value)
  310. {
  311. // FIXME: 1. Let end time be the current time plus the session implicit wait timeout.
  312. // 2. Let location strategy be equal to using.
  313. auto location_strategy = using_;
  314. // 3. Let selector be equal to value.
  315. auto selector = value;
  316. // 4. Let elements returned be the result of trying to call the relevant element location strategy with arguments start node, and selector.
  317. auto elements = Web::WebDriver::invoke_location_strategy(location_strategy, start_node, selector);
  318. // 5. If a DOMException, SyntaxError, XPathException, or other error occurs during the execution of the element location strategy, return error invalid selector.
  319. if (elements.is_error())
  320. return Web::WebDriver::Error::from_code(Web::WebDriver::ErrorCode::InvalidSelector, String::formatted("The location strategy could not finish: {}", elements.error().message));
  321. // FIXME: 6. If elements returned is empty and the current time is less than end time return to step 4. Otherwise, continue to the next step.
  322. // 7. Let result be an empty JSON List.
  323. JsonArray result;
  324. result.ensure_capacity(elements.value()->length());
  325. // 8. For each element in elements returned, append the web element reference object for element, to result.
  326. for (size_t i = 0; i < elements.value()->length(); ++i)
  327. result.append(web_element_reference_object(*elements.value()->item(i)));
  328. // 9. Return success with data result.
  329. return result;
  330. }
  331. }