WebDriverConnection.cpp 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872
  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 <LibJS/Runtime/Value.h>
  14. #include <LibWeb/CSS/PropertyID.h>
  15. #include <LibWeb/CSS/StyleProperties.h>
  16. #include <LibWeb/CSS/StyleValue.h>
  17. #include <LibWeb/DOM/Document.h>
  18. #include <LibWeb/DOM/Element.h>
  19. #include <LibWeb/Geometry/DOMRect.h>
  20. #include <LibWeb/HTML/AttributeNames.h>
  21. #include <LibWeb/HTML/BrowsingContext.h>
  22. #include <LibWeb/HTML/FormAssociatedElement.h>
  23. #include <LibWeb/HTML/HTMLInputElement.h>
  24. #include <LibWeb/HTML/HTMLOptionElement.h>
  25. #include <LibWeb/Page/Page.h>
  26. #include <LibWeb/Platform/EventLoopPlugin.h>
  27. #include <LibWeb/Platform/Timer.h>
  28. #include <LibWeb/WebDriver/Screenshot.h>
  29. #include <WebContent/ConnectionFromClient.h>
  30. #include <WebContent/PageHost.h>
  31. #include <WebContent/WebDriverConnection.h>
  32. namespace WebContent {
  33. static JsonValue make_success_response(JsonValue value)
  34. {
  35. JsonObject result;
  36. result.set("value", move(value));
  37. return result;
  38. }
  39. static JsonValue serialize_rect(Gfx::IntRect const& rect)
  40. {
  41. JsonObject serialized_rect = {};
  42. serialized_rect.set("x", rect.x());
  43. serialized_rect.set("y", rect.y());
  44. serialized_rect.set("width", rect.width());
  45. serialized_rect.set("height", rect.height());
  46. return make_success_response(move(serialized_rect));
  47. }
  48. static Gfx::IntRect compute_window_rect(Web::Page const& page)
  49. {
  50. return {
  51. page.window_position().x(),
  52. page.window_position().y(),
  53. page.window_size().width(),
  54. page.window_size().height()
  55. };
  56. }
  57. // https://w3c.github.io/webdriver/#dfn-calculate-the-absolute-position
  58. static Gfx::IntPoint calculate_absolute_position_of_element(Web::Page const& page, JS::NonnullGCPtr<Web::Geometry::DOMRect> rect)
  59. {
  60. // 1. Let rect be the value returned by calling getBoundingClientRect().
  61. // 2. Let window be the associated window of current top-level browsing context.
  62. auto const* window = page.top_level_browsing_context().active_window();
  63. // 3. Let x be (scrollX of window + rect’s x coordinate).
  64. auto x = (window ? static_cast<int>(window->scroll_x()) : 0) + static_cast<int>(rect->x());
  65. // 4. Let y be (scrollY of window + rect’s y coordinate).
  66. auto y = (window ? static_cast<int>(window->scroll_y()) : 0) + static_cast<int>(rect->y());
  67. // 5. Return a pair of (x, y).
  68. return { x, y };
  69. }
  70. static Gfx::IntRect calculate_absolute_rect_of_element(Web::Page const& page, Web::DOM::Element const& element)
  71. {
  72. auto bounding_rect = element.get_bounding_client_rect();
  73. auto coordinates = calculate_absolute_position_of_element(page, bounding_rect);
  74. return {
  75. coordinates.x(),
  76. coordinates.y(),
  77. static_cast<int>(bounding_rect->width()),
  78. static_cast<int>(bounding_rect->height())
  79. };
  80. }
  81. // https://w3c.github.io/webdriver/#dfn-get-or-create-a-web-element-reference
  82. static String get_or_create_a_web_element_reference(Web::DOM::Node const& element)
  83. {
  84. // FIXME: 1. For each known element of the current browsing context’s list of known elements:
  85. // FIXME: 1. If known element equals element, return success with known element’s web element reference.
  86. // FIXME: 2. Add element to the list of known elements of the current browsing context.
  87. // FIXME: 3. Return success with the element’s web element reference.
  88. return String::number(element.id());
  89. }
  90. // https://w3c.github.io/webdriver/#dfn-web-element-reference-object
  91. static JsonObject web_element_reference_object(Web::DOM::Node const& element)
  92. {
  93. // https://w3c.github.io/webdriver/#dfn-web-element-identifier
  94. static String const web_element_identifier = "element-6066-11e4-a52e-4f735466cecf"sv;
  95. // 1. Let identifier be the web element identifier.
  96. auto identifier = web_element_identifier;
  97. // 2. Let reference be the result of get or create a web element reference given element.
  98. auto reference = get_or_create_a_web_element_reference(element);
  99. // 3. Return a JSON Object initialized with a property with name identifier and value reference.
  100. JsonObject object;
  101. object.set("name"sv, identifier);
  102. object.set("value"sv, reference);
  103. return object;
  104. }
  105. // https://w3c.github.io/webdriver/#dfn-get-a-known-connected-element
  106. static ErrorOr<Web::DOM::Element*, Web::WebDriver::Error> get_known_connected_element(StringView element_id)
  107. {
  108. // NOTE: The whole concept of "connected elements" is not implemented yet. See get_or_create_a_web_element_reference().
  109. // For now the element is only represented by its ID.
  110. auto element = element_id.to_int();
  111. if (!element.has_value())
  112. return Web::WebDriver::Error::from_code(Web::WebDriver::ErrorCode::InvalidArgument, "Element ID is not an integer");
  113. auto* node = Web::DOM::Node::from_id(*element);
  114. if (!node || !node->is_element())
  115. return Web::WebDriver::Error::from_code(Web::WebDriver::ErrorCode::NoSuchElement, String::formatted("Could not find element with ID: {}", element_id));
  116. return static_cast<Web::DOM::Element*>(node);
  117. }
  118. // https://w3c.github.io/webdriver/#dfn-scrolls-into-view
  119. static void scroll_element_into_view(Web::DOM::Element& element)
  120. {
  121. // 1. Let options be the following ScrollIntoViewOptions:
  122. Web::DOM::ScrollIntoViewOptions options {};
  123. // Logical scroll position "block"
  124. // "end"
  125. options.block = Web::Bindings::ScrollLogicalPosition::End;
  126. // Logical scroll position "inline"
  127. // "nearest"
  128. options.inline_ = Web::Bindings::ScrollLogicalPosition::Nearest;
  129. // 2. Run Function.[[Call]](scrollIntoView, options) with element as the this value.
  130. element.scroll_into_view(options);
  131. }
  132. static ErrorOr<String, Web::WebDriver::Error> get_property(JsonValue const& payload, StringView key)
  133. {
  134. if (!payload.is_object())
  135. return Web::WebDriver::Error::from_code(Web::WebDriver::ErrorCode::InvalidArgument, "Payload is not a JSON object");
  136. auto const* property = payload.as_object().get_ptr(key);
  137. if (!property)
  138. return Web::WebDriver::Error::from_code(Web::WebDriver::ErrorCode::InvalidArgument, String::formatted("No property called '{}' present", key));
  139. if (!property->is_string())
  140. return Web::WebDriver::Error::from_code(Web::WebDriver::ErrorCode::InvalidArgument, String::formatted("Property '{}' is not a String", key));
  141. return property->as_string();
  142. }
  143. ErrorOr<NonnullRefPtr<WebDriverConnection>> WebDriverConnection::connect(ConnectionFromClient& web_content_client, PageHost& page_host, String const& webdriver_ipc_path)
  144. {
  145. dbgln_if(WEBDRIVER_DEBUG, "Trying to connect to {}", webdriver_ipc_path);
  146. auto socket = TRY(Core::Stream::LocalSocket::connect(webdriver_ipc_path));
  147. dbgln_if(WEBDRIVER_DEBUG, "Connected to WebDriver");
  148. return adopt_nonnull_ref_or_enomem(new (nothrow) WebDriverConnection(move(socket), web_content_client, page_host));
  149. }
  150. WebDriverConnection::WebDriverConnection(NonnullOwnPtr<Core::Stream::LocalSocket> socket, ConnectionFromClient& web_content_client, PageHost& page_host)
  151. : IPC::ConnectionToServer<WebDriverClientEndpoint, WebDriverServerEndpoint>(*this, move(socket))
  152. , m_web_content_client(web_content_client)
  153. , m_page_host(page_host)
  154. {
  155. }
  156. // https://w3c.github.io/webdriver/#dfn-close-the-session
  157. void WebDriverConnection::close_session()
  158. {
  159. // 1. Set the webdriver-active flag to false.
  160. set_is_webdriver_active(false);
  161. // 2. An endpoint node must close any top-level browsing contexts associated with the session, without prompting to unload.
  162. m_page_host.page().top_level_browsing_context().close();
  163. }
  164. void WebDriverConnection::set_is_webdriver_active(bool is_webdriver_active)
  165. {
  166. m_page_host.set_is_webdriver_active(is_webdriver_active);
  167. }
  168. // 10.1 Navigate To, https://w3c.github.io/webdriver/#navigate-to
  169. Messages::WebDriverClient::NavigateToResponse WebDriverConnection::navigate_to(JsonValue const& payload)
  170. {
  171. dbgln_if(WEBDRIVER_DEBUG, "WebDriverConnection::navigate_to {}", payload);
  172. // 1. If the current top-level browsing context is no longer open, return error with error code no such window.
  173. TRY(ensure_open_top_level_browsing_context());
  174. // 2. Let url be the result of getting the property url from the parameters argument.
  175. if (!payload.is_object() || !payload.as_object().has_string("url"sv))
  176. return Web::WebDriver::Error::from_code(Web::WebDriver::ErrorCode::InvalidArgument, "Payload doesn't have a string `url`"sv);
  177. URL url(payload.as_object().get_ptr("url"sv)->as_string());
  178. // 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.
  179. // FIXME: 4. Handle any user prompts and return its value if it is an error.
  180. // FIXME: 5. Let current URL be the current top-level browsing context’s active document’s URL.
  181. // FIXME: 6. If current URL and url do not have the same absolute URL:
  182. // 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.
  183. // 7. Navigate the current top-level browsing context to url.
  184. m_page_host.page().load(url);
  185. // FIXME: 8. If url is special except for file and current URL and URL do not have the same absolute URL:
  186. // FIXME: a. Try to wait for navigation to complete.
  187. // FIXME: b. Try to run the post-navigation checks.
  188. // FIXME: 9. Set the current browsing context with the current top-level browsing context.
  189. // 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.
  190. // 11. Return success with data null.
  191. return make_success_response({});
  192. }
  193. // 10.2 Get Current URL, https://w3c.github.io/webdriver/#get-current-url
  194. Messages::WebDriverClient::GetCurrentUrlResponse WebDriverConnection::get_current_url()
  195. {
  196. dbgln_if(WEBDRIVER_DEBUG, "WebDriverConnection::get_current_url");
  197. // 1. If the current top-level browsing context is no longer open, return error with error code no such window.
  198. TRY(ensure_open_top_level_browsing_context());
  199. // FIXME: 2. Handle any user prompts and return its value if it is an error.
  200. // 3. Let url be the serialization of the current top-level browsing context’s active document’s document URL.
  201. auto url = m_page_host.page().top_level_browsing_context().active_document()->url().to_string();
  202. // 4. Return success with data url.
  203. return make_success_response(url);
  204. }
  205. // 11.8.1 Get Window Rect, https://w3c.github.io/webdriver/#dfn-get-window-rect
  206. Messages::WebDriverClient::GetWindowRectResponse WebDriverConnection::get_window_rect()
  207. {
  208. // 1. If the current top-level browsing context is no longer open, return error with error code no such window.
  209. TRY(ensure_open_top_level_browsing_context());
  210. // FIXME: 2. Handle any user prompts and return its value if it is an error.
  211. // 3. Return success with data set to the WindowRect object for the current top-level browsing context.
  212. return serialize_rect(compute_window_rect(m_page_host.page()));
  213. }
  214. // 11.8.2 Set Window Rect, https://w3c.github.io/webdriver/#dfn-set-window-rect
  215. Messages::WebDriverClient::SetWindowRectResponse WebDriverConnection::set_window_rect(JsonValue const& payload)
  216. {
  217. if (!payload.is_object())
  218. return Web::WebDriver::Error::from_code(Web::WebDriver::ErrorCode::InvalidArgument, "Payload is not a JSON object");
  219. auto const& properties = payload.as_object();
  220. auto resolve_property = [](auto name, auto const* property, auto min, auto max) -> ErrorOr<Optional<i32>, Web::WebDriver::Error> {
  221. if (!property)
  222. return Optional<i32> {};
  223. if (!property->is_number())
  224. return Web::WebDriver::Error::from_code(Web::WebDriver::ErrorCode::InvalidArgument, String::formatted("Property '{}' is not a Number", name));
  225. auto number = property->template to_number<i64>();
  226. if (number < min)
  227. return Web::WebDriver::Error::from_code(Web::WebDriver::ErrorCode::InvalidArgument, String::formatted("Property '{}' value {} exceeds the minimum allowed value {}", name, number, min));
  228. if (number > max)
  229. return Web::WebDriver::Error::from_code(Web::WebDriver::ErrorCode::InvalidArgument, String::formatted("Property '{}' value {} exceeds the maximum allowed value {}", name, number, max));
  230. return static_cast<i32>(number);
  231. };
  232. // 1. Let width be the result of getting a property named width from the parameters argument, else let it be null.
  233. auto const* width_property = properties.get_ptr("width"sv);
  234. // 2. Let height be the result of getting a property named height from the parameters argument, else let it be null.
  235. auto const* height_property = properties.get_ptr("height"sv);
  236. // 3. Let x be the result of getting a property named x from the parameters argument, else let it be null.
  237. auto const* x_property = properties.get_ptr("x"sv);
  238. // 4. Let y be the result of getting a property named y from the parameters argument, else let it be null.
  239. auto const* y_property = properties.get_ptr("y"sv);
  240. // 5. If width or height is neither null nor a Number from 0 to 2^31 − 1, return error with error code invalid argument.
  241. auto width = TRY(resolve_property("width"sv, width_property, 0, NumericLimits<i32>::max()));
  242. auto height = TRY(resolve_property("height"sv, height_property, 0, NumericLimits<i32>::max()));
  243. // 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.
  244. auto x = TRY(resolve_property("x"sv, x_property, NumericLimits<i32>::min(), NumericLimits<i32>::max()));
  245. auto y = TRY(resolve_property("y"sv, y_property, NumericLimits<i32>::min(), NumericLimits<i32>::max()));
  246. // 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.
  247. // 8. If the current top-level browsing context is no longer open, return error with error code no such window.
  248. TRY(ensure_open_top_level_browsing_context());
  249. // FIXME: 9. Handle any user prompts and return its value if it is an error.
  250. // FIXME: 10. Fully exit fullscreen.
  251. // 11. Restore the window.
  252. restore_the_window();
  253. Gfx::IntRect window_rect;
  254. // 11. If width and height are not null:
  255. if (width.has_value() && height.has_value()) {
  256. // 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.
  257. // 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.
  258. auto size = m_web_content_client.did_request_resize_window({ *width, *height });
  259. window_rect.set_size(size);
  260. } else {
  261. window_rect.set_size(m_page_host.page().window_size());
  262. }
  263. // 12. If x and y are not null:
  264. if (x.has_value() && y.has_value()) {
  265. // 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.
  266. auto position = m_web_content_client.did_request_reposition_window({ *x, *y });
  267. window_rect.set_location(position);
  268. } else {
  269. window_rect.set_location(m_page_host.page().window_position());
  270. }
  271. // 14. Return success with data set to the WindowRect object for the current top-level browsing context.
  272. return serialize_rect(window_rect);
  273. }
  274. // 11.8.3 Maximize Window, https://w3c.github.io/webdriver/#dfn-maximize-window
  275. Messages::WebDriverClient::MaximizeWindowResponse WebDriverConnection::maximize_window()
  276. {
  277. // 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.
  278. // 2. If the current top-level browsing context is no longer open, return error with error code no such window.
  279. TRY(ensure_open_top_level_browsing_context());
  280. // FIXME: 3. Handle any user prompts and return its value if it is an error.
  281. // FIXME: 4. Fully exit fullscreen.
  282. // 5. Restore the window.
  283. restore_the_window();
  284. // 6. Maximize the window of the current top-level browsing context.
  285. auto window_rect = maximize_the_window();
  286. // 7. Return success with data set to the WindowRect object for the current top-level browsing context.
  287. return serialize_rect(window_rect);
  288. }
  289. // 11.8.4 Minimize Window, https://w3c.github.io/webdriver/#minimize-window
  290. Messages::WebDriverClient::MinimizeWindowResponse WebDriverConnection::minimize_window()
  291. {
  292. // 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.
  293. // 2. If the current top-level browsing context is no longer open, return error with error code no such window.
  294. TRY(ensure_open_top_level_browsing_context());
  295. // FIXME: 3. Handle any user prompts and return its value if it is an error.
  296. // FIXME: 4. Fully exit fullscreen.
  297. // 5. Iconify the window.
  298. auto window_rect = iconify_the_window();
  299. // 6. Return success with data set to the WindowRect object for the current top-level browsing context.
  300. return serialize_rect(window_rect);
  301. }
  302. // 12.3.2 Find Element, https://w3c.github.io/webdriver/#dfn-find-element
  303. Messages::WebDriverClient::FindElementResponse WebDriverConnection::find_element(JsonValue const& payload)
  304. {
  305. // 1. Let location strategy be the result of getting a property called "using".
  306. auto location_strategy_string = TRY(get_property(payload, "using"sv));
  307. auto location_strategy = Web::WebDriver::location_strategy_from_string(location_strategy_string);
  308. // 2. If location strategy is not present as a keyword in the table of location strategies, return error with error code invalid argument.
  309. if (!location_strategy.has_value())
  310. return Web::WebDriver::Error::from_code(Web::WebDriver::ErrorCode::InvalidArgument, String::formatted("Location strategy '{}' is invalid", location_strategy_string));
  311. // 3. Let selector be the result of getting a property called "value".
  312. // 4. If selector is undefined, return error with error code invalid argument.
  313. auto selector = TRY(get_property(payload, "value"sv));
  314. // 5. If the current browsing context is no longer open, return error with error code no such window.
  315. TRY(ensure_open_top_level_browsing_context());
  316. // FIXME: 6. Handle any user prompts and return its value if it is an error.
  317. // 7. Let start node be the current browsing context’s document element.
  318. auto* start_node = m_page_host.page().top_level_browsing_context().active_document();
  319. // 8. If start node is null, return error with error code no such element.
  320. if (!start_node)
  321. return Web::WebDriver::Error::from_code(Web::WebDriver::ErrorCode::NoSuchElement, "document element does not exist"sv);
  322. // 9. Let result be the result of trying to Find with start node, location strategy, and selector.
  323. auto result = TRY(find(*start_node, *location_strategy, selector));
  324. // 10. If result is empty, return error with error code no such element. Otherwise, return the first element of result.
  325. if (result.is_empty())
  326. return Web::WebDriver::Error::from_code(Web::WebDriver::ErrorCode::NoSuchElement, "The requested element does not exist"sv);
  327. return make_success_response(result.at(0));
  328. }
  329. // 12.3.3 Find Elements, https://w3c.github.io/webdriver/#dfn-find-elements
  330. Messages::WebDriverClient::FindElementsResponse WebDriverConnection::find_elements(JsonValue const& payload)
  331. {
  332. // 1. Let location strategy be the result of getting a property called "using".
  333. auto location_strategy_string = TRY(get_property(payload, "using"sv));
  334. auto location_strategy = Web::WebDriver::location_strategy_from_string(location_strategy_string);
  335. // 2. If location strategy is not present as a keyword in the table of location strategies, return error with error code invalid argument.
  336. if (!location_strategy.has_value())
  337. return Web::WebDriver::Error::from_code(Web::WebDriver::ErrorCode::InvalidArgument, String::formatted("Location strategy '{}' is invalid", location_strategy_string));
  338. // 3. Let selector be the result of getting a property called "value".
  339. // 4. If selector is undefined, return error with error code invalid argument.
  340. auto selector = TRY(get_property(payload, "value"sv));
  341. // 5. If the current browsing context is no longer open, return error with error code no such window.
  342. TRY(ensure_open_top_level_browsing_context());
  343. // FIXME: 6. Handle any user prompts and return its value if it is an error.
  344. // 7. Let start node be the current browsing context’s document element.
  345. auto* start_node = m_page_host.page().top_level_browsing_context().active_document();
  346. // 8. If start node is null, return error with error code no such element.
  347. if (!start_node)
  348. return Web::WebDriver::Error::from_code(Web::WebDriver::ErrorCode::NoSuchElement, "document element does not exist"sv);
  349. // 9. Return the result of trying to Find with start node, location strategy, and selector.
  350. auto result = TRY(find(*start_node, *location_strategy, selector));
  351. return make_success_response(move(result));
  352. }
  353. // 12.3.4 Find Element From Element, https://w3c.github.io/webdriver/#dfn-find-element-from-element
  354. Messages::WebDriverClient::FindElementFromElementResponse WebDriverConnection::find_element_from_element(JsonValue const& payload, String const& element_id)
  355. {
  356. // 1. Let location strategy be the result of getting a property called "using".
  357. auto location_strategy_string = TRY(get_property(payload, "using"sv));
  358. auto location_strategy = Web::WebDriver::location_strategy_from_string(location_strategy_string);
  359. // 2. If location strategy is not present as a keyword in the table of location strategies, return error with error code invalid argument.
  360. if (!location_strategy.has_value())
  361. return Web::WebDriver::Error::from_code(Web::WebDriver::ErrorCode::InvalidArgument, String::formatted("Location strategy '{}' is invalid", location_strategy_string));
  362. // 3. Let selector be the result of getting a property called "value".
  363. // 4. If selector is undefined, return error with error code invalid argument.
  364. auto selector = TRY(get_property(payload, "value"sv));
  365. // 5. If the current browsing context is no longer open, return error with error code no such window.
  366. TRY(ensure_open_top_level_browsing_context());
  367. // FIXME: 6. Handle any user prompts and return its value if it is an error.
  368. // 7. Let start node be the result of trying to get a known connected element with url variable element id.
  369. auto* start_node = TRY(get_known_connected_element(element_id));
  370. // 8. Let result be the value of trying to Find with start node, location strategy, and selector.
  371. auto result = TRY(find(*start_node, *location_strategy, selector));
  372. // 9. If result is empty, return error with error code no such element. Otherwise, return the first element of result.
  373. if (result.is_empty())
  374. return Web::WebDriver::Error::from_code(Web::WebDriver::ErrorCode::NoSuchElement, "The requested element does not exist"sv);
  375. return make_success_response(result.at(0));
  376. }
  377. // 12.3.5 Find Elements From Element, https://w3c.github.io/webdriver/#dfn-find-elements-from-element
  378. Messages::WebDriverClient::FindElementsFromElementResponse WebDriverConnection::find_elements_from_element(JsonValue const& payload, String const& element_id)
  379. {
  380. // 1. Let location strategy be the result of getting a property called "using".
  381. auto location_strategy_string = TRY(get_property(payload, "using"sv));
  382. auto location_strategy = Web::WebDriver::location_strategy_from_string(location_strategy_string);
  383. // 2. If location strategy is not present as a keyword in the table of location strategies, return error with error code invalid argument.
  384. if (!location_strategy.has_value())
  385. return Web::WebDriver::Error::from_code(Web::WebDriver::ErrorCode::InvalidArgument, String::formatted("Location strategy '{}' is invalid", location_strategy_string));
  386. // 3. Let selector be the result of getting a property called "value".
  387. // 4. If selector is undefined, return error with error code invalid argument.
  388. auto selector = TRY(get_property(payload, "value"sv));
  389. // 5. If the current browsing context is no longer open, return error with error code no such window.
  390. TRY(ensure_open_top_level_browsing_context());
  391. // FIXME: 6. Handle any user prompts and return its value if it is an error.
  392. // 7. Let start node be the result of trying to get a known connected element with url variable element id.
  393. auto* start_node = TRY(get_known_connected_element(element_id));
  394. // 8. Return the result of trying to Find with start node, location strategy, and selector.
  395. auto result = TRY(find(*start_node, *location_strategy, selector));
  396. return make_success_response(move(result));
  397. }
  398. // 12.4.1 Is Element Selected, https://w3c.github.io/webdriver/#dfn-is-element-selected
  399. Messages::WebDriverClient::IsElementSelectedResponse WebDriverConnection::is_element_selected(String const& element_id)
  400. {
  401. // 1. If the current browsing context is no longer open, return error with error code no such window.
  402. TRY(ensure_open_top_level_browsing_context());
  403. // FIXME: 2. Handle any user prompts and return its value if it is an error.
  404. // 3. Let element be the result of trying to get a known connected element with url variable element id.
  405. auto* element = TRY(get_known_connected_element(element_id));
  406. // 4. Let selected be the value corresponding to the first matching statement:
  407. bool selected = false;
  408. // element is an input element with a type attribute in the Checkbox- or Radio Button state
  409. if (is<Web::HTML::HTMLInputElement>(*element)) {
  410. // -> The result of element’s checkedness.
  411. auto& input = static_cast<Web::HTML::HTMLInputElement&>(*element);
  412. using enum Web::HTML::HTMLInputElement::TypeAttributeState;
  413. if (input.type_state() == Checkbox || input.type_state() == RadioButton)
  414. selected = input.checked();
  415. }
  416. // element is an option element
  417. else if (is<Web::HTML::HTMLOptionElement>(*element)) {
  418. // -> The result of element’s selectedness.
  419. selected = static_cast<Web::HTML::HTMLOptionElement&>(*element).selected();
  420. }
  421. // Otherwise
  422. // -> False.
  423. // 5. Return success with data selected.
  424. return make_success_response(selected);
  425. }
  426. // 12.4.2 Get Element Attribute, https://w3c.github.io/webdriver/#dfn-get-element-attribute
  427. Messages::WebDriverClient::GetElementAttributeResponse WebDriverConnection::get_element_attribute(String const& element_id, String const& name)
  428. {
  429. // 1. If the current browsing context is no longer open, return error with error code no such window.
  430. TRY(ensure_open_top_level_browsing_context());
  431. // FIXME: 2. Handle any user prompts and return its value if it is an error.
  432. // 3. Let element be the result of trying to get a known connected element with url variable element id.
  433. auto* element = TRY(get_known_connected_element(element_id));
  434. // 4. Let result be the result of the first matching condition:
  435. Optional<String> result;
  436. // -> If name is a boolean attribute
  437. if (Web::HTML::is_boolean_attribute(name)) {
  438. // "true" (string) if the element has the attribute, otherwise null.
  439. if (element->has_attribute(name))
  440. result = "true"sv;
  441. }
  442. // -> Otherwise
  443. else {
  444. // The result of getting an attribute by name name.
  445. result = element->get_attribute(name);
  446. }
  447. // 5. Return success with data result.
  448. if (result.has_value())
  449. return make_success_response(result.release_value());
  450. return make_success_response({});
  451. }
  452. // 12.4.3 Get Element Property, https://w3c.github.io/webdriver/#dfn-get-element-property
  453. Messages::WebDriverClient::GetElementPropertyResponse WebDriverConnection::get_element_property(String const& element_id, String const& name)
  454. {
  455. // 1. If the current browsing context is no longer open, return error with error code no such window.
  456. TRY(ensure_open_top_level_browsing_context());
  457. // FIXME: 2. Handle any user prompts and return its value if it is an error.
  458. // 3. Let element be the result of trying to get a known connected element with url variable element id.
  459. auto* element = TRY(get_known_connected_element(element_id));
  460. Optional<String> result;
  461. // 4. Let property be the result of calling the Object.[[GetProperty]](name) on element.
  462. if (auto property_or_error = element->get(name); !property_or_error.is_throw_completion()) {
  463. auto property = property_or_error.release_value();
  464. // 5. Let result be the value of property if not undefined, or null.
  465. if (!property.is_undefined()) {
  466. if (auto string_or_error = property.to_string(element->vm()); !string_or_error.is_error())
  467. result = string_or_error.release_value();
  468. }
  469. }
  470. // 6. Return success with data result.
  471. if (result.has_value())
  472. return make_success_response(result.release_value());
  473. return make_success_response({});
  474. }
  475. // 12.4.4 Get Element CSS Value, https://w3c.github.io/webdriver/#dfn-get-element-css-value
  476. Messages::WebDriverClient::GetElementCssValueResponse WebDriverConnection::get_element_css_value(String const& element_id, String const& name)
  477. {
  478. // 1. If the current browsing context is no longer open, return error with error code no such window.
  479. TRY(ensure_open_top_level_browsing_context());
  480. // FIXME: 2. Handle any user prompts and return its value if it is an error.
  481. // 3. Let element be the result of trying to get a known connected element with url variable element id.
  482. auto* element = TRY(get_known_connected_element(element_id));
  483. // 4. Let computed value be the result of the first matching condition:
  484. String computed_value;
  485. // -> current browsing context’s active document’s type is not "xml"
  486. if (!m_page_host.page().top_level_browsing_context().active_document()->is_xml_document()) {
  487. // computed value of parameter property name from element’s style declarations. property name is obtained from url variables.
  488. auto property = Web::CSS::property_id_from_string(name);
  489. if (auto* computed_values = element->computed_css_values())
  490. computed_value = computed_values->property(property)->to_string();
  491. }
  492. // -> Otherwise
  493. else {
  494. // "" (empty string)
  495. computed_value = String::empty();
  496. }
  497. // 5. Return success with data computed value.
  498. return make_success_response(move(computed_value));
  499. }
  500. // 12.4.5 Get Element Text, https://w3c.github.io/webdriver/#dfn-get-element-text
  501. Messages::WebDriverClient::GetElementTextResponse WebDriverConnection::get_element_text(String const& element_id)
  502. {
  503. // 1. If the current browsing context is no longer open, return error with error code no such window.
  504. TRY(ensure_open_top_level_browsing_context());
  505. // FIXME: 2. Handle any user prompts and return its value if it is an error.
  506. // 3. Let element be the result of trying to get a known connected element with url variable element id.
  507. auto* element = TRY(get_known_connected_element(element_id));
  508. // 4. Let rendered text be the result of performing implementation-specific steps whose result is exactly the same as the result of a Function.[[Call]](null, element) with bot.dom.getVisibleText as the this value.
  509. auto rendered_text = element->text_content();
  510. // 5. Return success with data rendered text.
  511. return make_success_response(move(rendered_text));
  512. }
  513. // 12.4.6 Get Element Tag Name, https://w3c.github.io/webdriver/#dfn-get-element-tag-name
  514. Messages::WebDriverClient::GetElementTagNameResponse WebDriverConnection::get_element_tag_name(String const& element_id)
  515. {
  516. // 1. If the current browsing context is no longer open, return error with error code no such window.
  517. TRY(ensure_open_top_level_browsing_context());
  518. // FIXME: 2. Handle any user prompts and return its value if it is an error.
  519. // 3. Let element be the result of trying to get a known connected element with url variable element id.
  520. auto* element = TRY(get_known_connected_element(element_id));
  521. // 4. Let qualified name be the result of getting element’s tagName IDL attribute.
  522. auto qualified_name = element->tag_name();
  523. // 5. Return success with data qualified name.
  524. return make_success_response(move(qualified_name));
  525. }
  526. // 12.4.7 Get Element Rect, https://w3c.github.io/webdriver/#dfn-get-element-rect
  527. Messages::WebDriverClient::GetElementRectResponse WebDriverConnection::get_element_rect(String const& element_id)
  528. {
  529. // 1. If the current browsing context is no longer open, return error with error code no such window.
  530. TRY(ensure_open_top_level_browsing_context());
  531. // FIXME: 2. Handle any user prompts and return its value if it is an error.
  532. // 3. Let element be the result of trying to get a known connected element with url variable element id.
  533. auto* element = TRY(get_known_connected_element(element_id));
  534. // 4. Calculate the absolute position of element and let it be coordinates.
  535. // 5. Let rect be element’s bounding rectangle.
  536. auto rect = calculate_absolute_rect_of_element(m_page_host.page(), *element);
  537. // 6. Let body be a new JSON Object initialized with:
  538. // "x"
  539. // The first value of coordinates.
  540. // "y"
  541. // The second value of coordinates.
  542. // "width"
  543. // Value of rect’s width dimension.
  544. // "height"
  545. // Value of rect’s height dimension.
  546. auto body = serialize_rect(rect);
  547. // 7. Return success with data body.
  548. return body;
  549. }
  550. // 12.4.8 Is Element Enabled, https://w3c.github.io/webdriver/#dfn-is-element-enabled
  551. Messages::WebDriverClient::IsElementEnabledResponse WebDriverConnection::is_element_enabled(String const& element_id)
  552. {
  553. // 1. If the current browsing context is no longer open, return error with error code no such window.
  554. TRY(ensure_open_top_level_browsing_context());
  555. // FIXME: 2. Handle any user prompts and return its value if it is an error.
  556. // 3. Let element be the result of trying to get a known connected element with url variable element id.
  557. auto* element = TRY(get_known_connected_element(element_id));
  558. // 4. Let enabled be a boolean initially set to true if the current browsing context’s active document’s type is not "xml".
  559. // 5. Otherwise, let enabled to false and jump to the last step of this algorithm.
  560. bool enabled = !m_page_host.page().top_level_browsing_context().active_document()->is_xml_document();
  561. // 6. Set enabled to false if a form control is disabled.
  562. if (enabled && is<Web::HTML::FormAssociatedElement>(*element)) {
  563. auto& form_associated_element = dynamic_cast<Web::HTML::FormAssociatedElement&>(*element);
  564. enabled = form_associated_element.enabled();
  565. }
  566. // 7. Return success with data enabled.
  567. return make_success_response(enabled);
  568. }
  569. // 17.1 Take Screenshot, https://w3c.github.io/webdriver/#take-screenshot
  570. Messages::WebDriverClient::TakeScreenshotResponse WebDriverConnection::take_screenshot()
  571. {
  572. // 1. If the current top-level browsing context is no longer open, return error with error code no such window.
  573. TRY(ensure_open_top_level_browsing_context());
  574. // 2. When the user agent is next to run the animation frame callbacks:
  575. // a. Let root rect be the current top-level browsing context’s document element’s rectangle.
  576. // b. Let screenshot result be the result of trying to call draw a bounding box from the framebuffer, given root rect as an argument.
  577. // c. Let canvas be a canvas element of screenshot result’s data.
  578. // d. Let encoding result be the result of trying encoding a canvas as Base64 canvas.
  579. // e. Let encoded string be encoding result’s data.
  580. auto* document = m_page_host.page().top_level_browsing_context().active_document();
  581. auto root_rect = calculate_absolute_rect_of_element(m_page_host.page(), *document->document_element());
  582. auto encoded_string = TRY(Web::WebDriver::capture_element_screenshot(
  583. [&](auto const& rect, auto& bitmap) { m_page_host.paint(rect, bitmap); },
  584. m_page_host.page(),
  585. *document->document_element(),
  586. root_rect));
  587. // 3. Return success with data encoded string.
  588. return make_success_response(move(encoded_string));
  589. }
  590. // 17.2 Take Element Screenshot, https://w3c.github.io/webdriver/#dfn-take-element-screenshot
  591. Messages::WebDriverClient::TakeElementScreenshotResponse WebDriverConnection::take_element_screenshot(String const& element_id)
  592. {
  593. // 1. If the current top-level browsing context is no longer open, return error with error code no such window.
  594. TRY(ensure_open_top_level_browsing_context());
  595. // FIXME: 2. Handle any user prompts and return its value if it is an error.
  596. // 3. Let element be the result of trying to get a known connected element with url variable element id.
  597. auto* element = TRY(get_known_connected_element(element_id));
  598. // 4. Scroll into view the element.
  599. scroll_element_into_view(*element);
  600. // 5. When the user agent is next to run the animation frame callbacks:
  601. // a. Let element rect be element’s rectangle.
  602. // b. Let screenshot result be the result of trying to call draw a bounding box from the framebuffer, given element rect as an argument.
  603. // c. Let canvas be a canvas element of screenshot result’s data.
  604. // d. Let encoding result be the result of trying encoding a canvas as Base64 canvas.
  605. // e. Let encoded string be encoding result’s data.
  606. auto element_rect = calculate_absolute_rect_of_element(m_page_host.page(), *element);
  607. auto encoded_string = TRY(Web::WebDriver::capture_element_screenshot(
  608. [&](auto const& rect, auto& bitmap) { m_page_host.paint(rect, bitmap); },
  609. m_page_host.page(),
  610. *element,
  611. element_rect));
  612. // 6. Return success with data encoded string.
  613. return make_success_response(move(encoded_string));
  614. }
  615. // https://w3c.github.io/webdriver/#dfn-no-longer-open
  616. ErrorOr<void, Web::WebDriver::Error> WebDriverConnection::ensure_open_top_level_browsing_context()
  617. {
  618. // A browsing context is said to be no longer open if it has been discarded.
  619. if (m_page_host.page().top_level_browsing_context().has_been_discarded())
  620. return Web::WebDriver::Error::from_code(Web::WebDriver::ErrorCode::NoSuchWindow, "Window not found"sv);
  621. return {};
  622. }
  623. // https://w3c.github.io/webdriver/#dfn-restore-the-window
  624. void WebDriverConnection::restore_the_window()
  625. {
  626. // 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.
  627. m_web_content_client.async_did_request_restore_window();
  628. // 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.
  629. // FIXME: Implement timeouts.
  630. Web::Platform::EventLoopPlugin::the().spin_until([this]() {
  631. auto state = m_page_host.page().top_level_browsing_context().system_visibility_state();
  632. return state == Web::HTML::VisibilityState::Visible;
  633. });
  634. }
  635. // https://w3c.github.io/webdriver/#dfn-maximize-the-window
  636. Gfx::IntRect WebDriverConnection::maximize_the_window()
  637. {
  638. // 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.
  639. auto rect = m_web_content_client.did_request_maximize_window();
  640. // Return when the window has completed the transition, or within an implementation-defined timeout.
  641. return rect;
  642. }
  643. // https://w3c.github.io/webdriver/#dfn-iconify-the-window
  644. Gfx::IntRect WebDriverConnection::iconify_the_window()
  645. {
  646. // 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.
  647. auto rect = m_web_content_client.did_request_minimize_window();
  648. // 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.
  649. // FIXME: Implement timeouts.
  650. Web::Platform::EventLoopPlugin::the().spin_until([this]() {
  651. auto state = m_page_host.page().top_level_browsing_context().system_visibility_state();
  652. return state == Web::HTML::VisibilityState::Hidden;
  653. });
  654. return rect;
  655. }
  656. // https://w3c.github.io/webdriver/#dfn-find
  657. ErrorOr<JsonArray, Web::WebDriver::Error> WebDriverConnection::find(Web::DOM::ParentNode& start_node, Web::WebDriver::LocationStrategy using_, StringView value)
  658. {
  659. // FIXME: 1. Let end time be the current time plus the session implicit wait timeout.
  660. // 2. Let location strategy be equal to using.
  661. auto location_strategy = using_;
  662. // 3. Let selector be equal to value.
  663. auto selector = value;
  664. // 4. Let elements returned be the result of trying to call the relevant element location strategy with arguments start node, and selector.
  665. auto elements = Web::WebDriver::invoke_location_strategy(location_strategy, start_node, selector);
  666. // 5. If a DOMException, SyntaxError, XPathException, or other error occurs during the execution of the element location strategy, return error invalid selector.
  667. if (elements.is_error())
  668. return Web::WebDriver::Error::from_code(Web::WebDriver::ErrorCode::InvalidSelector, String::formatted("The location strategy could not finish: {}", elements.error().message));
  669. // 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.
  670. // 7. Let result be an empty JSON List.
  671. JsonArray result;
  672. result.ensure_capacity(elements.value()->length());
  673. // 8. For each element in elements returned, append the web element reference object for element, to result.
  674. for (size_t i = 0; i < elements.value()->length(); ++i)
  675. result.append(web_element_reference_object(*elements.value()->item(i)));
  676. // 9. Return success with data result.
  677. return result;
  678. }
  679. }