How to use NetworkConditions class of PuppeteerSharp package

Best Puppeteer-sharp code snippet using PuppeteerSharp.NetworkConditions

Run Puppeteer-sharp automation tests on LambdaTest cloud grid

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

PageEmulateNetworkConditionsTests.cs

Source: PageEmulateNetworkConditionsTests.cs Github

copy
1using System.Threading.Tasks;
2using PuppeteerSharp.Tests.Attributes;
3using PuppeteerSharp.Xunit;
4using Xunit;
5using Xunit.Abstractions;
6
7namespace PuppeteerSharp.Tests.EmulationTests
8{
9    [Collection(TestConstants.TestFixtureCollectionName)]
10    public class PageEmulateNetworkConditionsTests : PuppeteerPageBaseTest
11    {
12        public PageEmulateNetworkConditionsTests(ITestOutputHelper output) : base(output)
13        {
14        }
15
16        [PuppeteerTest("emulation.spec.ts", "Page.emulateNetworkConditions", "should change navigator.connection.effectiveType")]
17        [SkipBrowserFact(skipFirefox: true)]
18        public async Task ShouldChangeNavigatorConnectionEffectiveType()
19        {
20            var slow3G = Puppeteer.NetworkConditions[NetworkConditions.Slow3G];
21            var fast3G = Puppeteer.NetworkConditions[NetworkConditions.Fast3G];
22
23            Assert.Equal("4g", await Page.EvaluateExpressionAsync<string>("window.navigator.connection.effectiveType").ConfigureAwait(false));
24            await Page.EmulateNetworkConditionsAsync(fast3G);
25            Assert.Equal("3g", await Page.EvaluateExpressionAsync<string>("window.navigator.connection.effectiveType").ConfigureAwait(false));
26            await Page.EmulateNetworkConditionsAsync(slow3G);
27            Assert.Equal("2g", await Page.EvaluateExpressionAsync<string>("window.navigator.connection.effectiveType").ConfigureAwait(false));
28            await Page.EmulateNetworkConditionsAsync(null);
29        }
30    }
31}
32
Full Screen

PredefinedNetworkConditions.cs

Source: PredefinedNetworkConditions.cs Github

copy
1using System;
2using System.Collections.Generic;
3using System.Collections.ObjectModel;
4
5namespace PuppeteerSharp
6{
7    /// <summary>
8    /// Predefined network conditions.
9    /// </summary>
10    public static class PredefinedNetworkConditions
11    {
12        private static readonly Dictionary<string, NetworkConditions> Conditions = new Dictionary<string, NetworkConditions>
13        {
14            [NetworkConditions.Slow3G] = new NetworkConditions
15            {
16                Download = ((500 * 1000) / 8) * 0.8,
17                Upload = ((500 * 1000) / 8) * 0.8,
18                Latency = 400 * 5,
19            },
20            [NetworkConditions.Fast3G] = new NetworkConditions
21            {
22                Download = ((1.6 * 1000 * 1000) / 8) * 0.9,
23                Upload = ((750 * 1000) / 8) * 0.9,
24                Latency = 150 * 3.75,
25            },
26        };
27
28        private static Lazy<IReadOnlyDictionary<string, NetworkConditions>> _readOnlyConditions =
29            new Lazy<IReadOnlyDictionary<string, NetworkConditions>>(() => new ReadOnlyDictionary<string, NetworkConditions>(Conditions));
30
31        internal static IReadOnlyDictionary<string, NetworkConditions> ToReadOnly() => _readOnlyConditions.Value;
32    }
33}
34
Full Screen

NetworkConditions.cs

Source: NetworkConditions.cs Github

copy
1namespace PuppeteerSharp
2{
3    /// <summary>
4    /// Options to be used in <see cref="Page.EmulateNetworkConditionsAsync(NetworkConditions)"/>
5    /// </summary>
6    public class NetworkConditions
7    {
8        /// <summary>
9        /// Key to be used with <see cref="Puppeteer.NetworkConditions()"/>
10        /// </summary>
11        public const string Slow3G = "Slow 3G";
12
13        /// <summary>
14        /// Key to be used with <see cref="Puppeteer.NetworkConditions()"/>
15        /// </summary>
16        public const string Fast3G = "Fast 3G";
17
18        /// <summary>
19        /// Download speed (bytes/s), `-1` to disable
20        /// </summary>
21        public double Download { get; set; } = -1;
22
23        /// <summary>
24        /// Upload speed (bytes/s), `-1` to disable
25        /// </summary>
26        public double Upload { get; set; } = -1;
27
28        /// <summary>
29        /// Latency (ms), `0` to disable
30        /// </summary>
31        public double Latency { get; set; } = 0;
32    }
33}
34
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 methods in NetworkConditions

    No methods found for this class 😞

Run Selenium Automation Tests on LambdaTest Cloud Grid

Trigger Selenium automation tests on a cloud-based Grid of 3000+ real browsers and operating systems.

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)