-
Notifications
You must be signed in to change notification settings - Fork 0
/
screenshot-windowed.tc.ts
56 lines (46 loc) · 2.11 KB
/
screenshot-windowed.tc.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import { fixture, test } from "testcafe";
import { readFileSync } from "fs";
import { join as joinPath } from "path";
import { PNG } from "pngjs";
import {
Dimensions,
getWindowDevicePixelRatio,
getWindowInnerDimensions,
landscapeDimensions,
mkValidPath,
Orientation,
portraitDimensions,
resizeTo,
} from "./tools";
for (const withScollbars of [true, false]) {
fixture(`windowed screenshot ${withScollbars ? "withScollbars" : "withoutScollbars"}`)
.meta({viewPort: "variable"})
.page("about:blank")
.clientScripts([
{content: `document.addEventListener("DOMContentLoaded", () => document.body.style.background = "#ccc");`},
{content: `document.addEventListener("DOMContentLoaded", () => document.body.style.border = "50px solid #bbb");`},
{content: withScollbars ? `document.addEventListener("DOMContentLoaded", () => document.body.style.height = "1000px");` : "{}"},
]);
const orientations: Orientation[] = ["PORTRAIT", "LANDSCAPE"];
for (const orientation of orientations) {
test(`takeScreenshot() - ${orientation}`, async t => {
const isLandscape = orientation === "LANDSCAPE";
const targetDimensions = isLandscape ? landscapeDimensions : portraitDimensions;
await resizeTo(t, targetDimensions);
await t.expect(getWindowInnerDimensions()).eql(targetDimensions, "Did not change to the expected dimensions!");
const screenshotPath = `${mkValidPath(t.browser.alias)}--${mkValidPath(t.fixture.name)}--${mkValidPath(t.test.name)}`;
await t.takeScreenshot(screenshotPath);
// adjust for retina displays
const devicePixelRatio = await getWindowDevicePixelRatio();
const adjustedDimensions: Dimensions = {
width: targetDimensions.width * devicePixelRatio,
height: targetDimensions.height * devicePixelRatio,
};
// adjust for cropped pixel
adjustedDimensions.height--;
const png = PNG.sync.read(readFileSync(joinPath("screenshots", `${screenshotPath}.png`)));
const pngDimensions: Dimensions = {width: png.width, height: png.height};
await t.expect(pngDimensions).eql(adjustedDimensions, "Screenshot does not have the same dimensions as the window!");
});
}
}