コード例 #1
0
 public void AlertNotText(Expression<Func<string, bool>> matchFunc)
 {
     var compiledFunc = matchFunc.Compile();
     this.commandProvider.AlertText((alertText) =>
     {
         if (compiledFunc(alertText))
         {
             // because the browser blocks, we dismiss the alert when a failure happens so we can cleanly shutdown.
             this.commandProvider.AlertClick(Alert.Cancel);
             this.ReportError("Expected alert text not to match expression [{0}] but it did.", matchFunc.ToExpressionString());
         }
     });
 }
コード例 #2
0
        public void WaitUntil(Expression<Func<bool>> conditionFunc, TimeSpan timeout)
        {
            this.Act(() =>
            {
                DateTime dateTimeTimeout = DateTime.Now.Add(timeout);
                bool isFuncValid = false;
                var compiledFunc = conditionFunc.Compile();

                while (DateTime.Now < dateTimeTimeout)
                {
                    if (compiledFunc() == true)
                    {
                        isFuncValid = true;
                        break;
                    }

                    System.Threading.Thread.Sleep(Settings.DefaultWaitUntilThreadSleep);
                }

                // If func is still not valid, assume we've hit the timeout.
                if (isFuncValid == false)
                {
                    throw new FluentException("Conditional wait passed the timeout [{0}ms] for expression [{1}].", timeout.TotalMilliseconds, conditionFunc.ToExpressionString());
                }
            });
        }
コード例 #3
0
        public void WaitUntil(Expression<Action> conditionAction, TimeSpan timeout)
        {
            this.Act(CommandType.Wait, () =>
            {
                DateTime dateTimeTimeout = DateTime.Now.Add(timeout);
                bool threwException = false;
                var compiledAction = conditionAction.Compile();

                FluentException lastFluentException = null;
                while (DateTime.Now < dateTimeTimeout)
                {
                    try
                    {
                        threwException = false;
                        compiledAction();
                    }
                    catch (FluentException ex)
                    {
                        threwException = true;
                        lastFluentException = ex;
                    }
                    catch (Exception ex)
                    {
                        throw new FluentException("An unexpected exception was thrown inside WaitUntil(Action). See InnerException for details.", ex);
                    }

                    if (!threwException)
                    {
                        break;
                    }

                    System.Threading.Thread.Sleep(this.Settings.WaitUntilInterval);
                }

                // If an exception was thrown the last loop, assume we hit the timeout
                if (threwException == true)
                {
                    throw new FluentException("Conditional wait passed the timeout [{0}ms] for expression [{1}]. See InnerException for details of the last FluentException thrown.", lastFluentException, timeout.TotalMilliseconds, conditionAction.ToExpressionString());
                }
            });
        }
コード例 #4
0
        public void WaitUntil(Expression<Func<bool>> conditionFunc, TimeSpan timeout)
        {
            this.Act(CommandType.Wait, () =>
            {
                DateTime dateTimeTimeout = DateTime.Now.Add(timeout);
                bool isFuncValid = false;
                var compiledFunc = conditionFunc.Compile();

                FluentException lastException = null;
                while (DateTime.Now < dateTimeTimeout)
                {
                    try
                    {
                        if (compiledFunc() == true)
                        {
                            isFuncValid = true;
                            break;
                        }

                        System.Threading.Thread.Sleep(this.Settings.WaitUntilInterval);
                    }
                    catch (FluentException ex)
                    {
                        lastException = ex;
                    }
                    catch (Exception ex)
                    {
                        throw new FluentException("An unexpected exception was thrown inside WaitUntil(Func<bool>). See InnerException for details.", ex);
                    }
                }

                // If func is still not valid, assume we've hit the timeout.
                if (isFuncValid == false)
                {
                    throw new FluentException("Conditional wait passed the timeout [{0}ms] for expression [{1}]. See InnerException for details of the last FluentException thrown.", lastException, timeout.TotalMilliseconds, conditionFunc.ToExpressionString());
                }
            });
        }
