Skip to content

Commit

Permalink
screenshot: add unit test for FullScreenshot
Browse files Browse the repository at this point in the history
This unit test is copied from puppeteer:
- testdata/grid.html
- testdata/digits/[0-9].png
- testdata/screenshots/grid-fullpage.png
  • Loading branch information
ZekeLu committed Jun 17, 2021
1 parent 271a472 commit e5f5a89
Show file tree
Hide file tree
Showing 14 changed files with 100 additions and 0 deletions.
48 changes: 48 additions & 0 deletions screenshot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"fmt"
"image"
_ "image/jpeg"
"image/png"
"os"
"path"
Expand Down Expand Up @@ -114,6 +115,53 @@ func TestCaptureScreenshot(t *testing.T) {
}
}

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 {
Expand Down
Binary file added testdata/digits/0.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added testdata/digits/1.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added testdata/digits/2.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added testdata/digits/3.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added testdata/digits/4.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added testdata/digits/5.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added testdata/digits/6.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added testdata/digits/7.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added testdata/digits/8.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added testdata/digits/9.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
52 changes: 52 additions & 0 deletions testdata/grid.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<script>
document.addEventListener('DOMContentLoaded', function() {
function generatePalette(amount) {
var result = [];
var hueStep = 360 / amount;
for (var i = 0; i < amount; ++i)
result.push('hsl(' + (hueStep * i) + ', 100%, 90%)');
return result;
}

var palette = generatePalette(100);
for (var i = 0; i < 200; ++i) {
var box = document.createElement('div');
box.classList.add('box');
box.style.setProperty('background-color', palette[i % palette.length]);
var x = i;
do {
var digit = x % 10;
x = (x / 10)|0;
var img = document.createElement('img');
img.src = `./digits/${digit}.png`;
box.insertBefore(img, box.firstChild);
} while (x);
document.body.appendChild(box);
}
});
</script>

<style>

body {
margin: 0;
padding: 0;
}

.box {
font-family: arial;
display: inline-flex;
align-items: center;
justify-content: center;
margin: 0;
padding: 0;
width: 50px;
height: 50px;
box-sizing: border-box;
border: 1px solid darkgray;
}

::-webkit-scrollbar {
display: none;
}
</style>
Binary file added testdata/screenshots/grid-fullpage-90.jpeg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added testdata/screenshots/grid-fullpage.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit e5f5a89

Please sign in to comment.