ElementLocationStrategies.cpp 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /*
  2. * Copyright (c) 2022, Tobias Christiansen <tobyase@serenityos.org>
  3. * Copyright (c) 2022, Tim Flynn <trflynn89@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <LibWeb/DOM/ParentNode.h>
  8. #include <LibWeb/WebDriver/ElementLocationStrategies.h>
  9. namespace Web::WebDriver {
  10. // https://w3c.github.io/webdriver/#css-selectors
  11. static ErrorOr<JS::NonnullGCPtr<DOM::NodeList>, Error> locate_element_by_css_selector(DOM::ParentNode& start_node, StringView selector)
  12. {
  13. // 1. Let elements be the result of calling querySelectorAll() with start node as this and selector as the argument.
  14. // If this causes an exception to be thrown, return error with error code invalid selector.
  15. auto elements = start_node.query_selector_all(selector);
  16. if (elements.is_exception())
  17. return Error::from_code(ErrorCode::InvalidSelector, "querySelectorAll() failed"sv);
  18. // 2.Return success with data elements.
  19. return elements.release_value();
  20. }
  21. // https://w3c.github.io/webdriver/#link-text
  22. static ErrorOr<JS::NonnullGCPtr<DOM::NodeList>, Error> locate_element_by_link_text(DOM::ParentNode&, StringView)
  23. {
  24. return Error::from_code(ErrorCode::UnsupportedOperation, "Not implemented: locate element by link text"sv);
  25. }
  26. // https://w3c.github.io/webdriver/#partial-link-text
  27. static ErrorOr<JS::NonnullGCPtr<DOM::NodeList>, Error> locate_element_by_partial_link_text(DOM::ParentNode&, StringView)
  28. {
  29. return Error::from_code(ErrorCode::UnsupportedOperation, "Not implemented: locate element by partial link text"sv);
  30. }
  31. // https://w3c.github.io/webdriver/#tag-name
  32. static ErrorOr<JS::NonnullGCPtr<DOM::NodeList>, Error> locate_element_by_tag_name(DOM::ParentNode&, StringView)
  33. {
  34. return Error::from_code(ErrorCode::UnsupportedOperation, "Not implemented: locate element by tag name"sv);
  35. }
  36. // https://w3c.github.io/webdriver/#xpath
  37. static ErrorOr<JS::NonnullGCPtr<DOM::NodeList>, Error> locate_element_by_x_path(DOM::ParentNode&, StringView)
  38. {
  39. return Error::from_code(ErrorCode::UnsupportedOperation, "Not implemented: locate element by XPath"sv);
  40. }
  41. Optional<LocationStrategy> location_strategy_from_string(StringView type)
  42. {
  43. if (type == "css selector"sv)
  44. return LocationStrategy::CssSelector;
  45. if (type == "link text"sv)
  46. return LocationStrategy::LinkText;
  47. if (type == "partial link text"sv)
  48. return LocationStrategy::PartialLinkText;
  49. if (type == "tag name"sv)
  50. return LocationStrategy::TagName;
  51. if (type == "xpath"sv)
  52. return LocationStrategy::XPath;
  53. return {};
  54. }
  55. ErrorOr<JS::NonnullGCPtr<DOM::NodeList>, Error> invoke_location_strategy(LocationStrategy type, DOM::ParentNode& start_node, StringView selector)
  56. {
  57. switch (type) {
  58. case LocationStrategy::CssSelector:
  59. return locate_element_by_css_selector(start_node, selector);
  60. case LocationStrategy::LinkText:
  61. return locate_element_by_link_text(start_node, selector);
  62. case LocationStrategy::PartialLinkText:
  63. return locate_element_by_partial_link_text(start_node, selector);
  64. case LocationStrategy::TagName:
  65. return locate_element_by_tag_name(start_node, selector);
  66. case LocationStrategy::XPath:
  67. return locate_element_by_x_path(start_node, selector);
  68. }
  69. VERIFY_NOT_REACHED();
  70. }
  71. }