コード例 #5
0
 public void False(Expression<Func<bool>> matchFunc)
 {
     this.commandProvider.Act(() =>
     {
         var compiledFunc = matchFunc.Compile();
         if (compiledFunc())
         {
             throw new FluentExpectFailedException("Expected expression [{0}] to return false.", matchFunc.ToExpressionString());
         }
     });
 }
コード例 #6
0
        public void Value(Func<IElement> element, Expression<Func<string, bool>> matchFunc)
        {
            this.commandProvider.Act(() =>
            {
                var compiledFunc = matchFunc.Compile();
                var unwrappedElement = element();
                if (unwrappedElement.IsText)
                {
                    if (!compiledFunc(unwrappedElement.Value))
                    {
                        throw new FluentExpectFailedException("Expected TextElement value to match expression [{0}] but it was actually [{1}].", matchFunc.ToExpressionString(), unwrappedElement.Value);
                    }
                }
                else if (unwrappedElement.IsSelect)
                {
                    if (unwrappedElement.IsMultipleSelect)
                    {
                        var foundMatch = false;
                        foreach (var optionValue in unwrappedElement.SelectedOptionValues)
                        {
                            if (compiledFunc(optionValue))
                            {
                                foundMatch = true;
                                break;
                            }
                        }

                        if (!foundMatch)
                        {
                            throw new FluentExpectFailedException("Expected SelectElement selected options to have at least one option with value matching expression[{0}]. Selected option values include [{1}]", matchFunc.ToExpressionString(), string.Join(",", unwrappedElement.SelectedOptionTextCollection));
                        }
                    }
                    else
                    {
                        if (!compiledFunc(unwrappedElement.Text))
                        {
                            throw new FluentExpectFailedException("Expected SelectElement selected option value to match expression [{0}] but it was actually [{1}].", matchFunc.ToExpressionString(), unwrappedElement.Value);
                        }
                    }
                }
                else
                {
                    if (!compiledFunc(unwrappedElement.Value))
                    {
                        throw new FluentExpectFailedException("Expected element value to match expression [{0}] but it was actually [{1}].", matchFunc.ToExpressionString(), unwrappedElement.Value);
                    }
                }
            });
        }
コード例 #7
0
        public void Url(Expression<Func<Uri, bool>> urlExpression)
        {
            this.commandProvider.Act(() =>
            {
                var compiledExpr = urlExpression.Compile();

                if (compiledExpr(this.commandProvider.Url) != true)
                {
                    throw new FluentExpectFailedException("Expected expression [{0}] to return true.", urlExpression.ToExpressionString());
                }
            });
        }
コード例 #8
0
 public void NotText(ElementProxy element, Expression<Func<string, bool>> matchFunc)
 {
     this.commandProvider.Act(commandType, () =>
     {
         var compiledFunc = matchFunc.Compile();
         var result = elementHasText(element, (elText) => compiledFunc(elText));
         if (result.HasText)
         {
             if (element.Element.IsMultipleSelect)
             {
                 this.ReportError("Expected SelectElement [{0}] selected options to have no options with text matching expression [{1}]. Selected option text values include [{2}]", result.Selector, matchFunc.ToExpressionString(), string.Join(",", element.Element.SelectedOptionTextCollection));
             }
             else if (element.Element.IsSelect)
             {
                 this.ReportError("Expected SelectElement [{0}] selected option text not to match expression [{1}] but it did.", result.Selector, matchFunc.ToExpressionString());
             }
             else
             {
                 this.ReportError("Expected {0} [{1}] text not to match expression [{2}] but it did.", result.ElementType, result.Selector, matchFunc.ToExpressionString());
             }
         }
     });
 }
