1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 03:37:34 +00:00

Browser+WebContent+WebDriver: Move Is Element Selected to WebContent

This commit is contained in:
Timothy Flynn 2022-11-10 08:15:39 -05:00 committed by Linus Groh
parent 560da56a1d
commit 04ea3992e9
16 changed files with 40 additions and 76 deletions

View file

@ -13,6 +13,8 @@
#include <AK/Vector.h>
#include <LibWeb/DOM/Document.h>
#include <LibWeb/HTML/BrowsingContext.h>
#include <LibWeb/HTML/HTMLInputElement.h>
#include <LibWeb/HTML/HTMLOptionElement.h>
#include <LibWeb/Page/Page.h>
#include <LibWeb/Platform/EventLoopPlugin.h>
#include <LibWeb/Platform/Timer.h>
@ -455,6 +457,41 @@ Messages::WebDriverClient::FindElementsFromElementResponse WebDriverConnection::
return make_success_response(move(result));
}
// 12.4.1 Is Element Selected, https://w3c.github.io/webdriver/#dfn-is-element-selected
Messages::WebDriverClient::IsElementSelectedResponse WebDriverConnection::is_element_selected(String const& element_id)
{
// 1. If the current browsing context is no longer open, return error with error code no such window.
TRY(ensure_open_top_level_browsing_context());
// FIXME: 2. Handle any user prompts and return its value if it is an error.
// 3. Let element be the result of trying to get a known connected element with url variable element id.
auto* element = TRY(get_known_connected_element(element_id));
// 4. Let selected be the value corresponding to the first matching statement:
bool selected = false;
// element is an input element with a type attribute in the Checkbox- or Radio Button state
if (is<Web::HTML::HTMLInputElement>(*element)) {
// -> The result of elements checkedness.
auto& input = static_cast<Web::HTML::HTMLInputElement&>(*element);
using enum Web::HTML::HTMLInputElement::TypeAttributeState;
if (input.type_state() == Checkbox || input.type_state() == RadioButton)
selected = input.checked();
}
// element is an option element
else if (is<Web::HTML::HTMLOptionElement>(*element)) {
// -> The result of elements selectedness.
selected = static_cast<Web::HTML::HTMLOptionElement&>(*element).selected();
}
// Otherwise
// -> False.
// 5. Return success with data selected.
return make_success_response(selected);
}
// https://w3c.github.io/webdriver/#dfn-no-longer-open
ErrorOr<void, Web::WebDriver::Error> WebDriverConnection::ensure_open_top_level_browsing_context()
{