Esempio n. 1
0
        public void Execute_ClickOnSelector_FindSingleElement()
        {
            // Arrange
            var webDriver = Substitute.For <IWebDriver>();

            // Act
            IgnoreExceptions.Run(() => _defaultCommand.Execute(webDriver));

            // Assert
            var element = webDriver.Received().FindElement(By.CssSelector(DefaultSelector));
        }
Esempio n. 2
0
        public void Execute_Selector_FindSingleElement()
        {
            // Arrange
            var webDriver = Substitute.For <IWebDriver>();

            // Act
            IgnoreExceptions.Run(() => _defaultCommand.Execute(webDriver));

            // Assert
            webDriver.ReceivedWithAnyArgs().FindElement(null);
        }
Esempio n. 3
0
        public void Execute_MouseClickDouble_PerformDoubleClick()
        {
            // Arrange
            var webDriver = Substitute.For <IWebDriver>();
            var command   = Substitute.ForPartsOf <ClickCommand>(DefaultSelector, MouseClick.Double);

            // Act
            IgnoreExceptions.Run(() => command.Execute(webDriver));

            // Assert
            command.ReceivedWithAnyArgs().PerformDoubleClickOnElement(null, null);
        }
Esempio n. 4
0
        public void Execute_NoClearRequested_ClearNotCalled()
        {
            // Arrange
            var command    = new TypeCommand(DefaultText, DefaultSelector, false);
            var webDriver  = Substitute.For <IWebDriver>();
            var webElement = Substitute.For <IWebElement>();

            // Act
            IgnoreExceptions.Run(() => command.Execute(webDriver));

            // Assert
            webElement.DidNotReceive().Clear();
        }
Esempio n. 5
0
        public void Execute_NoArgs_CallsSendKeys()
        {
            // Arrange
            var webDriver  = Substitute.For <IWebDriver>();
            var webElement = Substitute.For <IWebElement>();

            webDriver.FindElementBySelector(DefaultSelector).Returns(webElement);

            // Act
            IgnoreExceptions.Run(() => _defaultCommand.Execute(webDriver));

            // Assert
            webElement.Received().SendKeys(DefaultText);
        }
Esempio n. 6
0
        public void Execute_OffsetElement_PerformDragCoordinateToElement()
        {
            // Arrange
            var webDriver  = Substitute.For <IWebDriver, IHasInputDevices>();
            var webElement = Substitute.For <IWebElement, ILocatable>();
            var element    = new ElementProxy(webElement);
            var command    = Substitute.ForPartsOf <DragCommand>(0, 0, element);

            // Act
            IgnoreExceptions.Run(() => command.Execute(webDriver));

            // Assert
            command.Received().PerformDragCoordinateToElement(webDriver, 0, 0, webElement);
        }
Esempio n. 7
0
        public void Execute_ExistingElement_PerformMoveMouseToElement()
        {
            // Arrange
            var webDriver  = Substitute.For <IWebDriver>();
            var webElement = Substitute.For <IWebElement>();
            var element    = new ElementProxy(webElement);
            var command    = Substitute.ForPartsOf <HoverCommand>(element);

            // Act
            IgnoreExceptions.Run(() => command.Execute(webDriver));

            // Assert
            command.Received().PerformMoveMouseToElement(webDriver, webElement);
        }
Esempio n. 8
0
        public void Execute_OffsetOffset_PerformDragCoordinateToCoordinate()
        {
            // Arrange
            var webDriver  = Substitute.For <IWebDriver, IHasInputDevices>();
            var webElement = Substitute.For <IWebElement, ILocatable>();
            var command    = Substitute.ForPartsOf <DragCommand>(0, 0, 0, 0);

            webDriver.FindElement(null).ReturnsForAnyArgs(webElement);

            // Act
            IgnoreExceptions.Run(() => command.Execute(webDriver));

            // Assert
            command.Received().PerformDragCoordinateToCoordinate(webDriver, 0, 0, 0, 0);
        }
Esempio n. 9
0
        public void Execute_OffsetSelector_PerformDragCoordinateToElement()
        {
            // Arrange
            var webDriver  = Substitute.For <IWebDriver, IHasInputDevices>();
            var webElement = Substitute.For <IWebElement, ILocatable>();
            var command    = Substitute.ForPartsOf <DragCommand>(0, 0, "B");

            webDriver.FindElement(By.CssSelector("B")).Returns(webElement);

            // Act
            IgnoreExceptions.Run(() => command.Execute(webDriver));

            // Assert
            command.Received().PerformDragCoordinateToElement(webDriver, 0, 0, webElement);
        }
Esempio n. 10
0
        public void Execute_NoArgs_NavigateToUri()
        {
            // Arrange
            Uri uri        = new Uri(DefaultUrl);
            var webDriver  = Substitute.For <IWebDriver>();
            var navigation = Substitute.For <INavigation>();

            webDriver.Navigate().Returns(navigation);

            // Act
            IgnoreExceptions.Run(() => _defaultCommand.Execute(webDriver));

            // Assert
            navigation.Received().GoToUrl(uri);
        }
