Skip to content

Commit

Permalink
Add Slow 4G / Fast 4G predefined network conditions (#2787)
Browse files Browse the repository at this point in the history
* Add Slow 4G / Fast 4G predefined network conditions

* Use Slow 4G / Fast 4G in test
  • Loading branch information
campersau authored Sep 30, 2024
1 parent e14f59d commit cbff8a1
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,16 @@ public PageEmulateNetworkConditionsTests() : base()
[Test, Retry(2), PuppeteerTest("emulation.spec", "Emulation Page.emulateNetworkConditions", "should change navigator.connection.effectiveType")]
public async Task ShouldChangeNavigatorConnectionEffectiveType()
{
var slow3G = Puppeteer.NetworkConditions[NetworkConditions.Slow3G];
var fast4G = Puppeteer.NetworkConditions[NetworkConditions.Fast4G];
var slow4G = Puppeteer.NetworkConditions[NetworkConditions.Slow4G];
var fast3G = Puppeteer.NetworkConditions[NetworkConditions.Fast3G];
var slow3G = Puppeteer.NetworkConditions[NetworkConditions.Slow3G];

Assert.That(await Page.EvaluateExpressionAsync<string>("window.navigator.connection.effectiveType").ConfigureAwait(false), Is.EqualTo("4g"));
await Page.EmulateNetworkConditionsAsync(fast4G);
Assert.That(await Page.EvaluateExpressionAsync<string>("window.navigator.connection.effectiveType").ConfigureAwait(false), Is.EqualTo("4g"));
await Page.EmulateNetworkConditionsAsync(slow4G);
Assert.That(await Page.EvaluateExpressionAsync<string>("window.navigator.connection.effectiveType").ConfigureAwait(false), Is.EqualTo("3g"));
await Page.EmulateNetworkConditionsAsync(fast3G);
Assert.That(await Page.EvaluateExpressionAsync<string>("window.navigator.connection.effectiveType").ConfigureAwait(false), Is.EqualTo("3g"));
await Page.EmulateNetworkConditionsAsync(slow3G);
Expand Down
10 changes: 10 additions & 0 deletions lib/PuppeteerSharp/NetworkConditions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,16 @@ public class NetworkConditions
/// </summary>
public const string Fast3G = "Fast 3G";

/// <summary>
/// Key to be used with <see cref="Puppeteer.NetworkConditions()"/>.
/// </summary>
public const string Slow4G = "Slow 4G";

/// <summary>
/// Key to be used with <see cref="Puppeteer.NetworkConditions()"/>.
/// </summary>
public const string Fast4G = "Fast 4G";

/// <summary>
/// Download speed (bytes/s), `-1` to disable.
/// </summary>
Expand Down
35 changes: 35 additions & 0 deletions lib/PuppeteerSharp/PredefinedNetworkConditions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,51 @@ public static class PredefinedNetworkConditions
{
[NetworkConditions.Slow3G] = new NetworkConditions
{
// ~500Kbps down
Download = ((500 * 1000) / 8) * 0.8,

// ~500Kbps up
Upload = ((500 * 1000) / 8) * 0.8,

// 400ms RTT
Latency = 400 * 5,
},
[NetworkConditions.Fast3G] = new NetworkConditions
{
// ~1.6 Mbps down
Download = ((1.6 * 1000 * 1000) / 8) * 0.9,

// ~0.75 Mbps up
Upload = ((750 * 1000) / 8) * 0.9,

// 150ms RTT
Latency = 150 * 3.75,
},

// alias to Fast 3G to align with Lighthouse (crbug.com/342406608)
// and DevTools (crbug.com/342406608),
[NetworkConditions.Slow4G] = new NetworkConditions
{
// ~1.6 Mbps down
Download = ((1.6 * 1000 * 1000) / 8) * 0.9,

// ~0.75 Mbps up
Upload = ((750 * 1000) / 8) * 0.9,

// 150ms RTT
Latency = 150 * 3.75,
},
[NetworkConditions.Fast4G] = new NetworkConditions
{
// 9 Mbps down
Download = ((9 * 1000 * 1000) / 8) * 0.9,

// 1.5 Mbps up
Upload = ((1.5 * 1000 * 1000) / 8) * 0.9,

// 60ms RTT
Latency = 60 * 2.75,
},
};

private static readonly Lazy<IReadOnlyDictionary<string, NetworkConditions>> _readOnlyConditions =
Expand Down

0 comments on commit cbff8a1

Please sign in to comment.