Example #1
0
 public static bool CloseDialog(int times = 3, Action <IAlert> handleAlert = null)
 {
     if (handleAlert == null)
     {
         handleAlert = (alert) => alert.Accept();
     }
     do
     {
         try
         {
             IAlert alert = WebDriverManager.Driver.SwitchTo().Alert();
             handleAlert(alert);
             WebDriverManager.CurrentSwitchTo("");
             Thread.Sleep(2000);
             return(true);
         }
         catch (NoAlertPresentException)
         {
             Thread.Sleep(2000);
             continue;
         }
     } while (times-- > 0);
     return(false);
 }
Example #2
0
        public bool Click(Func <bool> isClicked, int waitTimeout = -1, params Action[] actions)
        {
            waitTimeout = waitTimeout > 0 ? waitTimeout : DefaultWaitElementGoneTimeout;

            actions = actions == null || actions.Length == 0 ? new Action[] { Click, ClickByScript, () => Element.SendKeys(Keys.Enter) } : actions;

            try
            {
                actions[0].Invoke();
                if (isClicked == null || isClicked())
                {
                    return(true);
                }
            }
            catch
            {
            }

            DateTime untilMoment = DateTime.Now.AddMilliseconds(waitTimeout);
            int      attempts    = 0;

            do
            {
                Thread.Sleep(waitTimeout / 10);
                try
                {
                    if (isClicked())
                    {
                        return(true);
                    }
                    switch ((++attempts) % actions.Length)
                    {
                    case 0:
                        Logger.D("Try to click it by script");
                        actions[0].Invoke();
                        break;

                    case 1:
                        Logger.D("Try to click it another time");
                        actions[1].Invoke();
                        break;

                    case 2:
                        actions[2].Invoke();
                        break;
                    }
                    Thread.Sleep(DefaultWaitBeforeReClicking);
                }
                catch (StaleElementReferenceException)
                {
                    if (isClicked())
                    {
                        return(true);
                    }
                }
                catch (NullReferenceException)
                {
                    if (isClicked())
                    {
                        return(true);
                    }
                }
                catch (UnhandledAlertException)
                {
                    try
                    {
                        Driver.SwitchTo().Alert().Accept();
                        WebDriverManager.CurrentSwitchTo("");
                    }
                    catch
                    {
                    }
                    Thread.Sleep(2000);

                    continue;
                }
                catch
                {
                }
            } while (DateTime.Now < untilMoment);

            Logger.I("Clicking failed to get expected result.");
            return(false);
        }
Example #3
0
 public virtual string Switch()
 {
     return(WebDriverManager.CurrentSwitchTo(this.FramePath));
 }