Current section
Files
Jump to
Current section
Files
src/butterbee.erl
-module(butterbee).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/butterbee.gleam").
-export([init/0, goto/2, url/1, wait/2, close/1, run_with_config/3, run/2, value/1]).
-if(?OTP_RELEASE >= 27).
-define(MODULEDOC(Str), -moduledoc(Str)).
-define(DOC(Str), -doc(Str)).
-else.
-define(MODULEDOC(Str), -compile([])).
-define(DOC(Str), -compile([])).
-endif.
-file("src/butterbee.gleam", 28).
?DOC(
" Initialize butterbee,\n"
" Call this in the main function of your test, before calling gleeunit.main.\n"
" Then call [`butterbee.run`](file:///home/gelei/Documents/butterbee/butterbee/build/dev/docs/butterbee/butterbee.html#run) in your test\n"
" to start using butterbee.\n"
).
-spec init() -> nil.
init() ->
_pipe = palabres:debug(<<"Initializing butterbee"/utf8>>),
palabres:log(_pipe),
_pipe@1 = palabres:debug(<<"Deleting data_dir"/utf8>>),
palabres:log(_pipe@1),
_ = simplifile_erl:delete(<<"/tmp/butterbee"/utf8>>),
nil.
-file("src/butterbee.gleam", 157).
?DOC(" Navigates to the given url\n").
-spec goto(butterbee@webdriver:web_driver(any()), binary()) -> butterbee@webdriver:web_driver(butterbidi@browsing_context@commands@navigate:navigate_result()).
goto(Driver, Url) ->
Result = case gleam_stdlib:uri_parse(Url) of
{error, nil} ->
{error, {could_not_parse_url, Url}};
{ok, Uri} ->
Driver@1 = begin
_pipe = Driver,
butterbee@webdriver:with_state(_pipe, {ok, Uri})
end,
case erlang:element(5, Driver@1) of
{error, Error} ->
{error, Error};
{ok, Uri@1} ->
Url@1 = gleam@uri:to_string(Uri@1),
gleam@result:'try'(
(butterbee@webdriver:get_context(Driver@1)),
fun(Context) ->
Params = begin
_pipe@1 = butterbidi@browsing_context@commands@navigate:default(
Context,
Url@1
),
butterbidi@browsing_context@commands@navigate:with_wait(
_pipe@1,
interactive
)
end,
butterbee@internal@commands@browsing_context:navigate(
Driver@1,
Params
)
end
)
end
end,
_pipe@2 = Driver,
butterbee@webdriver:with_state(_pipe@2, Result).
-file("src/butterbee.gleam", 188).
?DOC(" Returns the url of the current page\n").
-spec url(butterbee@webdriver:web_driver(any())) -> butterbee@webdriver:web_driver(binary()).
url(Driver) ->
_pipe@2 = case butterbee@internal@commands@browsing_context:get_tree(
Driver,
{get_tree_parameters, none, none}
) of
{error, Error} ->
{error, Error};
{ok, Get_tree_result} ->
_pipe = gleam@list:first(
erlang:element(2, erlang:element(2, Get_tree_result))
),
_pipe@1 = gleam@result:map_error(_pipe, fun(_) -> no_info_found end),
gleam@result:map(
_pipe@1,
fun(Context) -> erlang:element(5, Context) end
)
end,
butterbee@webdriver:map_state(_pipe@2, Driver).
-file("src/butterbee.gleam", 201).
?DOC(" Pause for a given amount of time (in milliseconds) before continuing\n").
-spec wait(VHP, integer()) -> VHP.
wait(State, Duration) ->
gleam_erlang_ffi:sleep(Duration),
State.
-file("src/butterbee.gleam", 208).
?DOC(false).
-spec close(butterbee@webdriver:web_driver(VHQ)) -> {ok, VHQ} |
{error, butterbee@internal@error:butterbee_error()}.
close(Driver) ->
_pipe = palabres:info(<<"Closing webdriver session"/utf8>>),
palabres:log(_pipe),
_ = butterbee@internal@commands@browser:close(Driver),
gleam@result:'try'(
(butterbee@webdriver:get_socket(Driver)),
fun(Socket) ->
_ = butterbee@internal@socket:close(Socket),
erlang:element(5, Driver)
end
).
-file("src/butterbee.gleam", 59).
?DOC(" Start a new webdriver session connect to the browser session, using the ButterbeeConfig type\n").
-spec run_with_config(
list(butterbee@config:browser_type()),
butterbee@config:butterbee_config(),
fun((butterbee@webdriver:web_driver(butterbidi@browsing_context@types@info:info())) -> any())
) -> nil.
run_with_config(Browsers, Config, Run) ->
gleam@list:each(
Browsers,
fun(Browser_type) ->
Driver = begin
_pipe = butterbee@webdriver:new(),
butterbee@webdriver:with_config(_pipe, Config)
end,
Session = case erlang:element(4, Driver) of
none ->
{error, driver_does_not_have_config};
{some, Config@1} ->
_pipe@1 = palabres:info(<<"Starting browser"/utf8>>),
_pipe@2 = palabres:string(
_pipe@1,
<<"browser"/utf8>>,
butterbee@config:browser_type_to_string(Browser_type)
),
palabres:log(_pipe@2),
gleam@result:'try'(
(butterbee@internal@runner@runner:new(
Browser_type,
Config@1
)),
fun(Browser) ->
_pipe@3 = palabres:debug(
<<"Getting capabilities"/utf8>>
),
palabres:log(_pipe@3),
Capabilities = begin
_pipe@4 = erlang:element(3, Config@1),
gleam@option:unwrap(
_pipe@4,
butterbee@config:default_capabilities_config(
)
)
end,
_pipe@5 = palabres:debug(<<"Getting request"/utf8>>),
palabres:log(_pipe@5),
gleam@result:'try'(
begin
_pipe@6 = erlang:element(4, Browser),
gleam@option:to_result(
_pipe@6,
browser_does_not_have_request
)
end,
fun(Request) ->
_pipe@7 = palabres:debug(
<<"Checking if browser is ready"/utf8>>
),
palabres:log(_pipe@7),
gleam@result:'try'(
butterbee@internal@retry:until_ok(
fun() ->
butterbee@internal@commands@session:status(
Request
)
end
),
fun(_) ->
_pipe@8 = palabres:debug(
<<"Starting webdriver session"/utf8>>
),
palabres:log(_pipe@8),
butterbee@internal@commands@session:new(
Request,
Capabilities
)
end
)
end
)
end
)
end,
Driver@2 = case Session of
{error, Error} ->
_pipe@9 = palabres:error(
<<"Failed to create webdriver session"/utf8>>
),
_pipe@10 = palabres:string(
_pipe@9,
<<"error"/utf8>>,
gleam@string:inspect(Error)
),
palabres:log(_pipe@10),
butterbee@webdriver:with_state(Driver, {error, Error});
{ok, New} ->
{Socket, Session@1} = New,
Driver@1 = begin
_pipe@11 = Driver,
_pipe@12 = butterbee@webdriver:with_socket(
_pipe@11,
Socket
),
butterbee@webdriver:with_state(
_pipe@12,
{ok, Session@1}
)
end,
Get_tree_parameters = begin
_pipe@13 = {get_tree_parameters, none, none},
butterbidi@browsing_context@commands@get_tree:with_max_depth(
_pipe@13,
1
)
end,
Get_tree = butterbee@internal@retry:until_ok(
fun() ->
butterbee@internal@commands@browsing_context:get_tree(
Driver@1,
Get_tree_parameters
)
end
),
Info@1 = case Get_tree of
{error, Error@1} ->
{error, Error@1};
{ok, Get_tree@1} ->
case erlang:element(
2,
erlang:element(2, Get_tree@1)
) of
[Info] ->
{ok, Info};
_ ->
{error, no_browsing_contexts}
end
end,
case Info@1 of
{ok, Info@2} ->
_pipe@14 = butterbee@webdriver:with_context(
Driver@1,
erlang:element(3, Info@2)
),
butterbee@webdriver:with_state(
_pipe@14,
{ok, Info@2}
);
{error, Error@2} ->
butterbee@webdriver:with_state(
Driver@1,
{error, Error@2}
)
end
end,
exception_ffi:defer(
fun() -> close(Driver@2) end,
fun() ->
_pipe@15 = palabres:info(<<"Starting test"/utf8>>),
palabres:log(_pipe@15),
Run(Driver@2)
end
)
end
).
-file("src/butterbee.gleam", 41).
?DOC(
" Start a new webdriver session connect to the browser session, \n"
" using the configuration in the gleam.toml file.\n"
" WebDriver holds the browsing context info in its state\n"
).
-spec run(
list(butterbee@config:browser_type()),
fun((butterbee@webdriver:web_driver(butterbidi@browsing_context@types@info:info())) -> any())
) -> nil.
run(Browsers, Run) ->
Config@1 = case butterbee@config:parse_config(<<"gleam.toml"/utf8>>) of
{ok, Config} ->
Config;
{error, Error} ->
_pipe = palabres:info(<<"could not parse gleam.toml"/utf8>>),
_pipe@1 = palabres:string(
_pipe,
<<"error"/utf8>>,
gleam@string:inspect(Error)
),
palabres:log(_pipe@1),
{butterbee_config,
{driver_config, 20000, 5000, <<"/tmp/butterbee"/utf8>>},
none,
none}
end,
run_with_config(Browsers, Config@1, Run).
-file("src/butterbee.gleam", 218).
?DOC(" Returns the state of the test without closing the webdriver session\n").
-spec value(butterbee@webdriver:web_driver(VHT)) -> {ok, VHT} |
{error, butterbee@internal@error:butterbee_error()}.
value(Driver) ->
erlang:element(5, Driver).