-
-
Notifications
You must be signed in to change notification settings - Fork 212
Shadow dom #728
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Shadow dom #728
Changes from 9 commits
77760e2
708a79f
bb932b4
ca0d431
80f5959
d356e2f
f749e16
2bbd851
2804fe9
5aebe19
158f6aa
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| defmodule Wallaby.Integration.Browser.ShadowDomTest do | ||
| use Wallaby.Integration.SessionCase, async: true | ||
|
|
||
| setup %{session: session} do | ||
| page = | ||
| session | ||
| |> visit("shadow_dom.html") | ||
|
|
||
| {:ok, page: page} | ||
| end | ||
|
|
||
| test "can find a shadow root", %{session: session} do | ||
| shadow_root = | ||
| session | ||
| |> find(Query.css("shadow-test")) | ||
| |> shadow_root() | ||
|
|
||
| assert shadow_root | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will this be an Element struct? if yes let's assert on that |
||
| end | ||
|
|
||
| test "can find stuff within da shadow dom", %{session: session} do | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. let's make these test names slightly more formal |
||
| element = | ||
| session | ||
| |> find(Query.css("shadow-test")) | ||
| |> shadow_root() | ||
| |> find(Query.css("#in-shadow")) | ||
|
|
||
| assert element | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. let's actually assert on the contents of these values rather than just on truthiness |
||
| end | ||
|
|
||
| test "can click stuff within da shadow dom", %{session: session} do | ||
| element = | ||
| session | ||
| |> find(Query.css("shadow-test")) | ||
| |> shadow_root() | ||
| |> click(Query.css("button")) | ||
|
|
||
| assert element | ||
| end | ||
|
|
||
| test "attempting to access a shadow root where there aint one", %{session: session} do | ||
| shadow_root = | ||
| session | ||
| |> find(Query.css("#outside-shadow")) | ||
| |> shadow_root() | ||
|
|
||
| refute shadow_root | ||
| end | ||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| <html> | ||
| <head> | ||
| <script> | ||
| class ShadowTestElement extends HTMLElement { | ||
| connectedCallback() { | ||
| this.attachShadow({mode: 'open'}); | ||
| this.shadowRoot.innerHTML = ` | ||
| <div id="in-shadow">I am in shadow</div> | ||
| <button>Hi!</button> | ||
| `; | ||
| } | ||
| } | ||
| window.customElements.define('shadow-test', ShadowTestElement); | ||
| </script> | ||
| </head> | ||
|
|
||
| <body> | ||
| <div id="outside-shadow">I am out of shadow</div> | ||
| <shadow-test></shadow-test> | ||
| </body> | ||
| </html> |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -1012,6 +1012,26 @@ defmodule Wallaby.Browser do | |||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||
| end | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| @doc """ | ||||||||||||||||||||||||||
| Finds the shadow DOM for the specified element and returns an element corresponding | ||||||||||||||||||||||||||
| to this shadow DOM. Subsequent queries made within element retured by calling `shadow_root` | ||||||||||||||||||||||||||
| will return elements within the shadow DOM, e. g. | ||||||||||||||||||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| ``` | ||||||||||||||||||||||||||
| element = | ||||||||||||||||||||||||||
| session | ||||||||||||||||||||||||||
| |> find(Query.css("shadow-test")) | ||||||||||||||||||||||||||
| |> shadow_root() | ||||||||||||||||||||||||||
| |> find(Query.css("#in-shadow")) | ||||||||||||||||||||||||||
| ``` | ||||||||||||||||||||||||||
|
Comment on lines
+1020
to
+1025
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||
| def shadow_root(%{driver: driver} = element) do | ||||||||||||||||||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please add an |
||||||||||||||||||||||||||
| case driver.shadow_root(element) do | ||||||||||||||||||||||||||
| {:ok, element} -> element | ||||||||||||||||||||||||||
| {:error, _error} -> nil | ||||||||||||||||||||||||||
| end | ||||||||||||||||||||||||||
| end | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| @doc """ | ||||||||||||||||||||||||||
| Validates that the query returns a result. This can be used to define other | ||||||||||||||||||||||||||
| types of matchers. | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change | ||
|---|---|---|---|---|
|
|
@@ -410,6 +410,9 @@ defmodule Wallaby.Chrome do | |||
| defdelegate accept_prompt(session, input, fun), to: WebdriverClient | ||||
| @doc false | ||||
| defdelegate dismiss_prompt(session, fun), to: WebdriverClient | ||||
| @doc false | ||||
| defdelegate shadow_root(element), to: WebdriverClient | ||||
|
|
||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||
| @doc false | ||||
| defdelegate parse_log(log), to: Wallaby.Chrome.Logger | ||||
|
|
||||
|
|
||||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -180,6 +180,11 @@ defmodule Wallaby.Driver do | |||||
| @callback find_elements(Session.t() | Element.t(), Query.compiled()) :: | ||||||
| {:ok, [Element.t()]} | {:error, reason} | ||||||
|
|
||||||
| @doc """ | ||||||
| Invoked to find shadow root of given element | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| """ | ||||||
| @callback shadow_root(Element.t()) :: {:ok, Element.t()} | {:error, reason} | ||||||
|
|
||||||
| @doc """ | ||||||
| Invoked to execute JavaScript in the browser. | ||||||
| """ | ||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -13,6 +13,7 @@ defmodule Wallaby.WebdriverClient do | |||||||||||||||||||||||||||
| | Session.t() | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| @web_element_identifier "element-6066-11e4-a52e-4f735466cecf" | ||||||||||||||||||||||||||||
| @shadow_root_identifier "shadow-6066-11e4-a52e-4f735466cecf" | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| @button_mapping %{left: 0, middle: 1, right: 2} | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
|
|
@@ -48,6 +49,18 @@ defmodule Wallaby.WebdriverClient do | |||||||||||||||||||||||||||
| do: {:ok, elements} | ||||||||||||||||||||||||||||
| end | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| @doc """ | ||||||||||||||||||||||||||||
| Finds the shadow root for a given element. | ||||||||||||||||||||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||
| @spec shadow_root(Element.t()) :: {:ok, Element.t()} | ||||||||||||||||||||||||||||
| def shadow_root(element) do | ||||||||||||||||||||||||||||
| with {:ok, resp} <- request(:get, element.url <> "/shadow"), | ||||||||||||||||||||||||||||
| {:ok, shadow_root} <- Map.fetch(resp, "value"), | ||||||||||||||||||||||||||||
| element <- cast_as_element(element, shadow_root) do | ||||||||||||||||||||||||||||
| {:ok, element} | ||||||||||||||||||||||||||||
| end | ||||||||||||||||||||||||||||
| end | ||||||||||||||||||||||||||||
|
Comment on lines
+56
to
+61
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| @doc """ | ||||||||||||||||||||||||||||
| Sets the value of an element. | ||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||
|
|
@@ -699,6 +712,16 @@ defmodule Wallaby.WebdriverClient do | |||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
| end | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| defp cast_as_element(parent, %{@shadow_root_identifier => id}) do | ||||||||||||||||||||||||||||
| %Wallaby.Element{ | ||||||||||||||||||||||||||||
| id: id, | ||||||||||||||||||||||||||||
| session_url: parent.session_url, | ||||||||||||||||||||||||||||
| url: parent.session_url <> "/shadow/#{id}", | ||||||||||||||||||||||||||||
| parent: parent, | ||||||||||||||||||||||||||||
| driver: parent.driver | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
| end | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| # Retrieves the text from an alert, prompt or confirm. | ||||||||||||||||||||||||||||
| @spec alert_text(Session.t()) :: {:ok, String.t()} | ||||||||||||||||||||||||||||
| defp alert_text(session) do | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -55,6 +55,22 @@ defmodule Wallaby.HTTPClientTest do | |||||
| assert {:error, :stale_reference} = Client.request(:post, bypass_url(bypass, "/my_url")) | ||||||
| end | ||||||
|
|
||||||
| test "with a non-existant shadow root response", %{bypass: bypass} do | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please add a success case test |
||||||
| Bypass.expect(bypass, fn conn -> | ||||||
| send_json_resp(conn, 404, %{ | ||||||
| "sessionId" => "abc123", | ||||||
| "status" => 10, | ||||||
| "value" => %{ | ||||||
| "message" => | ||||||
| "no such shadow root\n (Session info: headless chrome=111.0.5563.64)\n (Driver info: chromedriver=110.0.5481.77 (65ed616c6e8ee3fe0ad64fe83796c020644d42af-refs/branch-heads/5481@{#839}),platform=Mac OS X 12.0.1 arm64)" | ||||||
| } | ||||||
| }) | ||||||
| end) | ||||||
|
|
||||||
| assert {:error, :shadow_root_not_found} = | ||||||
| Client.request(:post, bypass_url(bypass, "/my_url")) | ||||||
| end | ||||||
|
|
||||||
| test "with an obscure status code", %{bypass: bypass} do | ||||||
| expected_message = "message from an obscure error" | ||||||
|
|
||||||
|
|
||||||
Uh oh!
There was an error while loading. Please reload this page.