Skip to content

Commit f3a5180

Browse files
authored
Add missing Viewport tests (#2614)
1 parent 4f344a4 commit f3a5180

File tree

6 files changed

+66
-4
lines changed

6 files changed

+66
-4
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<!DOCTYPE html>
2+
<img
3+
srcset="logo-1x.png, logo-2x.png 2x, logo-3x.png 3x"
4+
src="logo-1x.png"
5+
height="320"
6+
width="320" />
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<!DOCTYPE html>
2+
<style>
3+
p {
4+
color: transparent;
5+
}
6+
@media (resolution: 1dppx) {
7+
p {
8+
font-size: 1px;
9+
}
10+
}
11+
@media (resolution: 2dppx) {
12+
p {
13+
font-size: 2px;
14+
}
15+
}
16+
@media (resolution: 3dppx) {
17+
p {
18+
font-size: 3px;
19+
}
20+
}
21+
</style>
22+
<p>Test</p>
23+

lib/PuppeteerSharp.Tests/EmulationTests/PageViewPortTests.cs

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,6 @@ namespace PuppeteerSharp.Tests.EmulationTests
66
{
77
public class PageViewPortTests : PuppeteerPageBaseTest
88
{
9-
public PageViewPortTests() : base()
10-
{
11-
}
12-
139
[Test, Retry(2), PuppeteerTest("emulation.spec", "Emulation Page.viewport", "should get the proper viewport size")]
1410
public async Task ShouldGetTheProperViewPortSize()
1511
{
@@ -99,5 +95,42 @@ public async Task ShouldSupportLandscapeEmulation()
9995
await Page.SetViewportAsync(new ViewPortOptions { Width = 100, Height = 100 });
10096
Assert.AreEqual("portrait-primary", await Page.EvaluateExpressionAsync<string>("screen.orientation.type"));
10197
}
98+
99+
[Test, Retry(2), PuppeteerTest("emulation.spec", "Emulation Page.viewport", "should update media queries when resoltion changes")]
100+
public async Task ShouldUpdateMediaQueriesWhenResolutionChanges()
101+
{
102+
foreach (var dpr in new[] { 1, 2, 3 })
103+
{
104+
await Page.SetViewportAsync(new ViewPortOptions { Width = 800, Height = 600, DeviceScaleFactor = dpr });
105+
106+
await Page.GoToAsync(TestConstants.ServerUrl + "/resolution.html");
107+
Assert.AreEqual(dpr, await GetFontSizeAsync());
108+
var screenshot = await Page.ScreenshotDataAsync(new ScreenshotOptions() { FullPage = false });
109+
Assert.True(ScreenshotHelper.PixelMatch($"device-pixel-ratio{dpr}.png", screenshot));
110+
}
111+
}
112+
113+
[Test, Retry(2), PuppeteerTest("emulation.spec", "Emulation Page.viewport", "should load correct pictures when emulation dpr")]
114+
public async Task ShouldLoadCorrectPicturesWhenEmulationDpr()
115+
{
116+
foreach (var dpr in new[] { 1, 2, 3 })
117+
{
118+
await Page.SetViewportAsync(new ViewPortOptions { Width = 800, Height = 600, DeviceScaleFactor = dpr });
119+
120+
await Page.GoToAsync(TestConstants.ServerUrl + "/picture.html");
121+
StringAssert.EndsWith($"logo-{dpr}x.png", await GetCurrentSrc());
122+
123+
}
124+
}
125+
126+
private Task<int> GetFontSizeAsync()
127+
=> Page.EvaluateFunctionAsync<int>(@"() => {
128+
return parseInt(window.getComputedStyle(document.querySelector('p')).fontSize, 10);
129+
}");
130+
131+
private Task<string> GetCurrentSrc()
132+
=> Page.EvaluateFunctionAsync<string>(@"() => {
133+
return document.querySelector('img').currentSrc;
134+
}");
102135
}
103136
}
3.17 KB
Loading
10 KB
Loading
20.5 KB
Loading

0 commit comments

Comments
 (0)