-module(butterbee). -compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]). -define(FILEPATH, "src/butterbee.gleam"). -export([init/0, close/1, run_with_config/3, run/2, goto/2, url/1, wait/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", 31). ?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" "\n" " Note: Adds a log filter for `WebSocket handshake failed: Sock(Econnrefused)`\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>>), log_ffi:add_primary_filters( [<<"WebSocket handshake failed: Sock\\(Econnrefused\\)"/utf8>>, <<"Making request"/utf8>>] ), nil. -file("src/butterbee.gleam", 199). ?DOC(false). -spec close(butterbee@webdriver:web_driver(PGD)) -> {ok, PGD} | {error, butterbee@internal@error:butterbee_error()}. close(Driver) -> _pipe = palabres:info(<<"Closing webdriver session"/utf8>>), palabres:log(_pipe), _ = butterbee@internal@commands@browser:close(Driver), _ = butterbee@internal@socket:close(erlang:element(2, Driver)), erlang:element(5, Driver). -file("src/butterbee.gleam", 64). ?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) -> _pipe = palabres:info(<<"Starting browser"/utf8>>), _pipe@1 = palabres:string( _pipe, <<"browser"/utf8>>, butterbee@config:browser_type_to_string(Browser_type) ), palabres:log(_pipe@1), gleam@result:'try'( (butterbee@internal@runner@runner:new(Browser_type, Config)), fun(Browser) -> _pipe@2 = palabres:debug(<<"Getting capabilities"/utf8>>), palabres:log(_pipe@2), Capabilities = begin _pipe@3 = erlang:element(3, Config), gleam@option:unwrap( _pipe@3, butterbee@config:default_capabilities_config() ) end, _pipe@4 = palabres:debug(<<"Getting request"/utf8>>), palabres:log(_pipe@4), gleam@result:'try'( begin _pipe@5 = erlang:element(4, Browser), gleam@option:to_result( _pipe@5, browser_does_not_have_request ) end, fun(Request) -> _pipe@6 = palabres:debug( <<"Checking if browser is ready"/utf8>> ), palabres:log(_pipe@6), gleam@result:'try'( butterbee@internal@retry:until_ok( fun() -> butterbee@internal@commands@session:status( Request ) end ), fun(_) -> _pipe@7 = palabres:debug( <<"Starting webdriver session"/utf8>> ), palabres:log(_pipe@7), gleam@result:'try'( (butterbee@internal@commands@session:new( Request, Capabilities )), fun(_use0) -> {Socket, New} = _use0, Driver = begin _pipe@8 = butterbee@webdriver:new( Socket, Config, Browser ), butterbee@webdriver:with_state( _pipe@8, {ok, New} ) end, Get_tree_parameters = begin _pipe@9 = {get_tree_parameters, none, none}, butterbidi@browsing_context@commands@get_tree:with_max_depth( _pipe@9, 1 ) end, Get_tree = butterbee@internal@retry:until_ok( fun() -> butterbee@internal@commands@browsing_context:get_tree( Driver, Get_tree_parameters ) end ), Info@1 = case Get_tree of {error, Error} -> {error, Error}; {ok, Get_tree@1} -> case erlang:element( 2, erlang:element( 2, Get_tree@1 ) ) of [Info] -> {ok, Info}; _ -> {error, no_browsing_contexts} end end, Driver@1 = case Info@1 of {ok, Info@2} -> _pipe@10 = butterbee@webdriver:with_context( Driver, erlang:element( 3, Info@2 ) ), butterbee@webdriver:with_state( _pipe@10, {ok, Info@2} ); {error, Error@1} -> butterbee@webdriver:with_state( Driver, {error, Error@1} ) end, exception_ffi:defer( fun() -> _pipe@11 = palabres:debug( <<"Test cleanup"/utf8>> ), palabres:log(_pipe@11), close(Driver@1) end, fun() -> _pipe@12 = palabres:info( <<"Starting test"/utf8>> ), palabres:log(_pipe@12), {ok, Run(Driver@1)} end ) end ) end ) end ) end ) end ). -file("src/butterbee.gleam", 46). ?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", 145). ?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), case erlang:element(3, Driver@1) of none -> {error, driver_does_not_have_context}; {some, 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", 179). ?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", 192). ?DOC(" Pause for a given amount of time (in milliseconds) before continuing\n"). -spec wait(PGC, integer()) -> PGC. wait(State, Duration) -> gleam_erlang_ffi:sleep(Duration), State. -file("src/butterbee.gleam", 208). ?DOC(" Returns the state of the test without closing the webdriver session\n"). -spec value(butterbee@webdriver:web_driver(PGG)) -> {ok, PGG} | {error, butterbee@internal@error:butterbee_error()}. value(Driver) -> erlang:element(5, Driver).