Skip to content

Commit

Permalink
Wait for page mounting in feature tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bartblast committed Oct 30, 2024
1 parent 68afe01 commit dbcbd0e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
2 changes: 1 addition & 1 deletion test/features/mix.lock
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"erlex": {:hex, :erlex, "0.2.7", "810e8725f96ab74d17aac676e748627a07bc87eb950d2b83acd29dc047a30595", [:mix], [], "hexpm", "3ed95f79d1a844c3f6bf0cea61e0d5612a42ce56da9c03f01df538685365efb0"},
"file_system": {:hex, :file_system, "1.0.1", "79e8ceaddb0416f8b8cd02a0127bdbababe7bf4a23d2a395b983c1f8b3f73edd", [:mix], [], "hexpm", "4414d1f38863ddf9120720cd976fce5bdde8e91d8283353f0e31850fa89feb9e"},
"hackney": {:hex, :hackney, "1.20.1", "8d97aec62ddddd757d128bfd1df6c5861093419f8f7a4223823537bad5d064e2", [:rebar3], [{:certifi, "~> 2.12.0", [hex: :certifi, repo: "hexpm", optional: false]}, {:idna, "~> 6.1.0", [hex: :idna, repo: "hexpm", optional: false]}, {:metrics, "~> 1.0.0", [hex: :metrics, repo: "hexpm", optional: false]}, {:mimerl, "~> 1.1", [hex: :mimerl, repo: "hexpm", optional: false]}, {:parse_trans, "3.4.1", [hex: :parse_trans, repo: "hexpm", optional: false]}, {:ssl_verify_fun, "~> 1.1.0", [hex: :ssl_verify_fun, repo: "hexpm", optional: false]}, {:unicode_util_compat, "~> 0.7.0", [hex: :unicode_util_compat, repo: "hexpm", optional: false]}], "hexpm", "fe9094e5f1a2a2c0a7d10918fee36bfec0ec2a979994cff8cfe8058cd9af38e3"},
"hologram": {:git, "https://github.com/bartblast/hologram.git", "1c0867a612da93339215f0c32d7aaf3770a2c730", []},
"hologram": {:git, "https://github.com/bartblast/hologram.git", "68afe011c262c898eed3ffeee77ef767751832af", []},
"html_entities": {:hex, :html_entities, "0.5.2", "9e47e70598da7de2a9ff6af8758399251db6dbb7eebe2b013f2bbd2515895c3c", [:mix], [], "hexpm", "c53ba390403485615623b9531e97696f076ed415e8d8058b1dbaa28181f4fdcc"},
"httpoison": {:hex, :httpoison, "2.2.1", "87b7ed6d95db0389f7df02779644171d7319d319178f6680438167d7b69b1f3d", [:mix], [{:hackney, "~> 1.17", [hex: :hackney, repo: "hexpm", optional: false]}], "hexpm", "51364e6d2f429d80e14fe4b5f8e39719cacd03eb3f9a9286e61e216feac2d2df"},
"idna": {:hex, :idna, "6.1.1", "8a63070e9f7d0c62eb9d9fcb360a7de382448200fbbd1b106cc96d3d8099df8d", [:rebar3], [{:unicode_util_compat, "~> 0.7.0", [hex: :unicode_util_compat, repo: "hexpm", optional: false]}], "hexpm", "92376eb7894412ed19ac475e4a86f7b413c1b9fbb5bd16dccd57934157944cea"},
Expand Down
28 changes: 21 additions & 7 deletions test/features/test/support/helpers.ex
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ defmodule HologramFeatureTests.Helpers do

def assert_page(session, page_module, params \\ []) do
path = Router.Helpers.page_path(page_module, params)
wait_for_path(session, path)

assert Browser.current_path(session) == path

session
|> wait_for_path(path)
|> wait_for_page_mounting(page_module)
|> wait_for_server_connection()
end

def assert_text(parent, text) when is_binary(text) do
Expand Down Expand Up @@ -84,16 +84,15 @@ defmodule HologramFeatureTests.Helpers do
end

def reload(session) do
session
|> Browser.execute_script("document.location.reload();")
|> wait_for_server_connection()
Browser.execute_script(session, "document.location.reload();")
end

def visit(session, page_module, params \\ []) do
path = Router.Helpers.page_path(page_module, params)

session
|> Browser.visit(path)
|> wait_for_page_mounting(page_module)
|> wait_for_server_connection()
end

Expand All @@ -106,9 +105,24 @@ defmodule HologramFeatureTests.Helpers do
:timer.sleep(100)
wait_for_path(session, path, start_time)
end

session
end

defp wait_for_page_mounting(session, page_module, start_time \\ DateTime.utc_now()) do
callback = fn mounted_page ->
if mounted_page != inspect(page_module) && !timed_out?(start_time) do
:timer.sleep(100)
wait_for_page_mounting(session, page_module, start_time)
end
end

script = "return window?.hologram?.['mountedPage'];"

Browser.execute_script(session, script, [], callback)
end

def wait_for_server_connection(session, start_time \\ DateTime.utc_now()) do
defp wait_for_server_connection(session, start_time \\ DateTime.utc_now()) do
callback = fn connected? ->
if !connected? && !timed_out?(start_time) do
:timer.sleep(100)
Expand Down

0 comments on commit dbcbd0e

Please sign in to comment.