How to use ShouldRejectPromiseWithExeption method of PuppeteerSharp.Tests.PageTests.EvaluateTests class

Best Puppeteer-sharp code snippet using PuppeteerSharp.Tests.PageTests.EvaluateTests.ShouldRejectPromiseWithExeption

Run Puppeteer-sharp automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

EvaluateTests.cs

Source: EvaluateTests.cs Github

copy
1using System;
2using System.Collections.Generic;
3using System.Threading.Tasks;
4using Xunit;
5using Xunit.Abstractions;
6
7namespace PuppeteerSharp.Tests.PageTests
8{
9    [Collection("PuppeteerLoaderFixture collection")]
10    public class EvaluateTests : PuppeteerPageBaseTest
11    {
12        public EvaluateTests(ITestOutputHelper output) : base(output)
13        {
14        }
15
16        [Theory]
17        [InlineData("1 + 5;", 6)] //ShouldAcceptSemiColons
18        [InlineData("2 + 5\n// do some math!'", 7)] //ShouldAceptStringComments
19        public async Task BasicIntExressionEvaluationTest(string script, object expected)
20        {
21            var result = await Page.EvaluateExpressionAsync<int>(script);
22            Assert.Equal(expected, result);
23        }
24
25        [Theory]
26        [InlineData("() => 7 * 3", 21)] //ShouldWork
27        [InlineData("() => Promise.resolve(8 * 7)", 56)] //ShouldAwaitPromise
28        public async Task BasicIntFunctionEvaluationTest(string script, object expected)
29        {
30            var result = await Page.EvaluateFunctionAsync<int>(script);
31            Assert.Equal(expected, result);
32        }
33
34        [Fact]
35        public async Task ShouldWorkRightAfterFrameNavigated()
36        {
37            Task<int> frameEvaluation = null;
38
39            Page.FrameNavigated += (sender, e) =>
40            {
41                frameEvaluation = e.Frame.EvaluateFunctionAsync<int>("() => 6 * 7");
42            };
43
44            await Page.GoToAsync(TestConstants.EmptyPage);
45            Assert.Equal(42, await frameEvaluation);
46        }
47
48        [Fact]
49        public async Task ShouldRejectPromiseWithExeption()
50        {
51            var exception = await Assert.ThrowsAsync<EvaluationFailedException>(() =>
52            {
53                return Page.EvaluateFunctionAsync<object>("() => not.existing.object.property");
54            });
55
56            Assert.Contains("not is not defined", exception.Message);
57        }
58
59        [Fact]
60        public async Task SouldReturnComplexObjects()
61        {
62            dynamic obj = new
63            {
64                foo = "bar!"
65            };
66            dynamic result = await Page.EvaluateFunctionAsync("a => a", obj);
67            Assert.Equal("bar!", result.foo.ToString());
68        }
69
70        [Theory]
71        [InlineData("() => NaN", double.NaN)] //ShouldReturnNaN
72        [InlineData("() => -0", -0)] //ShouldReturnNegative0
73        [InlineData("() => Infinity", double.PositiveInfinity)] //ShouldReturnInfinity
74        [InlineData("() => -Infinity", double.NegativeInfinity)] //ShouldReturnNegativeInfinty
75        public async Task BasicEvaluationTest(string script, object expected)
76        {
77            dynamic result = await Page.EvaluateFunctionAsync(script);
78            Assert.Equal(expected, result);
79        }
80
81        [Fact]
82        public async Task ShouldAcceptNullAsOneOfMultipleParameters()
83        {
84            bool result = await Page.EvaluateFunctionAsync<bool>("(a, b) => Object.is(a, null) && Object.is(b, 'foo')", null, "foo");
85            Assert.True(result);
86        }
87
88        [Fact]
89        public async Task ShouldProperlyIgnoreUndefinedFields()
90        {
91            var result = await Page.EvaluateFunctionAsync<Dictionary<string, object>>("() => ({a: undefined})");
92            Assert.Empty(result);
93        }
94
95        [Fact]
96        public async Task ShouldProperlySerializeNullFields()
97        {
98            var result = await Page.EvaluateFunctionAsync<Dictionary<string, object>>("() => ({a: null})");
99            Assert.True(result.ContainsKey("a"));
100            Assert.Null(result["a"]);
101        }
102
103        [Fact]
104        public async Task ShouldFailForWindowObjectUsingEvaluateExpression()
105        {
106            var window = await Page.EvaluateExpressionAsync("window");
107            Assert.Null(window);
108        }
109
110        [Fact]
111        public async Task ShouldFailForWindowObjectUsingEvaluateFunction()
112        {
113            var window = await Page.EvaluateFunctionAsync("() => window");
114            Assert.Null(window);
115        }
116        
117        [Fact]
118        public async Task ShouldAcceptElementHandleAsAnArgument()
119        {
120            await Page.SetContentAsync("<section>42</section>");
121            var element = await Page.QuerySelectorAsync("section");
122            var text = await Page.EvaluateFunctionAsync<string>("e => e.textContent", element);
123            Assert.Equal("42", text);
124        }
125
126        [Fact]
127        public async Task ShouldThrowIfUnderlyingElementWasDisposed()
128        {
129            await Page.SetContentAsync("<section>39</section>");
130            var element = await Page.QuerySelectorAsync("section");
131            Assert.NotNull(element);
132            await element.DisposeAsync();
133            var exception = await Assert.ThrowsAsync<PuppeteerException>(()
134                => Page.EvaluateFunctionAsync<string>("e => e.textContent", element));
135            Assert.Contains("JSHandle is disposed", exception.Message);
136        }
137
138        [Fact]
139        public async Task ShouldThrowIfElementHandlesAreFromOtherFrames()
140        {
141            await FrameUtils.AttachFrameAsync(Page, "frame1", TestConstants.EmptyPage);
142            var bodyHandle = await Page.Frames[1].QuerySelectorAsync("body");
143            var exception = await Assert.ThrowsAsync<PuppeteerException>(()
144                => Page.EvaluateFunctionAsync<string>("body => body.innerHTML", bodyHandle));
145            Assert.Contains("JSHandles can be evaluated only in the context they were created", exception.Message);
146        }
147
148        [Fact]
149        public async Task ShouldAcceptObjectHandleAsAnArgument()
150        {
151            var navigatorHandle = await Page.EvaluateExpressionHandleAsync("navigator");
152            var text = await Page.EvaluateFunctionAsync<string>("e => e.userAgent", navigatorHandle);
153            Assert.Contains("Mozilla", text);
154        }
155
156        [Fact]
157        public async Task ShouldAcceptObjectHandleToPrimitiveTypes()
158        {
159            var aHandle = await Page.EvaluateExpressionHandleAsync("5");
160            var isFive = await Page.EvaluateFunctionAsync<bool>("e => Object.is(e, 5)", aHandle);
161            Assert.True(isFive);
162        }
163
164        [Fact]
165        public async Task ShouldWorkFromInsideAnExposedFunction()
166        {
167            await Page.ExposeFunctionAsync("callController", async (int a, int b) =>
168            {
169                return await Page.EvaluateFunctionAsync("(a, b) => a * b", a, b);
170            });
171            var result = await Page.EvaluateFunctionAsync<int>(@"async function() {
172                return await callController(9, 3);
173            }");
174            Assert.Equal(27, result);
175        }
176    }
177}
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Trigger ShouldRejectPromiseWithExeption code on LambdaTest Cloud Grid

Execute automation tests with ShouldRejectPromiseWithExeption on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)