0

I have a problem. scriptEventClick does not see the method in scriptGlobal. How should I know that scriptGlobal is registered in JS so that I can later call EvaluateScriptAsPromiseAsync

    public Task<bool> GetResultAfterPageLoad(string pageUrl)
    {
        TaskCompletionSource<bool> tcs = new TaskCompletionSource<bool>();

        EventHandler<LoadingStateChangedEventArgs> onPageLoaded = null;
        onPageLoaded = async (sender, args) =>
        {
            if (!args.IsLoading)
            {
                _browser.LoadingStateChanged -= onPageLoaded;
                // initialization script
                _browser.ExecuteScriptAsyncWhenPageLoaded(scriptGlobal);

                // script for click button
                JavascriptResponse responseClick = await _browser.EvaluateScriptAsPromiseAsync(scriptEventClick);

                tcs.SetResult(_result);
            }
        };

        _browser.LoadingStateChanged += onPageLoaded;
        _browser.Load(pageUrl);
        return tcs.Task;
    }

where

   scriptGlobal = "async function eventClick(){}";
   scriptEventClick = "return eventClick();";
Elgin Maksim
  • 31
  • 1
  • 10

1 Answers1

0

this solution works. but I think this is a bad answer. because I don't want to use delays in a loop

    _browser.ExecuteScriptAsyncWhenPageLoaded(scriptGlobal);

    JavascriptResponse responseGlobal = null;
    do
    {
        responseGlobal = await _browser.EvaluateScriptAsync("eventClick");
        await Task.Delay(100);
    }
    while (responseGlobal.Result == null);

    JavascriptResponse responseClick = await _browser.EvaluateScriptAsPromiseAsync(scriptEventClick);
Elgin Maksim
  • 31
  • 1
  • 10