How to use WaitForResponseAsync method of PuppeteerSharp.Page class

Best Puppeteer-sharp code snippet using PuppeteerSharp.Page.WaitForResponseAsync

Run Puppeteer-sharp automation tests on LambdaTest cloud grid

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

WaitForOptions.cs

Source: WaitForOptions.cs Github

copy
1using System;
2
3namespace PuppeteerSharp
4{
5    /// <summary>
6    /// Optional waiting parameters.
7    /// </summary>
8    /// <seealso cref="Page.WaitForRequestAsync(Func{Request, bool}, WaitForOptions)"/>
9    /// <seealso cref="Page.WaitForRequestAsync(string, WaitForOptions)"/>
10    /// <seealso cref="Page.WaitForResponseAsync(string, WaitForOptions)"/>
11    /// <seealso cref="Page.WaitForResponseAsync(Func{Response, bool}, WaitForOptions)"/>
12    public class WaitForOptions
13    {
14        /// <summary>
15        /// Maximum time to wait for in milliseconds. Pass 0 to disable timeout.
16        /// The default value can be changed by setting the <see cref="Page.DefaultTimeout"/> property.
17        /// </summary>
18        public int? Timeout { get; set; }
19    }
20}
Full Screen

WaitForResponseTests.cs

Source: WaitForResponseTests.cs Github

copy
1using System;
2using System.Net;
3using System.Threading.Tasks;
4using Xunit;
5using Xunit.Abstractions;
6
7namespace PuppeteerSharp.Tests.PageTests
8{
9    [Collection(TestConstants.TestFixtureCollectionName)]
10    public class WaitForResponseTests : PuppeteerPageBaseTest
11    {
12        public WaitForResponseTests(ITestOutputHelper output) : base(output)
13        {
14        }
15
16        [Fact]
17        public async Task ShouldWork()
18        {
19            await Page.GoToAsync(TestConstants.EmptyPage);
20            var task = Page.WaitForResponseAsync(TestConstants.ServerUrl + "/digits/2.png");
21
22            await Task.WhenAll(
23                task,
24                Page.EvaluateFunctionAsync(@"() => {
25                    fetch('/digits/1.png');
26                    fetch('/digits/2.png');
27                    fetch('/digits/3.png');
28                }")
29            );
30            Assert.Equal(TestConstants.ServerUrl + "/digits/2.png", task.Result.Url);
31        }
32
33        [Fact]
34        public async Task ShouldWorkWithPredicate()
35        {
36            await Page.GoToAsync(TestConstants.EmptyPage);
37            var task = Page.WaitForResponseAsync(response => response.Url == TestConstants.ServerUrl + "/digits/2.png");
38
39            await Task.WhenAll(
40            task,
41            Page.EvaluateFunctionAsync(@"() => {
42                fetch('/digits/1.png');
43                fetch('/digits/2.png');
44                fetch('/digits/3.png');
45            }")
46            );
47            Assert.Equal(TestConstants.ServerUrl + "/digits/2.png", task.Result.Url);
48        }
49
50        [Fact]
51        public async Task ShouldRespectTimeout()
52        {
53            await Page.GoToAsync(TestConstants.EmptyPage);
54            var exception = await Assert.ThrowsAnyAsync<TimeoutException>(async () =>
55                await Page.WaitForResponseAsync(request => false, new WaitForOptions
56                {
57                    Timeout = 1
58                }));
59
60            Assert.Contains("Timeout of 1 ms exceeded", exception.Message);
61        }
62
63        [Fact]
64        public async Task ShouldRespectDefaultTimeout()
65        {
66            await Page.GoToAsync(TestConstants.EmptyPage);
67            Page.DefaultTimeout = 1;
68            var exception = await Assert.ThrowsAnyAsync<TimeoutException>(async () =>
69                await Page.WaitForResponseAsync(request => false));
70
71            Assert.Contains("Timeout of 1 ms exceeded", exception.Message);
72        }
73
74        [Fact]
75        public async Task ShouldWorkWithNoTimeout()
76        {
77            await Page.GoToAsync(TestConstants.EmptyPage);
78            var task = Page.WaitForResponseAsync(TestConstants.ServerUrl + "/digits/2.png", new WaitForOptions
79            {
80                Timeout = 0
81            });
82
83            await Task.WhenAll(
84                task,
85                Page.EvaluateFunctionAsync(@"() => setTimeout(() => {
86                    fetch('/digits/1.png');
87                    fetch('/digits/2.png');
88                    fetch('/digits/3.png');
89                }, 50)")
90            );
91            Assert.Equal(TestConstants.ServerUrl + "/digits/2.png", task.Result.Url);
92        }
93    }
94}
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

Most used method in Page

Trigger WaitForResponseAsync code on LambdaTest Cloud Grid

Execute automation tests with WaitForResponseAsync 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)