forked from chromedp/chromedp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
screenshot_test.go
216 lines (190 loc) · 4.28 KB
/
screenshot_test.go
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package chromedp
import (
"bytes"
"fmt"
"image"
_ "image/jpeg"
_ "image/png"
"os"
"path"
"testing"
"github.com/orisano/pixelmatch"
)
func TestScreenshot(t *testing.T) {
t.Parallel()
tests := []struct {
name string
sel string
want string
}{
{
name: "padding border",
sel: "#padding-border",
want: "element-padding-border.png",
},
{
name: "larger than viewport",
sel: "#larger-than-viewport",
want: "element-larger-than-viewport.png",
},
{
name: "outside viewport",
sel: "#outside-viewport",
want: "element-scrolled-into-view.png",
},
{
name: "rotate element",
sel: "#rotated",
want: "element-rotate.png",
},
{
name: "fractional dimensions",
sel: "#fractional-dimensions",
want: "element-fractional.png",
},
{
name: "fractional offset",
sel: "#fractional-offset",
want: "element-fractional-offset.png",
},
}
for _, test := range tests {
test := test
t.Run(test.name, func(t *testing.T) {
t.Parallel()
ctx, cancel := testAllocate(t, "screenshot.html")
defer cancel()
var buf []byte
if err := Run(ctx,
EmulateViewport(500, 500),
EvaluateAsDevTools("document.documentElement.scrollTo(20, 30)", nil),
Screenshot(test.sel, &buf, ByQuery),
); err != nil {
t.Fatal(err)
}
diff, err := matchPixel(buf, test.want)
if err != nil {
t.Fatal(err)
}
if diff != 0 {
t.Fatalf("screenshot does not match. diff: %v", diff)
}
})
}
}
func TestScreenshotHighDPI(t *testing.T) {
t.Parallel()
ctx, cancel := testAllocate(t, "image.html")
defer cancel()
// Use a weird screen dimension with a 1.5 scale factor, so that
// cropping the screenshot is forced to use floating point arithmetic
// and keep the high DPI in mind.
// We also want the dimensions to be large enough to see the element we
// want, since we're not scrolling to ensure it's in view.
if err := Run(ctx, EmulateViewport(905, 705, EmulateScale(1.5))); err != nil {
t.Fatal(err)
}
var buf []byte
if err := Run(ctx, Screenshot("#half-color", &buf, ByID)); err != nil {
t.Fatal(err)
}
diff, err := matchPixel(buf, "half-color.png")
if err != nil {
t.Fatal(err)
}
if diff != 0 {
t.Fatalf("screenshot does not match. diff: %v", diff)
}
}
func TestCaptureScreenshot(t *testing.T) {
t.Parallel()
ctx, cancel := testAllocate(t, "grid.html")
defer cancel()
var buf []byte
if err := Run(ctx,
EmulateViewport(500, 500),
CaptureScreenshot(&buf),
); err != nil {
t.Fatal(err)
}
diff, err := matchPixel(buf, "sanity.png")
if err != nil {
t.Fatal(err)
}
if diff != 0 {
t.Fatalf("screenshot does not match. diff: %v", diff)
}
}
func TestFullScreenshot(t *testing.T) {
t.Parallel()
tests := []struct {
name string
quality int
want string
}{
{
name: "quality 100",
quality: 100,
want: "grid-fullpage.png",
},
{
name: "quality 90",
quality: 90,
want: "grid-fullpage-90.jpeg",
},
}
for _, test := range tests {
test := test
t.Run(test.name, func(t *testing.T) {
t.Parallel()
ctx, cancel := testAllocate(t, "grid.html")
defer cancel()
var buf []byte
if err := Run(ctx,
EmulateViewport(500, 500),
EvaluateAsDevTools("document.documentElement.scrollTo(20, 30)", nil),
FullScreenshot(&buf, test.quality),
); err != nil {
t.Fatal(err)
}
diff, err := matchPixel(buf, test.want)
if err != nil {
t.Fatal(err)
}
if diff != 0 {
t.Fatalf("screenshot does not match. diff: %v", diff)
}
})
}
}
func matchPixel(buf []byte, want string) (int, error) {
img1, format1, err := image.Decode(bytes.NewReader(buf))
if err != nil {
return 0, err
}
img2, format2, err := openImage(want)
if err != nil {
return 0, err
}
if format1 != format2 {
return 0, fmt.Errorf("image formats not matched: %s != %s", format1, format2)
}
return pixelmatch.MatchPixel(img1, img2, pixelmatch.Threshold(0.1))
}
func openImage(screenshot string) (image.Image, string, error) {
wd, err := os.Getwd()
if err != nil {
return nil, "", err
}
p := path.Join(wd, "testdata", "screenshots", screenshot)
f, err := os.Open(p)
if err != nil {
return nil, "", err
}
defer f.Close()
img, format, err := image.Decode(f)
if err != nil {
return nil, "", fmt.Errorf("decode image: %w", err)
}
return img, format, nil
}