diff --git a/lib/playwright_ex/channels/page.ex b/lib/playwright_ex/channels/page.ex index 64c7dce..b84fe35 100644 --- a/lib/playwright_ex/channels/page.ex +++ b/lib/playwright_ex/channels/page.ex @@ -379,6 +379,36 @@ defmodule PlaywrightEx.Page do |> ChannelResponse.unwrap(& &1) end + schema = + NimbleOptions.new!( + connection: PlaywrightEx.Channel.connection_opt(), + timeout: PlaywrightEx.Channel.timeout_opt(), + reason: [ + type: :string, + 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 diff --git a/test/playwright_ex/page_test.exs b/test/playwright_ex/page_test.exs index 521deb5..bfdde02 100644 --- a/test/playwright_ex/page_test.exs +++ b/test/playwright_ex/page_test.exs @@ -31,6 +31,12 @@ 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)