Skip to content

Commit

Permalink
Put metadata into useragent for Firefox (#614)
Browse files Browse the repository at this point in the history
This enables async tests while using the default selenium configuration.
  • Loading branch information
mhanberg authored Aug 1, 2021
1 parent 5557024 commit 1577ec0
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .tool-versions
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
elixir 1.11.2-otp-21
erlang 21.3
erlang 23.3.4.4
68 changes: 68 additions & 0 deletions integration_test/selenium/selenium_capabilities_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
defmodule Wallaby.Integration.SeleniumCapabilitiesTest do
use ExUnit.Case, async: false
use Wallaby.DSL

import Wallaby.SettingsTestHelpers

alias Wallaby.Integration.SessionCase
alias Wallaby.WebdriverClient

setup do
ensure_setting_is_reset(:wallaby, :selenium)
end

describe "capabilities" do
test "reads default capabilities" do
expected_capabilities = %{
javascriptEnabled: true,
browserName: "firefox",
"moz:firefoxOptions": %{
args: ["-headless"],
prefs: %{
"general.useragent.override" =>
"Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36"
}
}
}

create_session_fn = fn url, capabilities ->
assert capabilities == expected_capabilities

WebdriverClient.create_session(url, capabilities)
end

{:ok, session} = SessionCase.start_test_session(create_session_fn: create_session_fn)

session
|> visit("page_1.html")
|> assert_has(Query.text("Page 1"))

assert :ok = Wallaby.end_session(session)
end

test "reads capabilities from application config" do
expected_capabilities = %{
browserName: "firefox",
"moz:firefoxOptions": %{
args: ["-headless"]
}
}

Application.put_env(:wallaby, :selenium, capabilities: expected_capabilities)

create_session_fn = fn url, capabilities ->
assert capabilities == expected_capabilities

WebdriverClient.create_session(url, capabilities)
end

{:ok, session} = SessionCase.start_test_session(create_session_fn: create_session_fn)

session
|> visit("page_1.html")
|> assert_has(Query.text("Page 1"))

assert :ok = Wallaby.end_session(session)
end
end
end
25 changes: 18 additions & 7 deletions lib/wallaby/selenium.ex
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,10 @@ defmodule Wallaby.Selenium do

@behaviour Wallaby.Driver

alias Wallaby.{Driver, Element, Session}
alias Wallaby.Helpers.KeyCodes
alias Wallaby.Metadata
alias Wallaby.WebdriverClient
alias Wallaby.{Driver, Element, Session}

@typedoc """
Options to pass to Wallaby.start_session/1
Expand Down Expand Up @@ -88,9 +89,10 @@ defmodule Wallaby.Selenium do
@spec start_session([start_session_opts]) :: Wallaby.Driver.on_start_session() | no_return
def start_session(opts \\ []) do
base_url = Keyword.get(opts, :remote_url, "http://localhost:4444/wd/hub/")
capabilities = Keyword.get(opts, :capabilities, capabilities_from_config())
create_session = Keyword.get(opts, :create_session_fn, &WebdriverClient.create_session/2)
capabilities = Keyword.get(opts, :capabilities, capabilities_from_config(opts))

with {:ok, response} <- WebdriverClient.create_session(base_url, capabilities) do
with {:ok, response} <- create_session.(base_url, capabilities) do
id = response["sessionId"]

session = %Session{
Expand All @@ -108,10 +110,10 @@ defmodule Wallaby.Selenium do
end
end

defp capabilities_from_config() do
defp capabilities_from_config(opts) do
:wallaby
|> Application.get_env(:selenium, [])
|> Keyword.get(:capabilities, default_capabilities())
|> Keyword.get(:capabilities, default_capabilities(opts))
end

@doc false
Expand Down Expand Up @@ -330,12 +332,21 @@ defmodule Wallaby.Selenium do
end

@doc false
def default_capabilities do
def default_capabilities(opts \\ []) do
user_agent =
Metadata.append(
"Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36",
opts[:metadata]
)

%{
javascriptEnabled: true,
browserName: "firefox",
"moz:firefoxOptions": %{
args: ["-headless"]
args: ["-headless"],
prefs: %{
"general.useragent.override" => user_agent
}
}
}
end
Expand Down
8 changes: 7 additions & 1 deletion test/wallaby/selenium/start_session_config_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,13 @@ defmodule Wallaby.Selenium.StartSessionConfigTest do
"desiredCapabilities" => %{
"browserName" => "firefox",
"javascriptEnabled" => true,
"moz:firefoxOptions" => %{"args" => ["-headless"]}
"moz:firefoxOptions" => %{
"args" => ["-headless"],
"prefs" => %{
"general.useragent.override" =>
"Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36"
}
}
}
}

Expand Down

0 comments on commit 1577ec0

Please sign in to comment.