Skip to content

Commit

Permalink
Add Locator.drag_to/3
Browse files Browse the repository at this point in the history
...which in turn drives out the implementation of:

- `Frame.drag_and_drop/4`
- `Page.drag_and_drop/4`
  • Loading branch information
coreyti committed Aug 9, 2024
1 parent aad4161 commit f5a8b93
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 21 deletions.
7 changes: 5 additions & 2 deletions lib/playwright/frame.ex
Original file line number Diff line number Diff line change
Expand Up @@ -265,8 +265,11 @@ defmodule Playwright.Frame do

# ---

# @spec drag_and_drop(Frame.t(), binary(), binary(), options()) :: :ok
# def drag_and_drop(frame, source, target, options \\ %{})
@spec drag_and_drop(Frame.t(), binary(), binary(), options()) :: Frame.t()
def drag_and_drop(frame, source, target, options \\ %{}) do
params = Map.merge(%{source: source, target: target}, options)
post!(frame, :drag_and_drop, params)
end

# @spec eval_on_selector(Frame.t(), binary(), expression(), any(), options()) :: :ok
# def eval_on_selector(frame, selector, expression, arg \\ nil, options \\ %{})
Expand Down
14 changes: 12 additions & 2 deletions lib/playwright/locator.ex
Original file line number Diff line number Diff line change
Expand Up @@ -403,8 +403,13 @@ defmodule Playwright.Locator do
Frame.dispatch_event(locator.frame, locator.selector, type, event_init, options)
end

# @spec drag_to(Locator.t(), binary(), options()) :: :ok
# def drag_to(locator, target, options \\ %{})
@spec drag_to(Locator.t(), Locator.t(), options()) :: Locator.t()
def drag_to(source, target, options \\ %{}) do
returning(source, fn ->
options = Map.merge(options, %{strict: true})
Frame.drag_and_drop(source.frame, source.selector, target.selector, options)
end)
end

@doc """
Resolves the given `Playwright.Locator` to the first matching DOM element.
Expand Down Expand Up @@ -1303,6 +1308,11 @@ defmodule Playwright.Locator do
# private
# ---------------------------------------------------------------------------

defp returning(subject, task) do
task.()
subject
end

defp with_element(%Locator{frame: frame} = locator, options, task) do
params = Map.merge(options, %{selector: locator.selector})

Expand Down
28 changes: 16 additions & 12 deletions lib/playwright/page.ex
Original file line number Diff line number Diff line change
Expand Up @@ -237,10 +237,10 @@ defmodule Playwright.Page do
Get the `Playwright.BrowserContext` that the page belongs to.
"""
@spec context(t()) :: BrowserContext.t()
def context(owner)
def context(page)

def context(%Page{session: session} = owner) do
Channel.find(session, {:guid, owner.parent.guid})
def context(%Page{session: session} = page) do
Channel.find(session, {:guid, page.parent.guid})
end

