From 3581b419bb92d8c284b9792e5f392f9e7c116514 Mon Sep 17 00:00:00 2001 From: Caio Peluti Date: Thu, 14 May 2026 19:04:31 -0300 Subject: [PATCH 1/3] Added Page.close to the Page API --- lib/playwright_ex/channels/page.ex | 31 ++++++++++++++++++++++++++++++ test/playwright_ex/page_test.exs | 9 +++++++++ 2 files changed, 40 insertions(+) diff --git a/lib/playwright_ex/channels/page.ex b/lib/playwright_ex/channels/page.ex index 64c7dce..6b28c0c 100644 --- a/lib/playwright_ex/channels/page.ex +++ b/lib/playwright_ex/channels/page.ex @@ -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 diff --git a/test/playwright_ex/page_test.exs b/test/playwright_ex/page_test.exs index 521deb5..3fadc4c 100644 --- a/test/playwright_ex/page_test.exs +++ b/test/playwright_ex/page_test.exs @@ -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) From 457a8e57f3bf49271045beb3b00f3a859a3c4a79 Mon Sep 17 00:00:00 2001 From: Fredrik Teschke Date: Wed, 20 May 2026 09:57:08 +0200 Subject: [PATCH 2/3] Apply suggestion from @ftes --- lib/playwright_ex/channels/page.ex | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/playwright_ex/channels/page.ex b/lib/playwright_ex/channels/page.ex index 6b28c0c..b84fe35 100644 --- a/lib/playwright_ex/channels/page.ex +++ b/lib/playwright_ex/channels/page.ex @@ -385,7 +385,6 @@ defmodule PlaywrightEx.Page do timeout: PlaywrightEx.Channel.timeout_opt(), reason: [ type: :string, - required: false, doc: "The reason to be reported to the operations interrupted by the page closure." ] ) From 7be8621b6b5ad06def2c2d9e7a2d3996e1f06031 Mon Sep 17 00:00:00 2001 From: Fredrik Teschke Date: Wed, 20 May 2026 09:57:14 +0200 Subject: [PATCH 3/3] Apply suggestion from @ftes --- test/playwright_ex/page_test.exs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/test/playwright_ex/page_test.exs b/test/playwright_ex/page_test.exs index 3fadc4c..bfdde02 100644 --- a/test/playwright_ex/page_test.exs +++ b/test/playwright_ex/page_test.exs @@ -33,10 +33,7 @@ defmodule PlaywrightEx.PageTest do describe "close/2" do test "Close page", %{page: page, frame: _frame} do - assert {:ok, _} = - Page.close(page.guid, - timeout: @timeout - ) + assert {:ok, _} = Page.close(page.guid, timeout: @timeout) end end