forked from hardkoded/puppeteer-sharp
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add EmulateMediaTypeAsync and EmulateMediaFeaturesAsync (hardkoded#1324)
- Loading branch information
Showing
7 changed files
with
185 additions
and
14 deletions.
There are no files selected for viewing
46 changes: 46 additions & 0 deletions
46
lib/PuppeteerSharp.Tests/PageTests/EmulateMediaFeaturesTests.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,46 @@ | ||
using System.Threading.Tasks; | ||
using PuppeteerSharp.Media; | ||
using Xunit; | ||
using Xunit.Abstractions; | ||
|
||
namespace PuppeteerSharp.Tests.PageTests | ||
{ | ||
[Collection(TestConstants.TestFixtureCollectionName)] | ||
public class EmulateMediaFeaturesAsyncTests : PuppeteerPageBaseTest | ||
{ | ||
public EmulateMediaFeaturesAsyncTests(ITestOutputHelper output) : base(output) | ||
{ | ||
} | ||
|
||
[Fact] | ||
public async Task ShouldWork() | ||
{ | ||
await Page.EmulateMediaFeaturesAsync(new MediaFeatureValue[] { | ||
new MediaFeatureValue { MediaFeature = MediaFeature.PrefersReducedMotion, Value = "reduce" }, | ||
}); | ||
Assert.True(await Page.EvaluateFunctionAsync<bool>("() => matchMedia('(prefers-reduced-motion: reduce)').matches")); | ||
Assert.False(await Page.EvaluateFunctionAsync<bool>("() => matchMedia('(prefers-reduced-motion: no-preference)').matches")); | ||
await Page.EmulateMediaFeaturesAsync(new MediaFeatureValue[] { | ||
new MediaFeatureValue { MediaFeature = MediaFeature.PrefersColorScheme, Value = "light" }, | ||
}); | ||
Assert.True(await Page.EvaluateFunctionAsync<bool>("() => matchMedia('(prefers-color-scheme: light)').matches")); | ||
Assert.False(await Page.EvaluateFunctionAsync<bool>("() => matchMedia('(prefers-color-scheme: dark)').matches")); | ||
Assert.False(await Page.EvaluateFunctionAsync<bool>("() => matchMedia('(prefers-color-scheme: no-preference)').matches")); | ||
await Page.EmulateMediaFeaturesAsync(new MediaFeatureValue[] { | ||
new MediaFeatureValue { MediaFeature = MediaFeature.PrefersColorScheme, Value = "dark" }, | ||
}); | ||
Assert.True(await Page.EvaluateFunctionAsync<bool>("() => matchMedia('(prefers-color-scheme: dark)').matches")); | ||
Assert.False(await Page.EvaluateFunctionAsync<bool>("() => matchMedia('(prefers-color-scheme: light)').matches")); | ||
Assert.False(await Page.EvaluateFunctionAsync<bool>("() => matchMedia('(prefers-color-scheme: no-preference)').matches")); | ||
await Page.EmulateMediaFeaturesAsync(new MediaFeatureValue[] { | ||
new MediaFeatureValue { MediaFeature = MediaFeature.PrefersReducedMotion, Value = "reduce" }, | ||
new MediaFeatureValue { MediaFeature = MediaFeature.PrefersColorScheme, Value = "light" }, | ||
}); | ||
Assert.True(await Page.EvaluateFunctionAsync<bool>("() => matchMedia('(prefers-reduced-motion: reduce)').matches")); | ||
Assert.False(await Page.EvaluateFunctionAsync<bool>("() => matchMedia('(prefers-reduced-motion: no-preference)').matches")); | ||
Assert.True(await Page.EvaluateFunctionAsync<bool>("() => matchMedia('(prefers-color-scheme: light)').matches")); | ||
Assert.False(await Page.EvaluateFunctionAsync<bool>("() => matchMedia('(prefers-color-scheme: dark)').matches")); | ||
Assert.False(await Page.EvaluateFunctionAsync<bool>("() => matchMedia('(prefers-color-scheme: no-preference)').matches")); | ||
} | ||
} | ||
} |
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,24 @@ | ||
using System.Runtime.Serialization; | ||
using Newtonsoft.Json; | ||
using Newtonsoft.Json.Converters; | ||
|
||
namespace PuppeteerSharp | ||
{ | ||
/// <summary> | ||
/// Meadia Feature. See <see cref="Page.EmulateMediaFeaturesAsync(System.Collections.Generic.IEnumerable{MediaFeatureValue})"/> | ||
/// </summary> | ||
[JsonConverter(typeof(StringEnumConverter))] | ||
public enum MediaFeature | ||
{ | ||
/// <summary> | ||
/// prefers-color-scheme media feature. | ||
/// </summary> | ||
[EnumMember(Value = "prefers-color-scheme")] | ||
PrefersColorScheme, | ||
/// <summary> | ||
/// prefers-reduced-motion media feature. | ||
/// </summary> | ||
[EnumMember(Value = "prefers-reduced-motion")] | ||
PrefersReducedMotion | ||
} | ||
} |
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,20 @@ | ||
using Newtonsoft.Json; | ||
|
||
namespace PuppeteerSharp | ||
{ | ||
/// <summary> | ||
/// Media Feature. <see cref="Page.EmulateMediaFeaturesAsync(System.Collections.Generic.IEnumerable{MediaFeatureValue})"/> | ||
/// </summary> | ||
public class MediaFeatureValue | ||
{ | ||
/// <summary> | ||
/// The CSS media feature name. Supported names are `'prefers-colors-scheme'` and `'prefers-reduced-motion'`. | ||
/// </summary> | ||
[JsonProperty(PropertyName = "name")] | ||
public MediaFeature MediaFeature { get; set; } | ||
/// <summary> | ||
/// The value for the given CSS media feature. | ||
/// </summary> | ||
public string Value { get; set; } | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
lib/PuppeteerSharp/Messaging/EmulationSetEmulatedMediaFeatureRequest.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,11 @@ | ||
using System.Collections.Generic; | ||
using Newtonsoft.Json; | ||
using PuppeteerSharp.Media; | ||
|
||
namespace PuppeteerSharp.Messaging | ||
{ | ||
internal class EmulationSetEmulatedMediaFeatureRequest | ||
{ | ||
public IEnumerable<MediaFeatureValue> Features { get; set; } | ||
} | ||
} |
5 changes: 3 additions & 2 deletions
5
lib/PuppeteerSharp/Messaging/EmulationSetEmulatedMediaRequest.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
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