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:
parent
0e92de5e65
commit
f742f327a7
|
@ -30,8 +30,8 @@ func ExampleTitle() {
|
||||||
cancel()
|
cancel()
|
||||||
chromedp.FromContext(ctx).Allocator.Wait()
|
chromedp.FromContext(ctx).Allocator.Wait()
|
||||||
|
|
||||||
// Output:
|
// no expected output, to not run this test as part of 'go test'; it's
|
||||||
// Issues · chromedp/chromedp · GitHub
|
// too slow, requiring internet access.
|
||||||
}
|
}
|
||||||
|
|
||||||
func ExampleExecAllocatorOption() {
|
func ExampleExecAllocatorOption() {
|
||||||
|
|
20
nav_test.go
20
nav_test.go
|
@ -1,10 +1,14 @@
|
||||||
package chromedp
|
package chromedp
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
|
"image"
|
||||||
|
_ "image/png"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/chromedp/cdproto/emulation"
|
||||||
"github.com/chromedp/cdproto/page"
|
"github.com/chromedp/cdproto/page"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -213,18 +217,28 @@ func TestCaptureScreenshot(t *testing.T) {
|
||||||
ctx, cancel := testAllocate(t, "image.html")
|
ctx, cancel := testAllocate(t, "image.html")
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
|
// set the viewport size, to know what screenshot size to expect
|
||||||
|
width, height := 650, 450
|
||||||
var buf []byte
|
var buf []byte
|
||||||
if err := Run(ctx, Tasks{
|
if err := Run(ctx, Tasks{
|
||||||
|
emulation.SetDeviceMetricsOverride(int64(width), int64(height), 1.0, false),
|
||||||
WaitVisible(`#icon-brankas`, ByID), // for image.html
|
WaitVisible(`#icon-brankas`, ByID), // for image.html
|
||||||
CaptureScreenshot(&buf),
|
CaptureScreenshot(&buf),
|
||||||
}); err != nil {
|
}); err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(buf) == 0 {
|
config, format, err := image.DecodeConfig(bytes.NewReader(buf))
|
||||||
t.Fatal("failed to capture screenshot")
|
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) {
|
/*func TestAddOnLoadScript(t *testing.T) {
|
||||||
|
|
|
@ -1,7 +1,10 @@
|
||||||
package chromedp
|
package chromedp
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"image"
|
||||||
|
_ "image/png"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
|
@ -12,6 +15,7 @@ import (
|
||||||
"github.com/chromedp/cdproto/cdp"
|
"github.com/chromedp/cdproto/cdp"
|
||||||
"github.com/chromedp/cdproto/css"
|
"github.com/chromedp/cdproto/css"
|
||||||
"github.com/chromedp/cdproto/dom"
|
"github.com/chromedp/cdproto/dom"
|
||||||
|
"github.com/chromedp/cdproto/emulation"
|
||||||
|
|
||||||
"github.com/chromedp/chromedp/kb"
|
"github.com/chromedp/chromedp/kb"
|
||||||
)
|
)
|
||||||
|
@ -739,13 +743,21 @@ func TestScreenshot(t *testing.T) {
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
sel string
|
sel string
|
||||||
by QueryOption
|
by QueryOption
|
||||||
|
size int
|
||||||
}{
|
}{
|
||||||
{"/html/body/img", BySearch},
|
{"/html/body/img", BySearch, 239},
|
||||||
{"img", ByQueryAll},
|
{"img", ByQueryAll, 239},
|
||||||
{"img", ByQuery},
|
{"#icon-github", ByID, 120},
|
||||||
{"#icon-github", ByID},
|
}
|
||||||
|
|
||||||
|
// 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 {
|
for i, test := range tests {
|
||||||
|
@ -757,7 +769,17 @@ func TestScreenshot(t *testing.T) {
|
||||||
if len(buf) == 0 {
|
if len(buf) == 0 {
|
||||||
t.Fatalf("test %d failed to capture screenshot", i)
|
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)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user