Skip to content
Merged
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
40 changes: 38 additions & 2 deletions lib/playwright_ex/channels/browser.ex
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,31 @@ defmodule PlaywrightEx.Browser do
doc: "Specific user agent to use in this context."
],
viewport: [
type: :any,
doc: "Sets a consistent viewport for each page. Map with `:width` and `:height`, or `nil` to disable."
type:
{:or,
[
nil,
map: [
width: [
type: :pos_integer,
required: true,
doc: "Page width in CSS pixels."
],
height: [
type: :pos_integer,
required: true,
doc: "Page height in CSS pixels."
]
]
]},
type_spec:
Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

type_spec required, because NimbleOptions can't handle {:or, [nil, map: ...]}.

quote(
do:
nil
| %{width: pos_integer(), height: pos_integer()}
),
type_doc: "`nil | %{width: pos_integer(), height: pos_integer()}`",
doc: "Sets a consistent viewport for each page. Set to `nil` to disable consistent viewport emulation."
]
)

Expand All @@ -84,12 +107,25 @@ defmodule PlaywrightEx.Browser do
def new_context(browser_id, opts \\ []) do
{connection, opts} = opts |> PlaywrightEx.Channel.validate_known!(@schema) |> Keyword.pop!(:connection)
{timeout, opts} = Keyword.pop!(opts, :timeout)
opts = prepare_new_context_opts(opts)

connection
|> Connection.send(%{guid: browser_id, method: :new_context, params: Map.new(opts)}, timeout)
|> ChannelResponse.unwrap_create(:context, connection)
end

defp prepare_new_context_opts(opts) do
case Keyword.fetch(opts, :viewport) do
{:ok, nil} ->
opts
|> Keyword.delete(:viewport)
|> Keyword.put(:no_default_viewport, true)

_ ->
opts
end
end

schema =
NimbleOptions.new!(
connection: PlaywrightEx.Channel.connection_opt(),
Expand Down
19 changes: 19 additions & 0 deletions test/playwright_ex/browser_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
defmodule PlaywrightEx.BrowserTest do
use PlaywrightExCase, async: true

alias PlaywrightEx.Browser

describe "new_context/2" do
test "consistent viewport size", %{browser: browser} do
assert {:ok, _} =
Browser.new_context(browser.guid,
viewport: %{width: 800, height: 600},
timeout: @timeout
)
end

test "no default viewport", %{browser: browser} do
assert {:ok, _} = Browser.new_context(browser.guid, viewport: nil, timeout: @timeout)
end
end
end
Loading