Skip to content

Commit 7348fa4

Browse files
authored
Add Page.bring_to_front/2 (#32)
Add `PlaywrightEx.Page.bring_to_front/2` as a thin wrapper over Playwright's `bringToFront` page channel method. Closes #29
1 parent 7b6b977 commit 7348fa4

3 files changed

Lines changed: 43 additions & 0 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/).
1515
- `PlaywrightEx.Page.expect_url/2` for explicit URL expectations on pages.
1616
- Regex support in argument serialization/deserialization using protocol-native `{r: %{p, f}}` values.
1717
- `PlaywrightEx.Page.reload/2` to reload current page.
18+
- `PlaywrightEx.Page.bring_to_front/2` to activate a page tab. #29
1819
### Fixed
1920
- `PlaywrightEx.Frame.wait_for_selector/2`: crash when `state` is `"hidden"` or `"detached"` (result has no element). #22
2021
- `PlaywrightEx.BrowserContext.add_init_script/2` and `PlaywrightEx.Page.add_init_script/2`: use `source` parameter name required by Playwright protocol (instead of `content`).

lib/playwright_ex/channels/page.ex

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,35 @@ defmodule PlaywrightEx.Page do
7474
|> ChannelResponse.unwrap(& &1)
7575
end
7676

77+
schema =
78+
NimbleOptions.new!(
79+
connection: PlaywrightEx.Channel.connection_opt(),
80+
timeout: PlaywrightEx.Channel.timeout_opt()
81+
)
82+
83+
@doc """
84+
Brings page to front (activates tab).
85+
86+
Reference: https://playwright.dev/docs/api/class-page#page-bring-to-front
87+
88+
## Options
89+
#{NimbleOptions.docs(schema)}
90+
"""
91+
@schema schema
92+
@type bring_to_front_opt :: unquote(NimbleOptions.option_typespec(schema))
93+
@spec bring_to_front(PlaywrightEx.guid(), [bring_to_front_opt() | PlaywrightEx.unknown_opt()]) ::
94+
{:ok, any()} | {:error, any()}
95+
def bring_to_front(page_id, opts \\ []) do
96+
{connection, opts} =
97+
opts |> PlaywrightEx.Channel.validate_known!(@schema) |> Keyword.pop!(:connection)
98+
99+
{timeout, opts} = Keyword.pop!(opts, :timeout)
100+
101+
connection
102+
|> Connection.send(%{guid: page_id, method: :bring_to_front, params: Map.new(opts)}, timeout)
103+
|> ChannelResponse.unwrap(& &1)
104+
end
105+
77106
schema =
78107
NimbleOptions.new!(
79108
connection: PlaywrightEx.Channel.connection_opt(),

test/playwright_ex/page_test.exs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,22 @@
11
defmodule PlaywrightEx.PageTest do
22
use PlaywrightExCase, async: true
33

4+
alias PlaywrightEx.BrowserContext
45
alias PlaywrightEx.Frame
56
alias PlaywrightEx.Page
67

8+
describe "bring_to_front/2" do
9+
test "activates pages without protocol errors", %{browser_context: browser_context, page: page} do
10+
{:ok, second_page} = BrowserContext.new_page(browser_context.guid, timeout: @timeout)
11+
12+
assert {:ok, _} = Frame.goto(page.main_frame.guid, url: "about:blank", timeout: @timeout)
13+
assert {:ok, _} = Frame.goto(second_page.main_frame.guid, url: "about:blank", timeout: @timeout)
14+
15+
assert {:ok, _} = Page.bring_to_front(page.guid, timeout: @timeout)
16+
assert {:ok, _} = Page.bring_to_front(second_page.guid, timeout: @timeout)
17+
end
18+
end
19+
720
describe "add_init_script/2" do
821
test "applies script on navigation", %{page: page, frame: frame} do
922
assert {:ok, _} =

0 commit comments

Comments
 (0)