Skip to content

Commit 2721e9b

Browse files
authored
Add EmulateMediaTypeAsync and EmulateMediaFeaturesAsync (#1324)
1 parent 4e6d984 commit 2721e9b

File tree

7 files changed

+185
-14
lines changed

7 files changed

+185
-14
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
using System.Threading.Tasks;
2+
using PuppeteerSharp.Media;
3+
using Xunit;
4+
using Xunit.Abstractions;
5+
6+
namespace PuppeteerSharp.Tests.PageTests
7+
{
8+
[Collection(TestConstants.TestFixtureCollectionName)]
9+
public class EmulateMediaFeaturesAsyncTests : PuppeteerPageBaseTest
10+
{
11+
public EmulateMediaFeaturesAsyncTests(ITestOutputHelper output) : base(output)
12+
{
13+
}
14+
15+
[Fact]
16+
public async Task ShouldWork()
17+
{
18+
await Page.EmulateMediaFeaturesAsync(new MediaFeatureValue[] {
19+
new MediaFeatureValue { MediaFeature = MediaFeature.PrefersReducedMotion, Value = "reduce" },
20+
});
21+
Assert.True(await Page.EvaluateFunctionAsync<bool>("() => matchMedia('(prefers-reduced-motion: reduce)').matches"));
22+
Assert.False(await Page.EvaluateFunctionAsync<bool>("() => matchMedia('(prefers-reduced-motion: no-preference)').matches"));
23+
await Page.EmulateMediaFeaturesAsync(new MediaFeatureValue[] {
24+
new MediaFeatureValue { MediaFeature = MediaFeature.PrefersColorScheme, Value = "light" },
25+
});
26+
Assert.True(await Page.EvaluateFunctionAsync<bool>("() => matchMedia('(prefers-color-scheme: light)').matches"));
27+
Assert.False(await Page.EvaluateFunctionAsync<bool>("() => matchMedia('(prefers-color-scheme: dark)').matches"));
28+
Assert.False(await Page.EvaluateFunctionAsync<bool>("() => matchMedia('(prefers-color-scheme: no-preference)').matches"));
29+
await Page.EmulateMediaFeaturesAsync(new MediaFeatureValue[] {
30+
new MediaFeatureValue { MediaFeature = MediaFeature.PrefersColorScheme, Value = "dark" },
31+
});
32+
Assert.True(await Page.EvaluateFunctionAsync<bool>("() => matchMedia('(prefers-color-scheme: dark)').matches"));
33+
Assert.False(await Page.EvaluateFunctionAsync<bool>("() => matchMedia('(prefers-color-scheme: light)').matches"));
34+
Assert.False(await Page.EvaluateFunctionAsync<bool>("() => matchMedia('(prefers-color-scheme: no-preference)').matches"));
35+
await Page.EmulateMediaFeaturesAsync(new MediaFeatureValue[] {
36+
new MediaFeatureValue { MediaFeature = MediaFeature.PrefersReducedMotion, Value = "reduce" },
37+
new MediaFeatureValue { MediaFeature = MediaFeature.PrefersColorScheme, Value = "light" },
38+
});
39+
Assert.True(await Page.EvaluateFunctionAsync<bool>("() => matchMedia('(prefers-reduced-motion: reduce)').matches"));
40+
Assert.False(await Page.EvaluateFunctionAsync<bool>("() => matchMedia('(prefers-reduced-motion: no-preference)').matches"));
41+
Assert.True(await Page.EvaluateFunctionAsync<bool>("() => matchMedia('(prefers-color-scheme: light)').matches"));
42+
Assert.False(await Page.EvaluateFunctionAsync<bool>("() => matchMedia('(prefers-color-scheme: dark)').matches"));
43+
Assert.False(await Page.EvaluateFunctionAsync<bool>("() => matchMedia('(prefers-color-scheme: no-preference)').matches"));
44+
}
45+
}
46+
}

lib/PuppeteerSharp.Tests/PageTests/EmulateMediaTests.cs renamed to lib/PuppeteerSharp.Tests/PageTests/EmulateMediaTypeTests.cs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,23 @@
66
namespace PuppeteerSharp.Tests.PageTests
77
{
88
[Collection(TestConstants.TestFixtureCollectionName)]
9-
public class EmulateMediaTests : PuppeteerPageBaseTest
9+
public class EmulateMediaTypeTests : PuppeteerPageBaseTest
1010
{
11-
public EmulateMediaTests(ITestOutputHelper output) : base(output)
11+
public EmulateMediaTypeTests(ITestOutputHelper output) : base(output)
1212
{
1313
}
1414

1515
[Fact]
1616
public async Task ShouldWork()
1717
{
18-
Assert.True(await Page.EvaluateExpressionAsync<bool>("window.matchMedia('screen').matches"));
19-
Assert.False(await Page.EvaluateExpressionAsync<bool>("window.matchMedia('print').matches"));
20-
await Page.EmulateMediaAsync(MediaType.Print);
21-
Assert.False(await Page.EvaluateExpressionAsync<bool>("window.matchMedia('screen').matches"));
22-
Assert.True(await Page.EvaluateExpressionAsync<bool>("window.matchMedia('print').matches"));
23-
await Page.EmulateMediaAsync(MediaType.None);
24-
Assert.True(await Page.EvaluateExpressionAsync<bool>("window.matchMedia('screen').matches"));
25-
Assert.False(await Page.EvaluateExpressionAsync<bool>("window.matchMedia('print').matches"));
18+
Assert.True(await Page.EvaluateExpressionAsync<bool>("matchMedia('screen').matches"));
19+
Assert.False(await Page.EvaluateExpressionAsync<bool>("matchMedia('print').matches"));
20+
await Page.EmulateMediaTypeAsync(MediaType.Print);
21+
Assert.False(await Page.EvaluateExpressionAsync<bool>("matchMedia('screen').matches"));
22+
Assert.True(await Page.EvaluateExpressionAsync<bool>("matchMedia('print').matches"));
23+
await Page.EmulateMediaTypeAsync(MediaType.None);
24+
Assert.True(await Page.EvaluateExpressionAsync<bool>("matchMedia('screen').matches"));
25+
Assert.False(await Page.EvaluateExpressionAsync<bool>("matchMedia('print').matches"));
2626
}
2727
}
2828
}

lib/PuppeteerSharp/MediaFeature.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using System.Runtime.Serialization;
2+
using Newtonsoft.Json;
3+
using Newtonsoft.Json.Converters;
4+
5+
namespace PuppeteerSharp
6+
{
7+
/// <summary>
8+
/// Meadia Feature. See <see cref="Page.EmulateMediaFeaturesAsync(System.Collections.Generic.IEnumerable{MediaFeatureValue})"/>
9+
/// </summary>
10+
[JsonConverter(typeof(StringEnumConverter))]
11+
public enum MediaFeature
12+
{
13+
/// <summary>
14+
/// prefers-color-scheme media feature.
15+
/// </summary>
16+
[EnumMember(Value = "prefers-color-scheme")]
17+
PrefersColorScheme,
18+
/// <summary>
19+
/// prefers-reduced-motion media feature.
20+
/// </summary>
21+
[EnumMember(Value = "prefers-reduced-motion")]
22+
PrefersReducedMotion
23+
}
24+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using Newtonsoft.Json;
2+
3+
namespace PuppeteerSharp
4+
{
5+
/// <summary>
6+
/// Media Feature. <see cref="Page.EmulateMediaFeaturesAsync(System.Collections.Generic.IEnumerable{MediaFeatureValue})"/>
7+
/// </summary>
8+
public class MediaFeatureValue
9+
{
10+
/// <summary>
11+
/// The CSS media feature name. Supported names are `'prefers-colors-scheme'` and `'prefers-reduced-motion'`.
12+
/// </summary>
13+
[JsonProperty(PropertyName = "name")]
14+
public MediaFeature MediaFeature { get; set; }
15+
/// <summary>
16+
/// The value for the given CSS media feature.
17+
/// </summary>
18+
public string Value { get; set; }
19+
}
20+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using System.Collections.Generic;
2+
using Newtonsoft.Json;
3+
using PuppeteerSharp.Media;
4+
5+
namespace PuppeteerSharp.Messaging
6+
{
7+
internal class EmulationSetEmulatedMediaFeatureRequest
8+
{
9+
public IEnumerable<MediaFeatureValue> Features { get; set; }
10+
}
11+
}

lib/PuppeteerSharp/Messaging/EmulationSetEmulatedMediaRequest.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
using PuppeteerSharp.Media;
1+
using System.Collections.Generic;
2+
using PuppeteerSharp.Media;
23

34
namespace PuppeteerSharp.Messaging
45
{
5-
internal class EmulationSetEmulatedMediaRequest
6+
internal class EmulationSetEmulatedMediaTypeRequest
67
{
78
public MediaType Media { get; set; }
89
}

lib/PuppeteerSharp/Page.cs

Lines changed: 71 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1009,8 +1009,77 @@ public Task SetJavaScriptEnabledAsync(bool enabled)
10091009
/// </summary>
10101010
/// <returns>Task.</returns>
10111011
/// <param name="media">Media to set.</param>
1012-
public Task EmulateMediaAsync(MediaType media)
1013-
=> Client.SendAsync("Emulation.setEmulatedMedia", new EmulationSetEmulatedMediaRequest { Media = media });
1012+
[Obsolete("User EmulateMediaTypeAsync instead")]
1013+
public Task EmulateMediaAsync(MediaType media) => EmulateMediaTypeAsync(media);
1014+
1015+
/// <summary>
1016+
/// Emulates a media such as screen or print.
1017+
/// </summary>
1018+
/// <param name="type">Media to set.</param>
1019+
/// <example>
1020+
/// <code>
1021+
/// <![CDATA[
1022+
/// await page.EvaluateFunctionAsync<bool>("() => matchMedia('screen').matches)");
1023+
/// // → true
1024+
/// await page.EvaluateFunctionAsync<bool>("() => matchMedia('print').matches)");
1025+
/// // → true
1026+
/// await page.EmulateMediaTypeAsync(MediaType.Print);
1027+
/// await page.EvaluateFunctionAsync<bool>("() => matchMedia('screen').matches)");
1028+
/// // → false
1029+
/// await page.EvaluateFunctionAsync<bool>("() => matchMedia('print').matches)");
1030+
/// // → true
1031+
/// await page.EmulateMediaTypeAsync(MediaType.None);
1032+
/// await page.EvaluateFunctionAsync<bool>("() => matchMedia('screen').matches)");
1033+
/// // → true
1034+
/// await page.EvaluateFunctionAsync<bool>("() => matchMedia('print').matches)");
1035+
/// // → true
1036+
/// ]]>
1037+
/// </code>
1038+
/// </example>
1039+
/// <returns>Emulate media type task.</returns>
1040+
public Task EmulateMediaTypeAsync(MediaType type)
1041+
=> Client.SendAsync("Emulation.setEmulatedMedia", new EmulationSetEmulatedMediaTypeRequest { Media = type });
1042+
1043+
/// <summary>
1044+
/// Given an array of media feature objects, emulates CSS media features on the page.
1045+
/// </summary>
1046+
/// <param name="features">Features to apply</param>
1047+
/// <example>
1048+
/// <code>
1049+
/// <![CDATA[
1050+
/// await page.EmulateMediaFeaturesAsync(new MediaFeature[]{ new MediaFeature { MediaFeature = MediaFeature.PrefersColorScheme, Value = "dark" }});
1051+
/// await page.EvaluateFunctionAsync<bool>("() => matchMedia('(prefers-color-scheme: dark)').matches)");
1052+
/// // → true
1053+
/// await page.EvaluateFunctionAsync<bool>("() => matchMedia('(prefers-color-scheme: light)').matches)");
1054+
/// // → false
1055+
/// await page.EvaluateFunctionAsync<bool>("() => matchMedia('(prefers-color-scheme: no-preference)').matches)");
1056+
/// // → false
1057+
/// await page.EmulateMediaFeaturesAsync(new MediaFeature[]{ new MediaFeature { MediaFeature = MediaFeature.PrefersReducedMotion, Value = "reduce" }});
1058+
/// await page.EvaluateFunctionAsync<bool>("() => matchMedia('(prefers-reduced-motion: reduce)').matches)");
1059+
/// // → true
1060+
/// await page.EvaluateFunctionAsync<bool>("() => matchMedia('(prefers-color-scheme: no-preference)').matches)");
1061+
/// // → false
1062+
/// await page.EmulateMediaFeaturesAsync(new MediaFeature[]
1063+
/// {
1064+
/// new MediaFeature { MediaFeature = MediaFeature.PrefersColorScheme, Value = "dark" },
1065+
/// new MediaFeature { MediaFeature = MediaFeature.PrefersReducedMotion, Value = "reduce" },
1066+
/// });
1067+
/// await page.EvaluateFunctionAsync<bool>("() => matchMedia('(prefers-color-scheme: dark)').matches)");
1068+
/// // → true
1069+
/// await page.EvaluateFunctionAsync<bool>("() => matchMedia('(prefers-color-scheme: light)').matches)");
1070+
/// // → false
1071+
/// await page.EvaluateFunctionAsync<bool>("() => matchMedia('(prefers-color-scheme: no-preference)').matches)");
1072+
/// // → false
1073+
/// await page.EvaluateFunctionAsync<bool>("() => matchMedia('(prefers-reduced-motion: reduce)').matches)");
1074+
/// // → true
1075+
/// await page.EvaluateFunctionAsync<bool>("() => matchMedia('(prefers-color-scheme: no-preference)').matches)");
1076+
/// // → false
1077+
/// ]]>
1078+
/// </code>
1079+
/// </example>
1080+
/// <returns>Emulate features task</returns>
1081+
public Task EmulateMediaFeaturesAsync(IEnumerable<MediaFeatureValue> features)
1082+
=> Client.SendAsync("Emulation.setEmulatedMedia", new EmulationSetEmulatedMediaFeatureRequest { Features = features });
10141083

10151084
/// <summary>
10161085
/// Sets the viewport.

0 commit comments

Comments
 (0)