Add test/hk_browser_session_tests.erl
Browse files
test/hk_browser_session_tests.erl
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
%%%-------------------------------------------------------------------
|
| 2 |
+
%%% @doc Covers "browser navigation" from `<test>'.
|
| 3 |
+
%%%
|
| 4 |
+
%%% This is a real integration test against an actual headless
|
| 5 |
+
%%% Chromium process via CDP -- not a mock -- because the whole
|
| 6 |
+
%%% point of HK-BROWSER is that it drives a real browser. It is
|
| 7 |
+
%%% skipped (rather than failed) when the configured
|
| 8 |
+
%%% `chromium_executable' isn't present on disk, so the suite still
|
| 9 |
+
%%% runs somewhere without a Chromium install; CI/dev machines that
|
| 10 |
+
%%% ran `bin/bootstrap.sh' will have one.
|
| 11 |
+
%%% @end
|
| 12 |
+
%%%-------------------------------------------------------------------
|
| 13 |
+
-module(hk_browser_session_tests).
|
| 14 |
+
-include_lib("eunit/include/eunit.hrl").
|
| 15 |
+
|
| 16 |
+
setup() -> hk_test_helper:start_app().
|
| 17 |
+
teardown(Ok) -> hk_test_helper:stop_app(Ok).
|
| 18 |
+
|
| 19 |
+
browser_fixture_test_() ->
|
| 20 |
+
case chromium_available() of
|
| 21 |
+
true ->
|
| 22 |
+
{timeout, 30, {setup, fun setup/0, fun teardown/1, fun(_) -> [
|
| 23 |
+
{"open a tab, navigate to a data: URL, and read its text",
|
| 24 |
+
fun navigate_and_read/0},
|
| 25 |
+
{"extract_links returns the anchors on the page",
|
| 26 |
+
fun extract_links/0}
|
| 27 |
+
] end}};
|
| 28 |
+
false ->
|
| 29 |
+
{"chromium_executable not found on disk -- skipped", fun() -> ok end}
|
| 30 |
+
end.
|
| 31 |
+
|
| 32 |
+
navigate_and_read() ->
|
| 33 |
+
SessionId = hk_id:new(<<"sess">>),
|
| 34 |
+
{ok, Pid} = hk_browser_session_sup:start_session(#{session_id => SessionId, profile => <<"test">>}),
|
| 35 |
+
Html = <<"data:text/html,<html><body><h1>Hello HyperKitty</h1></body></html>">>,
|
| 36 |
+
{ok, TabId} = hk_browser_session:open_tab(Pid, Html),
|
| 37 |
+
{ok, Text} = hk_browser_session:read_page(Pid, TabId),
|
| 38 |
+
?assertNotEqual(nomatch, binary:match(Text, <<"Hello HyperKitty">>)),
|
| 39 |
+
ok = hk_browser_session:close_session(Pid).
|
| 40 |
+
|
| 41 |
+
extract_links() ->
|
| 42 |
+
SessionId = hk_id:new(<<"sess">>),
|
| 43 |
+
{ok, Pid} = hk_browser_session_sup:start_session(#{session_id => SessionId, profile => <<"test">>}),
|
| 44 |
+
Html = <<"data:text/html,<html><body><a href='https://example.com/one'>One</a>"
|
| 45 |
+
"<a href='https://example.com/two'>Two</a></body></html>">>,
|
| 46 |
+
{ok, TabId} = hk_browser_session:open_tab(Pid, Html),
|
| 47 |
+
{ok, Links} = hk_browser_session:extract_links(Pid, TabId),
|
| 48 |
+
?assertEqual(2, length(Links)),
|
| 49 |
+
ok = hk_browser_session:close_session(Pid).
|
| 50 |
+
|
| 51 |
+
%%% internal
|
| 52 |
+
|
| 53 |
+
chromium_available() ->
|
| 54 |
+
application:load(hyperkitty), % harmless if already loaded; ensures env is readable here
|
| 55 |
+
Path = application:get_env(hyperkitty, chromium_executable, "/usr/bin/chromium"),
|
| 56 |
+
filelib:is_file(Path).
|