コード例 #9
0
        public void Text(string selector, Expression<Func<string, bool>> matchFunc)
        {
            this.commandProvider.Act(() =>
            {
                var compiledFunc = matchFunc.Compile();
                var unwrappedElement = this.commandProvider.Find(selector)();
                if (unwrappedElement.IsText)
                {
                    if (!compiledFunc(unwrappedElement.Text))
                    {
                        throw new FluentExpectFailedException("Expected TextElement [{0}] text to match expression [{1}] but it was actually [{2}].", selector, matchFunc.ToExpressionString(), unwrappedElement.Text);
                    }
                }
                else if (unwrappedElement.IsSelect)
                {
                    if (unwrappedElement.IsMultipleSelect)
                    {
                        var foundMatch = false;
                        foreach (var optionText in unwrappedElement.SelectedOptionTextCollection)
                        {
                            if (compiledFunc(optionText))
                            {
                                foundMatch = true;
                                break;
                            }
                        }

                        if (!foundMatch)
                        {
                            throw new FluentExpectFailedException("Expected SelectElement [{0}] selected options to have at least one option with text matching expression[{1}]. Selected option text values include [{2}]", selector, matchFunc.ToExpressionString(), string.Join(",", unwrappedElement.SelectedOptionTextCollection));
                        }
                    }
                    else
                    {
                        if (!compiledFunc(unwrappedElement.Text))
                        {
                            throw new FluentExpectFailedException("Expected SelectElement [{0}] selected option text to match expression [{1}] but it was actually [{2}].", selector, matchFunc.ToExpressionString(), unwrappedElement.Text);
                        }
                    }
                }
                else
                {
                    if (!compiledFunc(unwrappedElement.Text))
                    {
                        throw new FluentExpectFailedException("Expected DOM Element [{0}] text to match expression [{1}] but it was actually [{2}].", selector, matchFunc.ToExpressionString(), unwrappedElement.Text);
                    }
                }
            });
        }
コード例 #10
0
        public void SetValues(Expression<Func<string, bool>> optionMatchingExpression, SelectMode selectMode)
        {
            IEnumerable<Automation.Core.Option> options = null;
            var compiledFunc = optionMatchingExpression.Compile();

            if (selectMode == SelectMode.Text)
            {
                options = _element.Options.Where(x => compiledFunc(x.Text));
            }
            else if (selectMode == SelectMode.Value)
            {
                options = _element.Options.Where(x => compiledFunc(x.Value));
            }

            if (options != null)
            {
                foreach (var option in options)
                {
                    option.Select();
                }

                if (options.Count() == 0)
                {
                    if (selectMode == SelectMode.Value)
                        throw new SelectException("Selection failed. No option values matched expression [{0}] on element.", optionMatchingExpression.ToExpressionString());
                    else if (selectMode == SelectMode.Text)
                        throw new SelectException("Selection failed. No option text matched expression [{0}] on element.", optionMatchingExpression.ToExpressionString());
                    else if (selectMode == SelectMode.Index)
                        throw new SelectException("Selection failed. No options matched collection of indices provided.");
                }

                this.OnChange();
            }
        }
コード例 #11
0
 public void Value(ElementProxy element, Expression<Func<string, bool>> matchFunc)
 {
     this.commandProvider.Act(commandType, () =>
     {
         var compiledFunc = matchFunc.Compile();
         var result = elementHasValue(element, (elValue) => compiledFunc(elValue));
         if (!result.HasValue)
         {
             if (element.Element.IsMultipleSelect)
             {
                 this.ReportError("Expected SelectElement [{0}] selected options to have at least one option with value matching expression [{1}]. Selected option values include [{2}]", result.Selector, matchFunc.ToExpressionString(), string.Join(",", element.Element.SelectedOptionValues));
             }
             else if (element.Element.IsSelect)
             {
                 this.ReportError("Expected SelectElement [{0}] selected option value to match expression [{1}] but it was actually [{2}].", result.Selector, matchFunc.ToExpressionString(), result.ActualValue);
             }
             else
             {
                 this.ReportError("Expected {0} [{1}] value to match expression [{2}] but it was actually [{3}].", result.ElementType, result.Selector, matchFunc.ToExpressionString(), result.ActualValue);
             }
         }
     });
 }
コード例 #12
0
 public void Url(Expression<Func<Uri, bool>> urlExpression)
 {
     this.commandProvider.Act(commandType, () =>
     {
         if (urlExpression.Compile()(this.commandProvider.Url) != true)
         {
             this.ReportError("Expected expression [{0}] to return true. URL was [{1}].", urlExpression.ToExpressionString(), this.commandProvider.Url.ToString());
         }
     });
 }
