protected bool WaitUntilTrue(int timeMs, string code)
        {
            RestartTimer();
            bool found = false;

            while (ElapsedMs < timeMs)
            {
                string totalCode = "(function() { " +
                                   "    return (" + code + ");" +
                                   "})();";

                Task task = Chromium.EvaluateScriptAsync(totalCode).ContinueWith(x => {
                    if (x.Result != null && x.Result.Success && x.Result.Result != null)
                    {
                        found = (bool)x.Result.Result;
                    }
                    else
                    {
                        found = false;
                    }
                });
                task.Wait(timeMs);
                if (found)
                {
                    break;
                }
                Application.DoEvents();
            }

            return(found);
        }
        protected bool ClickOnObjectByClass(string className, int index)
        {
            bool result = true;

            try {
                Task task = Chromium.EvaluateScriptAsync(
                    "(function() { " +
                    "    var links = document.getElementsByClassName('" + className + "'); " +
                    "    if(links.length == 0) return false;" +
                    "    links[" + index + "].click();" +
                    "    return true;" +
                    "})();").ContinueWith(x => {
                    if (x.Result != null && x.Result.Result != null)
                    {
                        Console.WriteLine("clickOnObject " + className + " -> " + x.Result.Result.ToString());
                    }
                    if (x.Result == null || x.Result.Result == null || (!(bool)x.Result.Result))
                    {
                        result = false;
                    }
                });
                task.Wait(10000);
            }
            catch (Exception e) {
                Telemetry.Default.TrackException(e);
                result = false;
            }
            if (!result)
            {
                return(false);
            }
            return(true);
        }
        protected bool ClickOnHref(string href)
        {
            bool result = true;

            try {
                Task task = Chromium.EvaluateScriptAsync(
                    "(function() { " +
                    "    var links = document.getElementsByTagName('a'); " +
                    "    for(i=0; i < links.length; i++) {" +
                    "        if(links[i].href == '" + href + "') {" +
                    "            links[i].click();" +
                    "            return true;" +
                    "        }" +
                    "    }" +
                    "    return false;" +
                    "})();").ContinueWith(x => {
                    if (x.Result != null && !((bool)x.Result.Result))
                    {
                        result = false;
                    }
                });
                task.Wait(10000);
            }
            catch (Exception e) {
                Telemetry.Default.TrackException(e);
                result = false;
            }
            if (!result)
            {
                return(false);
            }
            return(true);
        }
        protected bool ClickOnObjectByProperty(string className, string property, string value)
        {
            bool result = true;
            Task task   = Chromium.EvaluateScriptAsync(
                "(function() { " +
                "    var links = document.getElementsByClassName('" + className + "'); " +
                "    for(i=0; i < links.length; i++) {" +
                "        if(links[i]." + property + " == '" + value + "') {" +
                "            links[i].click();" +
                "            return true;" +
                "        }" +
                "    }" +
                "    return false;" +
                "})();").ContinueWith(x => {
                if (x.Result != null && !((bool)x.Result.Result))
                {
                    result = false;
                }
            });

            task.Wait(10000);
            if (!result)
            {
                return(false);
            }
            return(true);
        }
        public bool SetElementValueById(string id, string value)
        {
            bool result = true;
            Task task   = Chromium.EvaluateScriptAsync(
                "(function() { " +
                "    var input = document.getElementById('" + id + "');" +
                "    if(input != null) {" +
                "        input.value = " + value + ";" +
                "        input.defaultValue = " + value + ";" +
                "        var ev = new Event('input', { bubbles: true});" +
                "        ev.simulated = true;" +
                "        input.dispatchEvent(ev);" +
                "        return true;" +
                "    }" +
                "    return false;" +
                "})();").ContinueWith(x => {
                if (x.Result != null && !((bool)x.Result.Result))
                {
                    result = false;
                }
            });

            task.Wait(5000);
            if (!result)
            {
                return(false);
            }
            return(true);
        }
        async void OnAutorizationDocumentLoadedCore()
        {
            StopTimer();

            Task <JavascriptResponse> task = Chromium.EvaluateScriptAsync(
                "var me = document.getElementById('loginform-email'); me.focus();" +
                "me.value ='" + Login + "'; me.blur();");
            await task;

            if (!task.Result.Success)
            {
                return;
            }

            task = Chromium.EvaluateScriptAsync(
                "var pass = document.getElementById('loginform-password'); pass.focus();" +
                "pass.value = '" + Password + "'; pass.blur();");
            await task;

            if (!task.Result.Success)
            {
                return;
            }

            State = PortalState.Autorization;
            task  = Chromium.EvaluateScriptAsync(
                "var button = document.getElementsByClassName('btn btn-primary'); " +
                "button[0].click();");
            await task;

            if (!task.Result.Success)
            {
                return;
            }
        }
 public bool FindElementById(string id)
 {
     return((bool)Chromium.EvaluateScriptAsync(
                "(function() { " +
                "    var input = document.getElementById('" + id + "');" +
                "    return input != null;" +
                "})();").Result.Result);
 }
 public string GetElementByIdContent(string id)
 {
     return((string)Chromium.EvaluateScriptAsync(
                "(function() { " +
                "    var input = document.getElementById('" + id + "');" +
                "    if(input != null) {" +
                "        return input.innerText;" +
                "    }" +
                "    return null;" +
                "})();").Result.Result);
 }
 public bool CheckElementByClassNameContent(string className, string content)
 {
     return((bool)Chromium.EvaluateScriptAsync(
                "(function() { " +
                "    var input = document.getElementsByClassName('" + className + "');" +
                "    if(input != null && input.length > 0) {" +
                "        return input[0].innerText == '" + content + "';" +
                "    }" +
                "    return false;" +
                "})();").Result.Result);
 }
 public string GetElementByClassNameContent(string className)
 {
     return((string)Chromium.EvaluateScriptAsync(
                "(function() { " +
                "    var input = document.getElementsByClassName('" + className + "');" +
                "    if(input != null && input.length > 0) {" +
                "        return input[0].innerText;" +
                "    }" +
                "    return null;" +
                "})();").Result.Result);
 }
        protected bool SelectOpiton(string className, int optionIndex)
        {
            bool   result  = true;
            string message = "success";

            for (int i = 0; i < 3; i++)
            {
                Task task = Chromium.EvaluateScriptAsync(
                    "(function() {" +
                    "    var select = document.getElementsByClassName('" + className + "');" +
                    "    if(select == null || select.length == 0) return false;" +
                    "    select[0].value = select[0].options[" + optionIndex + "].value;" +
                    "    $(select[0]).trigger('change');" +
                    "    return true;" +
                    "})();"
                    ).ContinueWith(x => {
                    if (x.Result != null && x.Result.Result != null && !((bool)x.Result.Result))
                    {
                        message = x.Result.Message;
                        result  = false;
                    }
                });
                task.Wait(10000);
                if (task.Status != TaskStatus.RanToCompletion)
                {
                    Application.DoEvents();
                }
                if (result)
                {
                    break;
                }
            }
            if (!result)
            {
                return(false);
            }
            return(true);
        }
        public bool ClickOnObjectById(string idName)
        {
            bool result = true;
            Task task   = Chromium.EvaluateScriptAsync(
                "(function() { " +
                "    var item = document.getElementById('" + idName + "'); " +
                "    if(item == null) return false;" +
                "    item.click();" +
                "    return true;" +
                "})();").ContinueWith(x => {
                if (x.Result != null && !((bool)x.Result.Result))
                {
                    result = false;
                }
            });

            task.Wait(10000);
            if (!result)
            {
                return(false);
            }
            return(true);
        }
        protected async Task <T> ExecuteScript <T>(int timeMs, string code, T failValue)
        {
            RestartTimer();

            string totalCode = "(function() { " +
                               code +
                               "})();";

            try {
                Task <JavascriptResponse> task = Chromium.EvaluateScriptAsync(totalCode, TimeSpan.FromMilliseconds(timeMs));
                await task;
                if (task.Result != null && task.Result.Success && task.Result != null)
                {
                    Console.WriteLine("'" + code + "' execution time = " + timer.ElapsedMilliseconds);
                    return((T)task.Result.Result);
                }
            }
            catch (Exception e) {
                Telemetry.Default.TrackException(e);

                return(failValue);
            }
            return(failValue);
        }