@doc """
Expand All @@ -261,19 +261,23 @@ defmodule Playwright.Page do
main_frame(page) |> Frame.dispatch_event(selector, type, event_init, options)
end

# ---
@spec drag_and_drop(Page.t(), binary(), binary(), options()) :: Page.t()
def drag_and_drop(page, source, target, options \\ %{}) do
with_latest(page, fn page ->
main_frame(page) |> Frame.drag_and_drop(source, target, options)
end)
end

# @spec drag_and_drop(Page.t(), binary(), binary(), options()) :: :ok
# def drag_and_drop(page, source, target, options \\ %{})
# ---

# @spec emulate_media(t(), options()) :: :ok
# def emulate_media(page, options \\ %{})

# ---

@spec eval_on_selector(t(), binary(), binary(), term(), map()) :: term()
def eval_on_selector(%Page{} = owner, selector, expression, arg \\ nil, options \\ %{}) do
main_frame(owner)
def eval_on_selector(%Page{} = page, selector, expression, arg \\ nil, options \\ %{}) do
main_frame(page)
|> Frame.eval_on_selector(selector, expression, arg, options)
end

Expand Down Expand Up @@ -659,7 +663,7 @@ defmodule Playwright.Page do
# ---

# @spec unroute(t(), function()) :: :ok
# def unroute(owner, handler \\ nil)
# def unroute(page, handler \\ nil)

# @spec unroute_all(t(), map()) :: :ok
# def unroute_all(page, options \\ %{})
Expand All @@ -674,10 +678,10 @@ defmodule Playwright.Page do
# ---

# @spec video(t()) :: Video.t() | nil
# def video(owner, handler \\ nil)
# def video(page, handler \\ nil)

# @spec viewport_size(t()) :: dimensions() | nil
# def viewport_size(owner)
# def viewport_size(page)

# @spec wait_for_event(t(), binary(), map()) :: map()
# def wait_for_event(page, event, options \\ %{})
Expand Down Expand Up @@ -716,7 +720,7 @@ defmodule Playwright.Page do
# def wait_for_url(page, url, options \\ %{})

# @spec workers(t()) :: [Worker.t()]
# def workers(owner)
# def workers(page)

# ---

Expand Down
4 changes: 2 additions & 2 deletions lib/playwright/sdk/channel.ex
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,13 @@ defmodule Playwright.SDK.Channel do
end

def recv(session, {nil, message}) when is_map(message) do
# IO.inspect(message, label: "<--- Channel.recv/2 A")
Response.recv(session, message)
# |> IO.inspect(label: "<--- Channel.recv/2 A")
end

def recv(session, {from, message}) when is_map(message) do
# IO.inspect(message, label: "<--- Channel.recv/2 B")
Response.recv(session, message)
# |> IO.inspect(label: "<--- Channel.recv/2 B")
|> reply(from)
end

Expand Down
10 changes: 8 additions & 2 deletions lib/playwright/sdk/channel_owner.ex
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,14 @@ defmodule Playwright.SDK.ChannelOwner do
end
end

defp with_latest(owner, task) do
Channel.find(owner.session, {:guid, owner.guid}) |> task.()
defp returning(%{session: session} = subject, task) do
task.()
Channel.find(session, {:guid, subject.guid})
end

defp with_latest(subject, task) do
Channel.find(subject.session, {:guid, subject.guid}) |> task.()
Channel.find(subject.session, {:guid, subject.guid})
end
end
end
Expand Down
29 changes: 28 additions & 1 deletion test/api/locator_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,14 @@ defmodule Playwright.LocatorTest do
end
end

describe "Locator.dblclick" do
# test_locator_content_frame_should_work

# describe "Locator.count/1" do
# test "returns the number of elements matching the given selector" do
# end
# end

describe "Locator.dblclick/2" do
test "with a button", %{assets: assets, page: page} do
locator = Page.locator(page, "button")
page |> Page.goto(assets.prefix <> "/input/button.html")
Expand Down Expand Up @@ -167,6 +174,26 @@ defmodule Playwright.LocatorTest do
end
end

describe "Locator.drag_to/3" do
test "returns 'subject", %{assets: assets, page: page} do
source = Page.locator(page, "#source")
target = Page.locator(page, "#target")

page |> Page.goto(assets.prefix <> "/drag-n-drop.html")
assert source == Locator.drag_to(source, target)
end

test "enables dragging of a source element toward a target element", %{assets: assets, page: page} do
source = Page.locator(page, "#source")
target = Page.locator(page, "#target")

page |> Page.goto(assets.prefix <> "/drag-n-drop.html")
Locator.drag_to(source, target)

Page.eval_on_selector(page, "#target", "target => target.contains(document.querySelector('#source'))")
end
end

describe "Locator.element_handle/2" do
test "passed as `arg` to a nested Locator", %{assets: assets, page: page} do
page |> Page.goto(assets.prefix <> "/playground.html")
Expand Down
20 changes: 20 additions & 0 deletions test/api/page_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,20 @@ defmodule Playwright.PageTest do
alias Playwright.SDK.Channel
alias Playwright.SDK.Channel.{Error, Event}

describe "Page.drag_and_drop/4" do
test "returns 'subject'", %{assets: assets, page: page} do
page |> Page.goto(assets.prefix <> "/drag-n-drop.html")
assert %Page{} = Page.drag_and_drop(page, "#source", "#target")
end

test "drags the source element to the target element", %{assets: assets, page: page} do
page |> Page.goto(assets.prefix <> "/drag-n-drop.html")
page |> Page.drag_and_drop("#source", "#target")

assert Page.eval_on_selector(page, "#target", "target => target.contains(document.querySelector('#source'))")
end
end

describe "Page.expose_binding/4" do
test "returns 'subject'", %{page: page} do
assert %Page{} = Page.expose_binding(page, "fn", fn -> nil end)
Expand Down Expand Up @@ -47,6 +61,12 @@ defmodule Playwright.PageTest do
# test_expose_function_should_work_after_cross_origin_navigation
# test_expose_function_should_work_with_complex_objects

describe "Page.goto/3" do
test "on success, returns a Response", %{assets: assets, page: page} do
assert %Response{} = Page.goto(page, assets.prefix <> "/empty.html")
end
end

describe "Page.hover/2" do
test "triggers hover state", %{assets: assets, page: page} do
page |> Page.goto(assets.prefix <> "/input/scrollable.html")
Expand Down

0 comments on commit f5a8b93

Please sign in to comment.