Exemple #1
0
        public async Task JavascriptAsyncErrorsThrowUnhandledException()
        {
            const string ExceptionMessage = "nooo";

            var taskCompletionSource = new TaskCompletionSource <Exception>();

            await Run(async() => {
                await WithUnhandledExceptionHandling(async() => {
                    TargetView.ExecuteScript($"function foo() {{ throw new Error('{ExceptionMessage}'); }}; setTimeout(function() {{ foo(); }}, 1); ");

                    await taskCompletionSource.Task;
                    var exception = taskCompletionSource.Task.Result;

                    StringAssert.Contains(ExceptionMessage, exception.Message);

                    var stack = exception.StackTrace.Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
                    Assert.AreEqual(2, stack.Length);
                    StringAssert.StartsWith("   at foo in about:blank:line 1", stack.ElementAt(0), "Found " + stack.ElementAt(0));
                    StringAssert.StartsWith("   at <anonymous> in about:blank:line 1", stack.ElementAt(1), "Found " + stack.ElementAt(1));
                },
                                                     e => {
                    taskCompletionSource.SetResult(e);
                    return(true);
                });
            });
        }
Exemple #2
0
        public void UnhandledExceptionEventIsCalled()
        {
            const string ExceptionMessage = "nooo";

            Exception exception = null;

            var controlUnhandled    = false;
            var dispatcherUnhandled = false;
            var markAsHandled       = true;

            DispatcherUnhandledExceptionEventHandler unhandledDispatcherException = (o, e) => {
                exception           = e.Exception;
                dispatcherUnhandled = true;
                e.Handled           = true;
            };

            Action <int> assertResult = (result) => {
                Assert.NotNull(exception);
                Assert.IsTrue(exception.Message.Contains(exception.Message));
                Assert.AreEqual(2, result, "Result should not be affected");
            };

            TargetView.Dispatcher.UnhandledException += unhandledDispatcherException;

            WithUnhandledExceptionHandling(() => {
                try {
                    TargetView.ExecuteScript($"throw new Error('{ExceptionMessage}')");
                    var result = TargetView.EvaluateScript <int>("1+1"); // force exception to occur

                    assertResult(result);
                    Assert.IsTrue(controlUnhandled);
                    Assert.IsFalse(dispatcherUnhandled);

                    controlUnhandled = false;
                    markAsHandled    = false;

                    TargetView.ExecuteScript($"throw new Error('{ExceptionMessage}')");
                    result = TargetView.EvaluateScript <int>("1+1"); // force exception to occur

                    WaitFor(() => dispatcherUnhandled);

                    assertResult(result);
                    Assert.IsTrue(controlUnhandled);
                    Assert.IsTrue(dispatcherUnhandled);
                } finally {
                    TargetView.Dispatcher.UnhandledException -= unhandledDispatcherException;
                }
            },
                                           e => {
                exception        = e;
                controlUnhandled = true;
                return(markAsHandled);
            });
        }
Exemple #3
0
        public void ExecutionOrderIsRespected()
        {
            try {
                TargetView.ExecuteScript("x = ''");
                var expectedResult = "";
                // queue 10000 scripts
                for (var i = 0; i < 10000; i++)
                {
                    TargetView.ExecuteScript($"x += '{i},'");
                    expectedResult += i + ",";
                }
                var result = TargetView.EvaluateScript <string>("x");
                Assert.AreEqual(expectedResult, result);

                TargetView.ExecuteScript("x = '-'");
                result = TargetView.EvaluateScript <string>("x");
                Assert.AreEqual("-", result);
            } finally {
                TargetView.EvaluateScript <bool>("delete x");
            }
        }
Exemple #4
0
        public void JavascriptAsyncErrorsThrowUnhandledException()
        {
            const string ExceptionMessage = "nooo";

            Exception exception = null;

            WithUnhandledExceptionHandling(() => {
                TargetView.ExecuteScript($"function foo() {{ throw new Error('{ExceptionMessage}'); }}; setTimeout(function() {{ foo(); }}, 1); ");

                WaitFor(() => exception != null);

                Assert.IsTrue(exception.Message.Contains(ExceptionMessage), "Found " + exception.Message);
                var stack = exception.StackTrace.Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
                Assert.AreEqual(2, stack.Length);
                Assert.True(stack.ElementAt(0).StartsWith("   at foo in about:blank:line 1"), "Found " + stack.ElementAt(0));
                Assert.True(stack.ElementAt(1).StartsWith("   at <anonymous> in about:blank:line 1"), "Found " + stack.ElementAt(1));
            },
                                           e => {
                exception = e;
                return(true);
            });
        }
        public async Task UnhandledExceptionEventIsCalled()
        {
            await Run(() => {
                const string ExceptionMessage = "nooo";

                var taskCompletionSource = new TaskCompletionSource <Exception>();

                WithUnhandledExceptionHandling(() => {
                    TargetView.ExecuteScript($"throw new Error('{ExceptionMessage}')");

                    var result = TargetView.EvaluateScript <int>("1+1"); // force exception to occur
                    Assert.AreEqual(2, result, "Result should not be affected");

                    var exception = taskCompletionSource.Task.Result;

                    StringAssert.Contains(ExceptionMessage, exception.Message);
                },
                                               e => {
                    taskCompletionSource.SetResult(e);
                    return(true);
                });
            });
        }