Esempio n. 11
0
        public void Execute_ClearRequested_ClearCalled()
        {
            // Arrange
            var command    = new TypeCommand(DefaultText, DefaultSelector, true);
            var webDriver  = Substitute.For <IWebDriver>();
            var webElement = Substitute.For <IWebElement>();

            webDriver.FindElementBySelector(DefaultSelector).Returns(webElement);

            // Act
            IgnoreExceptions.Run(() => command.Execute(webDriver));

            // Assert
            webElement.Received().Clear();
        }
Esempio n. 12
0
        public void Execute_NoArgs_PerformWindowMaximize()
        {
            // Arrange
            var webDriver = Substitute.For <IWebDriver>();
            var options   = Substitute.For <IOptions>();
            var window    = Substitute.For <IWindow>();

            webDriver.Manage().Returns(options);
            options.Window.Returns(window);

            // Act
            IgnoreExceptions.Run(() => _defaultCommand.Execute(webDriver));

            // Assert
            window.Received().Maximize();
        }
Esempio n. 13
0
        public void Execute_TwoElements_PerformDragElementToElement()
        {
            // Arrange
            var webDriver   = Substitute.For <IWebDriver, IHasInputDevices>();
            var webElementA = Substitute.For <IWebElement, ILocatable>();
            var webElementB = Substitute.For <IWebElement, ILocatable>();
            var elementA    = new ElementProxy(webElementA);
            var elementB    = new ElementProxy(webElementB);
            var command     = Substitute.ForPartsOf <DragCommand>(elementA, elementB);

            // Act
            IgnoreExceptions.Run(() => command.Execute(webDriver));

            // Assert
            command.Received().PerformDragElementToElement(webDriver, webElementA, webElementB);
        }
Esempio n. 14
0
        public void Execute_TwoSelectors_PerformDragElementToElement()
        {
            // Arrange
            var webDriver   = Substitute.For <IWebDriver, IHasInputDevices>();
            var webElementA = Substitute.For <IWebElement, ILocatable>();
            var webElementB = Substitute.For <IWebElement, ILocatable>();
            var command     = Substitute.ForPartsOf <DragCommand>("A", "B");

            webDriver.FindElement(By.CssSelector("A")).Returns(webElementA);
            webDriver.FindElement(By.CssSelector("B")).Returns(webElementB);

            // Act
            IgnoreExceptions.Run(() => command.Execute(webDriver));

            // Assert
            command.Received().PerformDragElementToElement(webDriver, webElementA, webElementB);
        }