コード例 #13
0
 public void True(Expression<Func<bool>> matchFunc)
 {
     this.commandProvider.Act(commandType, () =>
     {
         var compiledFunc = matchFunc.Compile();
         if (!compiledFunc())
         {
             this.ReportError("Expected expression [{0}] to return true.", matchFunc.ToExpressionString());
         }
     });
 }
コード例 #14
0
 public void Throws(Expression<Action> matchAction)
 {
     this.commandProvider.Act(commandType, () =>
     {
         if (!throwsException(matchAction))
         {
             this.ReportError("Expected expression [{0}] to throw an exception.", matchAction.ToExpressionString());
         }
     });
 }
コード例 #15
0
        public void WaitUntil(Expression<Action> conditionAction, TimeSpan timeout)
        {
            this.Act(() =>
            {
                DateTime dateTimeTimeout = DateTime.Now.Add(timeout);
                bool threwException = false;
                var compiledAction = conditionAction.Compile();

                while (DateTime.Now < dateTimeTimeout)
                {
                    try
                    {
                        threwException = false;
                        compiledAction();
                    }
                    catch (FluentException)
                    {
                        threwException = true;
                    }

                    if (!threwException)
                    {
                        break;
                    }

                    System.Threading.Thread.Sleep(Settings.DefaultWaitUntilThreadSleep);
                }

                // If an exception was thrown the last loop, assume we hit the timeout
                if (threwException == true)
                {
                    throw new FluentException("Conditional wait passed the timeout [{0}ms]", timeout.TotalMilliseconds, conditionAction.ToExpressionString());
                }
            });
        }
コード例 #16
0
        public void Throws(Expression<Action> matchAction)
        {
            this.commandProvider.Act(() =>
            {
                bool threwException = false;
                var compiledAction = matchAction.Compile();

                try
                {
                    compiledAction();
                }
                catch (FluentExpectFailedException)
                {
                    threwException = true;
                }

                if (!threwException)
                {
                    throw new FluentExpectFailedException("Expected expression [{0}] to throw an exception.", matchAction.ToExpressionString());
                }
            });
        }
コード例 #17
0
 /// <summary>
 /// Expects page URL matches expression.
 /// </summary>
 /// <param name="valueExpression">The value expression.</param>
 public virtual void Url(Expression<Func<Uri, bool>> valueExpression)
 {
     var _compiledFunc = valueExpression.Compile();
     if (!_compiledFunc(Provider.GetUri()))
     {
         Provider.TakeAssertExceptionScreenshot();
         throw new AssertException("URL Assertion failed. Expected URL to match expression [{0}]. Actual URL is [{1}].", valueExpression.ToExpressionString(), Provider.GetUri());
     }
 }
コード例 #18
0
        public void SetValues(Expression<Func<string, bool>> optionMatchingExpression, SelectMode selectMode)
        {
            var compiledFunc = optionMatchingExpression.Compile();
            if (selectMode == SelectMode.Value)
            {
                var options = _element.Options.Where(x => compiledFunc(x.GetAttribute("value"))).Select<IWebElement, string>(x => x.GetAttribute("value")).ToList();
                foreach (var optionValue in options)
                {
                    // dirty hack is dirty!
                    OpenQA.Selenium.Support.UI.SelectElement element = new OpenQA.Selenium.Support.UI.SelectElement(this.GetWebElement(true));
                    element.SelectByValue(optionValue);
                }

                if (options.Count() == 0)
                {
                    throw new SelectException("Selection failed. No option values matched expression [{0}] on element.", optionMatchingExpression.ToExpressionString());
                }
            }
            else if (selectMode == SelectMode.Text)
            {
                var options = _element.Options.Where(x => compiledFunc(x.Text)).Select<IWebElement, string>(x => x.Text).ToList();
                foreach (var optionText in options)
                {
                    // dirty hack is dirty!
                    OpenQA.Selenium.Support.UI.SelectElement element = new OpenQA.Selenium.Support.UI.SelectElement(this.GetWebElement(true));
                    element.SelectByText(optionText);
                }

                if (options.Count() == 0)
                {
                    throw new SelectException("Selection failed. No option text matched expression [{0}] on element.", optionMatchingExpression.ToExpressionString());
                }
            }

            this.OnChange();
        }