Ejemplo n.º 1
0
 public StubbornWebElement(By selector, RemoteWebDriver browser, ISearchContext parent, Func <IWebElement, bool> collectionPredicate, int elementIndex, StubbornConfiguration configuration = null)
 {
     _browser             = browser;
     _parent              = parent;
     _selector            = selector;
     _collectionPredicate = collectionPredicate;
     _elementIndex        = elementIndex;
     _configuration       = configuration ?? StubbornConfiguration.Default;
 }
Ejemplo n.º 2
0
 public StubbornWebElement(By selector, StubbornWebElement parent, int elementIndex = 1, StubbornConfiguration configuration = null)
     : this(selector, parent._browser, parent._browser, null, elementIndex, configuration)
 {
 }
Ejemplo n.º 3
0
 public StubbornWebElement(By selector, RemoteWebDriver browser, ISearchContext parent = null, int elementIndex = 1, StubbornConfiguration configuration = null)
     : this(selector, browser, parent ?? browser, null, elementIndex, configuration)
 {
 }
Ejemplo n.º 4
0
        public static void Do <TResult1, TResult2>(RemoteWebDriver browser,
                                                   Func <IWebElement> webElementSource,
                                                   Action <Func <IWebElement> > seleniumAction,
                                                   Func <Func <IWebElement>, TResult1> expectedConditionAfterAction,
                                                   Func <IWebDriver, TResult2> errorWaitCondition = null,
                                                   int maxRetries    = 10,
                                                   WaitTime waitTime = WaitTime.Short,
                                                   [CallerMemberName] string caller    = "",
                                                   StubbornConfiguration configuration = null,
                                                   string logMessage = null)
        {
            configuration = configuration ?? StubbornConfiguration.Default;

            configuration.Log.Info(logMessage ?? caller);

            var wait      = new WebDriverWait(browser, waitTime.ToTimeSpan());
            int attemptNo = 0;

            while (true)
            {
                configuration.BeforeDoActions.ForEach(action => action(browser));

                var actionException = Try(() => seleniumAction(webElementSource));

                configuration.BetweenDoActions.ForEach(action => action(browser));

                var expectedConditionException = Try(() => wait.Until(expectedConditionAfterAction, webElementSource));

                if (actionException == null && expectedConditionException == null)
                {
                    return;
                }

                if (attemptNo > 0 && actionException != null && expectedConditionException == null)
                {
                    configuration.Log.Warning($"Action threw exception (\"{actionException.Message}\") but excepted condition is met");
                    return;
                }

                var relevantException = actionException ?? expectedConditionException;
                if (attemptNo >= maxRetries)
                {
                    ExceptionDispatchInfo.Capture(relevantException).Throw();
                }

                attemptNo++;
                try
                {
                    if (errorWaitCondition != null)
                    {
                        wait.Until(errorWaitCondition);
                    }
                    else
                    {
                        Thread.Sleep(WaitTime.Short.ToTimeSpan());
                    }
                }
                catch (Exception)
                {
                    // Ignore wait errors - just try to perform the core action again
                }

                configuration.Log.Warning($"Repeating {caller}");
            }
        }