From f742f327a7836d3caf4b12c9995ddda6aba3a92f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Mart=C3=AD?= Date: Thu, 21 Mar 2019 16:39:10 +0000 Subject: [PATCH] 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. --- example_test.go | 4 ++-- nav_test.go | 20 +++++++++++++++++--- query_test.go | 36 +++++++++++++++++++++++++++++------- 3 files changed, 48 insertions(+), 12 deletions(-) diff --git a/example_test.go b/example_test.go index aaaa492..7911b3f 100644 --- a/example_test.go +++ b/example_test.go @@ -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() { diff --git a/nav_test.go b/nav_test.go index 8aa5448..ad37722 100644 --- a/nav_test.go +++ b/nav_test.go @@ -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) { diff --git a/query_test.go b/query_test.go index 9aec711..67659be 100644 --- a/query_test.go +++ b/query_test.go @@ -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) + } } }