|
| 1 | +using System.Threading.Tasks; |
| 2 | +using NUnit.Framework; |
| 3 | +using PuppeteerSharp.Nunit; |
| 4 | + |
| 5 | +namespace PuppeteerSharp.Tests.StackTraceTests |
| 6 | +{ |
| 7 | + public class StackTraceTests : PuppeteerPageBaseTest |
| 8 | + { |
| 9 | + [Test, PuppeteerTest("stacktrace.spec", "Stack trace", "should work")] |
| 10 | + public void ShouldWork() |
| 11 | + { |
| 12 | + var exception = Assert.ThrowsAsync<EvaluationFailedException>(async () => |
| 13 | + { |
| 14 | + await Page.EvaluateFunctionAsync(@"() => { |
| 15 | + throw new Error('Test'); |
| 16 | + }"); |
| 17 | + }); |
| 18 | + |
| 19 | + Assert.That(exception.Message, Does.Contain("Test")); |
| 20 | + } |
| 21 | + |
| 22 | + [Test, PuppeteerTest("stacktrace.spec", "Stack trace", "should work with handles")] |
| 23 | + public void ShouldWorkWithHandles() |
| 24 | + { |
| 25 | + var exception = Assert.ThrowsAsync<EvaluationFailedException>(async () => |
| 26 | + { |
| 27 | + await Page.EvaluateFunctionHandleAsync(@"() => { |
| 28 | + throw new Error('Test'); |
| 29 | + }"); |
| 30 | + }); |
| 31 | + |
| 32 | + Assert.That(exception.Message, Does.Contain("Test")); |
| 33 | + } |
| 34 | + |
| 35 | + [Test, PuppeteerTest("stacktrace.spec", "Stack trace", "should work with contiguous evaluation")] |
| 36 | + public async Task ShouldWorkWithContiguousEvaluation() |
| 37 | + { |
| 38 | + await using var thrower = await Page.EvaluateFunctionHandleAsync(@"() => { |
| 39 | + return () => { |
| 40 | + throw new Error('Test'); |
| 41 | + }; |
| 42 | + }"); |
| 43 | + |
| 44 | + var exception = Assert.ThrowsAsync<EvaluationFailedException>(async () => |
| 45 | + { |
| 46 | + await thrower.EvaluateFunctionAsync(@"thrower => { |
| 47 | + thrower(); |
| 48 | + }"); |
| 49 | + }); |
| 50 | + |
| 51 | + Assert.That(exception.Message, Does.Contain("Test")); |
| 52 | + } |
| 53 | + |
| 54 | + [Test, PuppeteerTest("stacktrace.spec", "Stack trace", "should work with nested function calls")] |
| 55 | + public void ShouldWorkWithNestedFunctionCalls() |
| 56 | + { |
| 57 | + var exception = Assert.ThrowsAsync<EvaluationFailedException>(async () => |
| 58 | + { |
| 59 | + await Page.EvaluateFunctionAsync(@"() => { |
| 60 | + function a() { |
| 61 | + throw new Error('Test'); |
| 62 | + } |
| 63 | + function b() { |
| 64 | + a(); |
| 65 | + } |
| 66 | + function c() { |
| 67 | + b(); |
| 68 | + } |
| 69 | + function d() { |
| 70 | + c(); |
| 71 | + } |
| 72 | + d(); |
| 73 | + }"); |
| 74 | + }); |
| 75 | + |
| 76 | + Assert.That(exception.Message, Does.Contain("Test")); |
| 77 | + } |
| 78 | + |
| 79 | + [Test, PuppeteerTest("stacktrace.spec", "Stack trace", "should work for none error objects")] |
| 80 | + public async Task ShouldWorkForNonErrorObjects() |
| 81 | + { |
| 82 | + var errorTask = new TaskCompletionSource<PageErrorEventArgs>(); |
| 83 | + |
| 84 | + void ErrorHandler(object sender, PageErrorEventArgs e) |
| 85 | + { |
| 86 | + errorTask.TrySetResult(e); |
| 87 | + Page.PageError -= ErrorHandler; |
| 88 | + } |
| 89 | + |
| 90 | + Page.PageError += ErrorHandler; |
| 91 | + |
| 92 | + await Task.WhenAll( |
| 93 | + errorTask.Task, |
| 94 | + Page.EvaluateFunctionAsync(@"() => { |
| 95 | + // This can happen when a 404 with HTML is returned |
| 96 | + void Promise.reject(new Response()); |
| 97 | + }") |
| 98 | + ); |
| 99 | + |
| 100 | + var error = await errorTask.Task; |
| 101 | + Assert.That(error, Is.Not.Null); |
| 102 | + } |
| 103 | + } |
| 104 | +} |
0 commit comments