Skip to content

Commit

Permalink
Introduce Page.EmulateVisionDeficiencyAsync and Page.EmulateCPUThrott…
Browse files Browse the repository at this point in the history
…lingAsync (hardkoded#1839)

closes hardkoded#1764
  • Loading branch information
kblok authored Oct 5, 2021
1 parent e5230b4 commit f388b5e
Show file tree
Hide file tree
Showing 10 changed files with 178 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Threading.Tasks;
using PuppeteerSharp.Media;
using PuppeteerSharp.Tests.Attributes;
using PuppeteerSharp.Xunit;
using Xunit;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System.Threading.Tasks;
using PuppeteerSharp.Tests.Attributes;
using PuppeteerSharp.Xunit;
using Xunit;
using Xunit.Abstractions;

namespace PuppeteerSharp.Tests.EmulationTests
{
[Collection(TestConstants.TestFixtureCollectionName)]
public class PageEmulateCPUThrottlingTests : PuppeteerPageBaseTest
{
public PageEmulateCPUThrottlingTests(ITestOutputHelper output) : base(output)
{
}

[PuppeteerTest("emulation.spec.ts", "Page.emulateCPUThrottling", "should change the CPU throttling rate successfully")]
[SkipBrowserFact(skipFirefox: true)]
public async Task ShouldChangeTheCPUThrottlingRateSuccessfully()
{
await Page.EmulateCPUThrottlingAsync(100);
await Page.EmulateCPUThrottlingAsync();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
using System.Net;
using System.Threading.Tasks;
using PuppeteerSharp.Tests.Attributes;
using PuppeteerSharp.Xunit;
using Xunit;
using Xunit.Abstractions;

namespace PuppeteerSharp.Tests.PageTests
namespace PuppeteerSharp.Tests.EmulationTests
{
[Collection(TestConstants.TestFixtureCollectionName)]
public class EmulateNetworkConditionsTests : PuppeteerPageBaseTest
public class PageEmulateNetworkConditionsTests : PuppeteerPageBaseTest
{
public EmulateNetworkConditionsTests(ITestOutputHelper output) : base(output)
public PageEmulateNetworkConditionsTests(ITestOutputHelper output) : base(output)
{
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using System.Threading.Tasks;
using PuppeteerSharp.Tests.Attributes;
using PuppeteerSharp.Xunit;
using Xunit;
using Xunit.Abstractions;

namespace PuppeteerSharp.Tests.EmulationTests
{
[Collection(TestConstants.TestFixtureCollectionName)]
public class PageEmulateVisionDeficiencyTests : PuppeteerPageBaseTest
{
public PageEmulateVisionDeficiencyTests(ITestOutputHelper output) : base(output)
{
}

[PuppeteerTest("emulation.spec.ts", "Page.emulateVisionDeficiency", "should work")]
[SkipBrowserFact(skipFirefox: true)]
public async Task ShouldWork()
{
await Page.SetViewportAsync(new ViewPortOptions { Width = 500, Height = 500 });
await Page.GoToAsync(TestConstants.ServerUrl + "/grid.html");

await Page.EmulateVisionDeficiencyAsync(VisionDeficiency.None);
var screenshot = await Page.ScreenshotDataAsync();
Assert.True(ScreenshotHelper.PixelMatch("screenshot-sanity.png", screenshot));

await Page.EmulateVisionDeficiencyAsync(VisionDeficiency.Achromatopsia);
screenshot = await Page.ScreenshotDataAsync();
Assert.True(ScreenshotHelper.PixelMatch("vision-deficiency-achromatopsia.png", screenshot));

await Page.EmulateVisionDeficiencyAsync(VisionDeficiency.BlurredVision);
screenshot = await Page.ScreenshotDataAsync();
Assert.True(ScreenshotHelper.PixelMatch("vision-deficiency-blurredVision.png", screenshot));

await Page.EmulateVisionDeficiencyAsync(VisionDeficiency.Deuteranopia);
screenshot = await Page.ScreenshotDataAsync();
Assert.True(ScreenshotHelper.PixelMatch("vision-deficiency-deuteranopia.png", screenshot));

await Page.EmulateVisionDeficiencyAsync(VisionDeficiency.Protanopia);
screenshot = await Page.ScreenshotDataAsync();
Assert.True(ScreenshotHelper.PixelMatch("vision-deficiency-protanopia.png", screenshot));

await Page.EmulateVisionDeficiencyAsync(VisionDeficiency.Tritanopia);
screenshot = await Page.ScreenshotDataAsync();
Assert.True(ScreenshotHelper.PixelMatch("vision-deficiency-tritanopia.png", screenshot));

await Page.EmulateVisionDeficiencyAsync(VisionDeficiency.None);
screenshot = await Page.ScreenshotDataAsync();
Assert.True(ScreenshotHelper.PixelMatch("screenshot-sanity.png", screenshot));
}
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions lib/PuppeteerSharp.Tests/WontImplementTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ public WontImplementTests(ITestOutputHelper output) : base(output)
[PuppeteerTest("EventEmitter.spec.ts", "removeAllListeners", "removes every listener from all events by default")]
[PuppeteerTest("EventEmitter.spec.ts", "removeAllListeners", "returns the emitter for chaining")]
[PuppeteerTest("EventEmitter.spec.ts", "removeAllListeners", "can filter to remove only listeners for a given event name")]
[PuppeteerTest("emulation.spec.ts", "Page.emulateMediaType", "should throw in case of bad argument")]
[PuppeteerTest("emulation.spec.ts", "Page.emulateMediaFeatures", "should throw in case of bad argument")]
[PuppeteerTest("emulation.spec.ts", "Page.emulateVisionDeficiency", "should throw for invalid vision deficiencies")]
[PuppeteerFact]
public void TheseTesstWontBeImplemented()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace PuppeteerSharp.Messaging
{
internal class EmulationSetCPUThrottlingRateRequest
{
public decimal Rate{ get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace PuppeteerSharp.Messaging
{
internal class EmulationSetEmulatedVisionDeficiencyRequest
{
public VisionDeficiency Type { get; set; }
}
}
33 changes: 33 additions & 0 deletions lib/PuppeteerSharp/Page.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1895,6 +1895,21 @@ public Task SetBurstModeOffAsync()
/// <returns>A task that resolves when the message has been sent to Chromium.</returns>
public Task BringToFrontAsync() => Client.SendAsync("Page.bringToFront");

/// <summary>
/// Simulates the given vision deficiency on the page.
/// </summary>
/// <example>
/// await Page.EmulateVisionDeficiencyAsync(VisionDeficiency.Achromatopsia);
/// await Page.ScreenshotAsync("Achromatopsia.png");
/// </example>
/// <param name="type">The type of deficiency to simulate, or <see cref="VisionDeficiency.None"/> to reset.</param>
/// <returns>A task that resolves when the message has been sent to the browser.</returns>
public Task EmulateVisionDeficiencyAsync(VisionDeficiency type)
=> Client.SendAsync("Emulation.setEmulatedVisionDeficiency", new EmulationSetEmulatedVisionDeficiencyRequest
{
Type = type,
});

/// <summary>
/// Changes the timezone of the page.
/// </summary>
Expand All @@ -1916,6 +1931,24 @@ public async Task EmulateTimezoneAsync(string timezoneId)
}
}

/// <summary>
/// Enables CPU throttling to emulate slow CPUs.
/// </summary>
/// <param name="factor">Throttling rate as a slowdown factor (1 is no throttle, 2 is 2x slowdown, etc).</param>
/// <returns>A task that resolves when the message has been sent to the browser.</returns>
internal Task EmulateCPUThrottlingAsync(decimal? factor = null)
{
if (factor != null && factor < 1)
{
throw new ArgumentException("Throttling rate should be greater or equal to 1", nameof(factor));
}

return Client.SendAsync("Emulation.setCPUThrottlingRate", new EmulationSetCPUThrottlingRateRequest
{
Rate = factor ?? 1
});
}

internal void OnPopup(Page popupPage) => Popup?.Invoke(this, new PopupEventArgs { PopupPage = popupPage });

internal static async Task<Page> CreateAsync(
Expand Down
49 changes: 49 additions & 0 deletions lib/PuppeteerSharp/VisionDeficiency.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using System.Runtime.Serialization;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;

namespace PuppeteerSharp
{
/// <summary>
/// Types of vision deficiency to emulate using <see cref="Page.EmulateVisionDeficiencyAsync(VisionDeficiency)"/>
/// </summary>
[JsonConverter(typeof(StringEnumConverter))]
public enum VisionDeficiency
{
/// <summary>
/// None
/// </summary>
[EnumMember(Value = "none")]
None,

/// <summary>
/// Achromatopsia
/// </summary>
[EnumMember(Value = "achromatopsia")]
Achromatopsia,

/// <summary>
/// BlurredVision
/// </summary>
[EnumMember(Value = "blurredVision")]
BlurredVision,

/// <summary>
/// Deuteranopia
/// </summary>
[EnumMember(Value = "deuteranopia")]
Deuteranopia,

/// <summary>
/// Protanopia
/// </summary>
[EnumMember(Value = "protanopia")]
Protanopia,

/// <summary>
/// Tritanopia
/// </summary>
[EnumMember(Value = "tritanopia")]
Tritanopia,
}
}

0 comments on commit f388b5e

Please sign in to comment.