Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions lib/playwright_ex/channels/page.ex
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,37 @@ defmodule PlaywrightEx.Page do
|> ChannelResponse.unwrap(& &1)
end

schema =
NimbleOptions.new!(
connection: PlaywrightEx.Channel.connection_opt(),
timeout: PlaywrightEx.Channel.timeout_opt(),
reason: [
type: :string,
required: false,
doc: "The reason to be reported to the operations interrupted by the page closure."
]
)

@doc """
Closes the page.

Reference: https://playwright.dev/docs/api/class-page#page-close

## Options
#{NimbleOptions.docs(schema)}
"""
@schema schema
@type close_opt :: unquote(NimbleOptions.option_typespec(schema))
@spec close(PlaywrightEx.guid(), [close_opt() | PlaywrightEx.unknown_opt()]) :: {:ok, any()} | {:error, any()}
def close(page_id, opts \\ []) do
{connection, opts} = opts |> PlaywrightEx.Channel.validate_known!(@schema) |> Keyword.pop!(:connection)
{timeout, opts} = Keyword.pop!(opts, :timeout)

connection
|> Connection.send(%{guid: page_id, method: :close, params: Map.new(opts)}, timeout)
|> ChannelResponse.unwrap(fn _ -> :ok end)
end

defp main_frame_id!(connection, page_id) do
page_initializer = Connection.initializer!(connection, page_id)
page_initializer.main_frame.guid
Expand Down
9 changes: 9 additions & 0 deletions test/playwright_ex/page_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,15 @@ defmodule PlaywrightEx.PageTest do
end
end

describe "close/2" do
test "Close page", %{page: page, frame: _frame} do
assert {:ok, _} =
Page.close(page.guid,
timeout: @timeout
)
end
end

describe "expect_url/2" do
test "matches current URL with string expectation", %{page: page, frame: frame} do
{:ok, _} = Frame.goto(frame.guid, url: "about:blank#current", timeout: @timeout)
Expand Down