Esempio n. 15
0
        public void Execute_NoArgs_CallSaveAsFile(string filename, ScreenshotImageFormat imageFormat)
        {
            // Arrange
            var command    = new ScreenshotCommand(filename, DefaultDirectoryPath);
            var webDriver  = Substitute.For <IWebDriver, ITakesScreenshot>();
            var screenshot = Substitute.For <Screenshot>(Convert.ToBase64String(Encoding.UTF8.GetBytes("fake")));

            webDriver.TakeScreenshot().Returns(screenshot);

            // Act
            IgnoreExceptions.Run(() => command.Execute(webDriver));

            // Assert
            IgnoreExceptions.Run <ArgumentException>(() =>
            {
                screenshot.Received().SaveAsFile(filename, imageFormat);
            });
        }
        /// <summary>
        /// Logs an exception to the configured error store, or the in-memory default store if none is configured
        /// </summary>
        /// <param name="ex">The exception to log</param>
        /// <param name="context">The HTTPContext to record variables from.  If this isn't a web request, pass <see langword="null" /> in here</param>
        /// <param name="appendFullStackTrace">Wehther to append a full stack trace to the exception's detail</param>
        /// <param name="rollupPerServer">Whether to log up per-server, e.g. errors are only duplicates if they have same stack on the same machine</param>
        /// <param name="customData">Any custom data to store with the exception like UserId, etc...this will be rendered as JSON in the error view for script use</param>
        /// <remarks>
        /// When dealing with a non web requests, pass <see langword="null" /> in for context.
        /// It shouldn't be forgotten for most web application usages, so it's not an optional parameter.
        /// </remarks>
        public static void LogException(Exception ex, HttpContext context, bool appendFullStackTrace = false, bool rollupPerServer = false, Dictionary <string, string> customData = null)
        {
            if (!_enableLogging)
            {
                return;
            }
            try
            {
                if (IgnoreRegexes.Any(re => re.IsMatch(ex.ToString())))
                {
                    return;
                }
                if (IgnoreExceptions.Any(type => IsDescendentOf(ex.GetType(), type.ToString())))
                {
                    return;
                }

                if (customData == null && GetCustomData != null)
                {
                    customData = new Dictionary <string, string>();
                    try
                    {
                        GetCustomData(ex, context, customData);
                    }
                    catch (Exception cde)
                    {
                        // if there was an error getting custom errors, log it so we can display such in the view...and not fail to log the original error
                        customData.Add(CustomDataErrorKey, cde.ToString());
                    }
                }

                var error = new Error(ex, context)
                {
                    RollupPerServer = rollupPerServer,
                    CustomData      = customData
                };

                var exCursor = ex;
                while (exCursor != null)
                {
                    if (exCursor.Data.Contains("SQL"))
                    {
                        error.SQL = exCursor.Data["SQL"] as string;
                    }
                    exCursor = exCursor.InnerException;
                }

                if (appendFullStackTrace)
                {
                    var frames = new StackTrace(fNeedFileInfo: true).GetFrames();
                    if (frames != null)
                    {
                        error.Detail += "\n\nFull Trace:\n\n" + string.Join("", frames.Skip(2));
                    }
                }

                Trace.WriteLine(ex); // always echo the error to trace for local debugging
                Default.Log(error);
            }
            catch (Exception e)
            {
                Trace.WriteLine(e);
            }
        }
        /// <summary>
        /// Logs an exception to the configured error store, or the in-memory default store if none is configured
        /// </summary>
        /// <param name="ex">The exception to log</param>
        /// <param name="context">The HTTPContext to record variables from.  If this isn't a web request, pass <see langword="null" /> in here</param>
        /// <param name="applicationMessage">The application message</param>
        /// <param name="appendFullStackTrace">Whether to append a full stack trace to the exception's detail</param>
        /// <param name="rollupPerServer">Whether to log up per-server, e.g. errors are only duplicates if they have same stack on the same machine</param>
        /// <param name="customData">Any custom data to store with the exception like UserId, etc...this will be rendered as JSON in the error view for script use</param>
        /// <param name="applicationName">If specified, the application name to log with, if not specified the name in the config is used</param>
        /// <returns>The Error created, if one was created and logged, null if nothing was logged</returns>
        /// <remarks>
        /// When dealing with a non web requests, pass <see langword="null" /> in for context.
        /// It shouldn't be forgotten for most web application usages, so it's not an optional parameter.
        /// </remarks>
        public static Error LogException(Exception ex, HttpContext context, string applicationMessage = null, bool appendFullStackTrace = false, bool rollupPerServer = false, Dictionary <string, string> customData = null, string applicationName = null)
        {
            if (!_enableLogging)
            {
                return(null);
            }
            try
            {
                if (IgnoreRegexes.Any(re => re.IsMatch(ex.ToString())))
                {
                    return(null);
                }
                if (IgnoreExceptions.Any(type => IsDescendentOf(ex.GetType(), type.ToString())))
                {
                    return(null);
                }

                if (customData == null && GetCustomData != null)
                {
                    customData = new Dictionary <string, string>();
                    try
                    {
                        GetCustomData(ex, context, customData);
                    }
                    catch (Exception cde)
                    {
                        // if there was an error getting custom errors, log it so we can display such in the view...and not fail to log the original error
                        customData.Add(CustomDataErrorKey, cde.ToString());
                    }
                }

                var error = new Error(ex, context, applicationName, applicationMessage)
                {
                    RollupPerServer = rollupPerServer,
                    CustomData      = customData
                };

                if (GetIPAddress != null)
                {
                    try
                    {
                        error.IPAddress = GetIPAddress();
                    }
                    catch (Exception gipe)
                    {
                        // if there was an error getting the IP, log it so we can display such in the view...and not fail to log the original error
                        error.CustomData.Add(CustomDataErrorKey, "Fetching IP Adddress: " + gipe);
                    }
                }

                var exCursor = ex;
                while (exCursor != null)
                {
                    AddErrorData(error, exCursor);
                    exCursor = exCursor.InnerException;
                }

                if (appendFullStackTrace)
                {
                    var frames = new StackTrace(fNeedFileInfo: true).GetFrames();
                    if (frames != null)
                    {
                        error.Detail += "\n\nFull Trace:\n\n" + string.Join("", frames.Skip(2));
                    }
                    error.ErrorHash = error.GetHash();
                }

                if (OnBeforeLog != null)
                {
                    try
                    {
                        var args = new ErrorBeforeLogEventArgs(error);
                        OnBeforeLog(Default, args);
                        if (args.Abort)
                        {
                            return(null);            // if we've been told to abort, then abort dammit!
                        }
                    }
                    catch (Exception e)
                    {
                        Trace.WriteLine(e);
                    }
                }

                Trace.WriteLine(ex); // always echo the error to trace for local debugging
                Default.Log(error);

                if (OnAfterLog != null)
                {
                    try
                    {
                        OnAfterLog(Default, new ErrorAfterLogEventArgs(error));
                    }
                    catch (Exception e)
                    {
                        Trace.WriteLine(e);
                    }
                }
                return(error);
            }
            catch (Exception e)
            {
                Trace.WriteLine(e);
                return(null);
            }
        }