-
-
Notifications
You must be signed in to change notification settings - Fork 458
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce Page.EmulateNetworkConditionsAsync (#1647)
* feat: add page.emulateNetworkConditions * Add Async suffix * Add NetworkConditions defaults * Drop PredefinedNetworkConditionsName, merge values into NetworkConditions
- Loading branch information
Showing
8 changed files
with
158 additions
and
15 deletions.
There are no files selected for viewing
29 changes: 29 additions & 0 deletions
29
lib/PuppeteerSharp.Tests/PageTests/EmulateNetworkConditions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
using System.Net; | ||
using System.Threading.Tasks; | ||
using Xunit; | ||
using Xunit.Abstractions; | ||
|
||
namespace PuppeteerSharp.Tests.PageTests | ||
{ | ||
[Collection(TestConstants.TestFixtureCollectionName)] | ||
public class EmulateNetworkConditionsTests : PuppeteerPageBaseTest | ||
{ | ||
public EmulateNetworkConditionsTests(ITestOutputHelper output) : base(output) | ||
{ | ||
} | ||
|
||
[Fact] | ||
public async Task ShouldChangeNavigatorConnectionEffectiveType() | ||
{ | ||
var slow3G = Puppeteer.NetworkConditions[NetworkConditions.Slow3G]; | ||
var fast3G = Puppeteer.NetworkConditions[NetworkConditions.Fast3G]; | ||
|
||
Assert.Equal("4g", await Page.EvaluateExpressionAsync<string>("window.navigator.connection.effectiveType").ConfigureAwait(false)); | ||
await Page.EmulateNetworkConditionsAsync(fast3G); | ||
Assert.Equal("3g", await Page.EvaluateExpressionAsync<string>("window.navigator.connection.effectiveType").ConfigureAwait(false)); | ||
await Page.EmulateNetworkConditionsAsync(slow3G); | ||
Assert.Equal("2g", await Page.EvaluateExpressionAsync<string>("window.navigator.connection.effectiveType").ConfigureAwait(false)); | ||
await Page.EmulateNetworkConditionsAsync(null); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
namespace PuppeteerSharp | ||
{ | ||
internal class InternalNetworkConditions : NetworkConditions | ||
{ | ||
public bool Offline { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
namespace PuppeteerSharp | ||
{ | ||
/// <summary> | ||
/// Options to be used in <see cref="Page.EmulateNetworkConditionsAsync(NetworkConditions)"/> | ||
/// </summary> | ||
public class NetworkConditions | ||
{ | ||
/// <summary> | ||
/// Key to be used with <see cref="Puppeteer.NetworkConditions()"/> | ||
/// </summary> | ||
public const string Slow3G = "Slow 3G"; | ||
|
||
/// <summary> | ||
/// Key to be used with <see cref="Puppeteer.NetworkConditions()"/> | ||
/// </summary> | ||
public const string Fast3G = "Fast 3G"; | ||
|
||
/// <summary> | ||
/// Download speed (bytes/s), `-1` to disable | ||
/// </summary> | ||
public double Download { get; set; } = -1; | ||
|
||
/// <summary> | ||
/// Upload speed (bytes/s), `-1` to disable | ||
/// </summary> | ||
public double Upload { get; set; } = -1; | ||
|
||
/// <summary> | ||
/// Latency (ms), `0` to disable | ||
/// </summary> | ||
public double Latency { get; set; } = 0; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Collections.ObjectModel; | ||
|
||
namespace PuppeteerSharp | ||
{ | ||
/// <summary> | ||
/// Predefined network conditions. | ||
/// </summary> | ||
public static class PredefinedNetworkConditions | ||
{ | ||
private static readonly Dictionary<string, NetworkConditions> Conditions = new Dictionary<string, NetworkConditions> | ||
{ | ||
[NetworkConditions.Slow3G] = new NetworkConditions | ||
{ | ||
Download = ((500 * 1000) / 8) * 0.8, | ||
Upload = ((500 * 1000) / 8) * 0.8, | ||
Latency = 400 * 5, | ||
}, | ||
[NetworkConditions.Fast3G] = new NetworkConditions | ||
{ | ||
Download = ((1.6 * 1000 * 1000) / 8) * 0.9, | ||
Upload = ((750 * 1000) / 8) * 0.9, | ||
Latency = 150 * 3.75, | ||
}, | ||
}; | ||
|
||
private static Lazy<IReadOnlyDictionary<string, NetworkConditions>> _readOnlyConditions = | ||
new Lazy<IReadOnlyDictionary<string, NetworkConditions>>(() => new ReadOnlyDictionary<string, NetworkConditions>(Conditions)); | ||
|
||
internal static IReadOnlyDictionary<string, NetworkConditions> ToReadOnly() => _readOnlyConditions.Value; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters