speed up the screenshot tests, and test the images

Using a smaller viewport speeds up both tests, and lets us know what
dimensions to expect in TestCaptureScreenshot.

For TestScreenshot, we can know what dimensions to expect in advance, as
we have the images in testdata.

'go test -run Screenshot' goes from ~0.9s to ~0.5s on my machine.

Finally, don't run ExampleTitle as part of 'go test', as it's slow.
This commit is contained in:
Daniel Martí 2019-03-21 16:39:10 +00:00
parent 0e92de5e65
commit f742f327a7
3 changed files with 48 additions and 12 deletions

View File

@ -30,8 +30,8 @@ func ExampleTitle() {
cancel()
chromedp.FromContext(ctx).Allocator.Wait()
// Output:
// Issues · chromedp/chromedp · GitHub
// no expected output, to not run this test as part of 'go test'; it's
// too slow, requiring internet access.
}
func ExampleExecAllocatorOption() {

View File

@ -1,10 +1,14 @@
package chromedp
import (
"bytes"
"image"
_ "image/png"
"strings"
"testing"
"time"
"github.com/chromedp/cdproto/emulation"
"github.com/chromedp/cdproto/page"
)
@ -213,18 +217,28 @@ func TestCaptureScreenshot(t *testing.T) {
ctx, cancel := testAllocate(t, "image.html")
defer cancel()
// set the viewport size, to know what screenshot size to expect
width, height := 650, 450
var buf []byte
if err := Run(ctx, Tasks{
emulation.SetDeviceMetricsOverride(int64(width), int64(height), 1.0, false),
WaitVisible(`#icon-brankas`, ByID), // for image.html
CaptureScreenshot(&buf),
}); err != nil {
t.Fatal(err)
}
if len(buf) == 0 {
t.Fatal("failed to capture screenshot")
config, format, err := image.DecodeConfig(bytes.NewReader(buf))
if err != nil {
t.Fatal(err)
}
if want := "png"; format != want {
t.Fatalf("expected format to be %q, got %q", want, format)
}
if config.Width != width || config.Height != height {
t.Fatalf("expected dimensions to be %d*%d, got %d*%d",
width, height, config.Width, config.Height)
}
//TODO: test image
}
/*func TestAddOnLoadScript(t *testing.T) {

View File

@ -1,7 +1,10 @@
package chromedp
import (
"bytes"
"fmt"
"image"
_ "image/png"
"io/ioutil"
"net/http"
"net/http/httptest"
@ -12,6 +15,7 @@ import (
"github.com/chromedp/cdproto/cdp"
"github.com/chromedp/cdproto/css"
"github.com/chromedp/cdproto/dom"
"github.com/chromedp/cdproto/emulation"
"github.com/chromedp/chromedp/kb"
)
@ -739,13 +743,21 @@ func TestScreenshot(t *testing.T) {
defer cancel()
tests := []struct {
sel string
by QueryOption
sel string
by QueryOption
size int
}{
{"/html/body/img", BySearch},
{"img", ByQueryAll},
{"img", ByQuery},
{"#icon-github", ByID},
{"/html/body/img", BySearch, 239},
{"img", ByQueryAll, 239},
{"#icon-github", ByID, 120},
}
// a smaller viewport speeds up this test
width, height := 650, 450
if err := Run(ctx, emulation.SetDeviceMetricsOverride(
int64(width), int64(height), 1.0, false,
)); err != nil {
t.Fatal(err)
}
for i, test := range tests {
@ -757,7 +769,17 @@ func TestScreenshot(t *testing.T) {
if len(buf) == 0 {
t.Fatalf("test %d failed to capture screenshot", i)
}
//TODO: test image
config, format, err := image.DecodeConfig(bytes.NewReader(buf))
if err != nil {
t.Fatal(err)
}
if want := "png"; format != want {
t.Fatalf("expected format to be %q, got %q", want, format)
}
if config.Width != test.size || config.Height != test.size {
t.Fatalf("expected dimensions to be %d*%d, got %d*%d",
test.size, test.size, config.Width, config.Height)
}
}
}