2017-02-15 14:46:07 +00:00
|
|
|
package chromedp
|
|
|
|
|
|
|
|
import (
|
2019-03-21 16:39:10 +00:00
|
|
|
"bytes"
|
2019-03-21 20:09:46 +00:00
|
|
|
"fmt"
|
2019-03-21 16:39:10 +00:00
|
|
|
"image"
|
|
|
|
_ "image/png"
|
2019-03-21 20:09:46 +00:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
2017-02-15 14:46:07 +00:00
|
|
|
"strings"
|
|
|
|
"testing"
|
2019-03-28 21:21:52 +00:00
|
|
|
"time"
|
2017-02-20 13:23:41 +00:00
|
|
|
|
2019-03-21 16:39:10 +00:00
|
|
|
"github.com/chromedp/cdproto/emulation"
|
2017-12-27 02:30:28 +00:00
|
|
|
"github.com/chromedp/cdproto/page"
|
2017-02-15 14:46:07 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestNavigate(t *testing.T) {
|
2017-02-18 06:11:46 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
2019-03-20 12:05:04 +00:00
|
|
|
ctx, cancel := testAllocate(t, "image.html")
|
2019-03-05 13:14:50 +00:00
|
|
|
defer cancel()
|
2017-02-15 14:46:07 +00:00
|
|
|
|
2019-03-05 13:14:50 +00:00
|
|
|
if err := Run(ctx, WaitVisible(`#icon-brankas`, ByID)); err != nil {
|
2017-02-15 14:46:07 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var urlstr string
|
2019-03-05 13:14:50 +00:00
|
|
|
if err := Run(ctx, Location(&urlstr)); err != nil {
|
2017-02-15 14:46:07 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2019-03-05 13:14:50 +00:00
|
|
|
|
2019-03-20 12:05:04 +00:00
|
|
|
if !strings.HasSuffix(urlstr, "image.html") {
|
2017-02-22 10:44:07 +00:00
|
|
|
t.Errorf("expected to be on image.html, at: %s", urlstr)
|
2017-02-15 14:46:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var title string
|
2019-03-05 13:14:50 +00:00
|
|
|
if err := Run(ctx, Title(&title)); err != nil {
|
2017-02-15 14:46:07 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2019-03-05 13:14:50 +00:00
|
|
|
|
2019-03-20 12:05:04 +00:00
|
|
|
exptitle := "this is title"
|
2017-02-22 10:44:07 +00:00
|
|
|
if title != exptitle {
|
2017-02-15 14:46:07 +00:00
|
|
|
t.Errorf("expected title to contain google, instead title is: %s", title)
|
|
|
|
}
|
|
|
|
}
|
2017-02-20 13:23:41 +00:00
|
|
|
|
|
|
|
func TestNavigationEntries(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
2019-03-05 13:14:50 +00:00
|
|
|
ctx, cancel := testAllocate(t, "")
|
|
|
|
defer cancel()
|
2017-02-20 13:23:41 +00:00
|
|
|
|
2019-03-20 17:27:57 +00:00
|
|
|
tests := []struct {
|
|
|
|
file, waitID string
|
|
|
|
}{
|
|
|
|
{"form.html", "#form"},
|
|
|
|
{"image.html", "#icon-brankas"},
|
2017-02-20 13:23:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var entries []*page.NavigationEntry
|
|
|
|
var index int64
|
2019-03-05 13:14:50 +00:00
|
|
|
if err := Run(ctx, NavigationEntries(&index, &entries)); err != nil {
|
2017-02-20 13:23:41 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2019-03-20 16:56:03 +00:00
|
|
|
if len(entries) != 1 {
|
|
|
|
t.Errorf("expected to have 1 navigation entry: got %d", len(entries))
|
2017-02-20 13:23:41 +00:00
|
|
|
}
|
2019-03-20 16:56:03 +00:00
|
|
|
if index != 0 {
|
|
|
|
t.Errorf("expected navigation index is 0, got: %d", index)
|
2017-02-20 13:23:41 +00:00
|
|
|
}
|
|
|
|
|
2019-03-20 16:56:03 +00:00
|
|
|
expIdx, expEntries := 1, 2
|
2019-03-20 17:27:57 +00:00
|
|
|
for i, test := range tests {
|
2019-04-01 15:57:22 +00:00
|
|
|
if err := Run(ctx,
|
|
|
|
Navigate(testdataDir+"/"+test.file),
|
2019-03-20 17:27:57 +00:00
|
|
|
WaitVisible(test.waitID, ByID),
|
|
|
|
NavigationEntries(&index, &entries),
|
2019-04-01 15:57:22 +00:00
|
|
|
); err != nil {
|
2017-02-20 13:23:41 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if len(entries) != expEntries {
|
|
|
|
t.Errorf("test %d expected to have %d navigation entry: got %d", i, expEntries, len(entries))
|
|
|
|
}
|
2019-03-20 16:56:03 +00:00
|
|
|
if want := int64(i + 1); index != want {
|
2019-03-05 13:14:50 +00:00
|
|
|
t.Errorf("test %d expected navigation index is %d, got: %d", i, want, index)
|
2017-02-20 13:23:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
expIdx++
|
|
|
|
expEntries++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNavigateToHistoryEntry(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
2019-03-20 12:05:04 +00:00
|
|
|
ctx, cancel := testAllocate(t, "image.html")
|
2019-03-05 13:14:50 +00:00
|
|
|
defer cancel()
|
2017-02-20 13:23:41 +00:00
|
|
|
|
|
|
|
var entries []*page.NavigationEntry
|
|
|
|
var index int64
|
2019-04-01 15:57:22 +00:00
|
|
|
if err := Run(ctx,
|
2019-03-20 17:27:57 +00:00
|
|
|
WaitVisible(`#icon-brankas`, ByID), // for image.html
|
|
|
|
NavigationEntries(&index, &entries),
|
2017-02-20 13:23:41 +00:00
|
|
|
|
2019-04-01 15:57:22 +00:00
|
|
|
Navigate(testdataDir+"/form.html"),
|
2019-03-20 17:27:57 +00:00
|
|
|
WaitVisible(`#form`, ByID), // for form.html
|
2019-04-01 15:57:22 +00:00
|
|
|
); err != nil {
|
2017-02-20 13:23:41 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var title string
|
2019-04-01 15:57:22 +00:00
|
|
|
if err := Run(ctx,
|
2019-03-20 17:27:57 +00:00
|
|
|
NavigateToHistoryEntry(entries[index].ID),
|
|
|
|
WaitVisible(`#icon-brankas`, ByID), // for image.html
|
|
|
|
Title(&title),
|
2019-04-01 15:57:22 +00:00
|
|
|
); err != nil {
|
2017-02-20 13:23:41 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if title != entries[index].Title {
|
|
|
|
t.Errorf("expected title to be %s, instead title is: %s", entries[index].Title, title)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNavigateBack(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
2019-03-20 12:05:04 +00:00
|
|
|
ctx, cancel := testAllocate(t, "form.html")
|
2019-03-05 13:14:50 +00:00
|
|
|
defer cancel()
|
2017-02-20 13:23:41 +00:00
|
|
|
|
2019-03-20 17:27:57 +00:00
|
|
|
var title, exptitle string
|
2019-04-01 15:57:22 +00:00
|
|
|
if err := Run(ctx,
|
2019-03-20 17:27:57 +00:00
|
|
|
WaitVisible(`#form`, ByID), // for form.html
|
|
|
|
Title(&exptitle),
|
2017-02-20 13:23:41 +00:00
|
|
|
|
2019-04-01 15:57:22 +00:00
|
|
|
Navigate(testdataDir+"/image.html"),
|
2019-03-20 17:27:57 +00:00
|
|
|
WaitVisible(`#icon-brankas`, ByID), // for image.html
|
2017-02-20 13:23:41 +00:00
|
|
|
|
2019-03-20 17:27:57 +00:00
|
|
|
NavigateBack(),
|
|
|
|
WaitVisible(`#form`, ByID), // for form.html
|
|
|
|
Title(&title),
|
2019-04-01 15:57:22 +00:00
|
|
|
); err != nil {
|
2017-02-20 13:23:41 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2019-03-05 13:14:50 +00:00
|
|
|
|
2017-02-22 10:44:07 +00:00
|
|
|
if title != exptitle {
|
|
|
|
t.Errorf("expected title to be %s, instead title is: %s", exptitle, title)
|
2017-02-20 13:23:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNavigateForward(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
2019-03-20 12:05:04 +00:00
|
|
|
ctx, cancel := testAllocate(t, "form.html")
|
2019-03-05 13:14:50 +00:00
|
|
|
defer cancel()
|
2017-02-20 13:23:41 +00:00
|
|
|
|
2019-03-20 17:27:57 +00:00
|
|
|
var title, exptitle string
|
2019-04-01 15:57:22 +00:00
|
|
|
if err := Run(ctx,
|
2019-03-20 17:27:57 +00:00
|
|
|
WaitVisible(`#form`, ByID), // for form.html
|
2017-02-20 13:23:41 +00:00
|
|
|
|
2019-04-01 15:57:22 +00:00
|
|
|
Navigate(testdataDir+"/image.html"),
|
2019-03-20 17:27:57 +00:00
|
|
|
WaitVisible(`#icon-brankas`, ByID), // for image.html
|
|
|
|
Title(&exptitle),
|
2017-02-20 13:23:41 +00:00
|
|
|
|
2019-03-20 17:27:57 +00:00
|
|
|
NavigateBack(),
|
|
|
|
WaitVisible(`#form`, ByID), // for form.html
|
2017-02-20 13:23:41 +00:00
|
|
|
|
2019-03-20 17:27:57 +00:00
|
|
|
NavigateForward(),
|
|
|
|
WaitVisible(`#icon-brankas`, ByID), // for image.html
|
|
|
|
Title(&title),
|
2019-04-01 15:57:22 +00:00
|
|
|
); err != nil {
|
2017-02-20 13:23:41 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2019-03-05 13:14:50 +00:00
|
|
|
|
2017-02-22 10:44:07 +00:00
|
|
|
if title != exptitle {
|
|
|
|
t.Errorf("expected title to be %s, instead title is: %s", exptitle, title)
|
2017-02-20 13:23:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStop(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
2019-03-20 12:05:04 +00:00
|
|
|
ctx, cancel := testAllocate(t, "form.html")
|
2019-03-05 13:14:50 +00:00
|
|
|
defer cancel()
|
|
|
|
if err := Run(ctx, Stop()); err != nil {
|
2017-02-20 13:23:41 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestReload(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
2019-03-21 20:09:46 +00:00
|
|
|
count := 0
|
|
|
|
// create test server
|
|
|
|
mux := http.NewServeMux()
|
|
|
|
mux.HandleFunc("/", func(res http.ResponseWriter, req *http.Request) {
|
|
|
|
fmt.Fprintf(res, `<html>
|
|
|
|
<head>
|
|
|
|
<title>Title</title>
|
|
|
|
</head>
|
|
|
|
<body>
|
|
|
|
<div id="count%d"></div>
|
|
|
|
</body></html`, count)
|
|
|
|
count++
|
|
|
|
})
|
|
|
|
s := httptest.NewServer(mux)
|
|
|
|
defer s.Close()
|
|
|
|
|
|
|
|
ctx, cancel := testAllocate(t, "")
|
2019-03-05 13:14:50 +00:00
|
|
|
defer cancel()
|
2017-02-20 13:23:41 +00:00
|
|
|
|
2019-03-20 17:27:57 +00:00
|
|
|
var title, exptitle string
|
2019-04-01 15:57:22 +00:00
|
|
|
if err := Run(ctx,
|
2019-03-21 20:09:46 +00:00
|
|
|
Navigate(s.URL),
|
|
|
|
WaitReady(`#count0`, ByID),
|
2019-03-20 17:27:57 +00:00
|
|
|
Title(&exptitle),
|
|
|
|
|
|
|
|
Reload(),
|
2019-03-21 20:09:46 +00:00
|
|
|
WaitReady(`#count1`, ByID),
|
2019-03-20 17:27:57 +00:00
|
|
|
Title(&title),
|
2019-04-01 15:57:22 +00:00
|
|
|
); err != nil {
|
2017-02-20 13:23:41 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2019-03-05 13:14:50 +00:00
|
|
|
|
2017-02-22 10:44:07 +00:00
|
|
|
if title != exptitle {
|
|
|
|
t.Errorf("expected title to be %s, instead title is: %s", exptitle, title)
|
2017-02-20 13:23:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCaptureScreenshot(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
2019-03-20 12:05:04 +00:00
|
|
|
ctx, cancel := testAllocate(t, "image.html")
|
2019-03-05 13:14:50 +00:00
|
|
|
defer cancel()
|
2017-02-20 13:23:41 +00:00
|
|
|
|
2019-03-21 16:39:10 +00:00
|
|
|
// set the viewport size, to know what screenshot size to expect
|
|
|
|
width, height := 650, 450
|
2017-02-20 13:23:41 +00:00
|
|
|
var buf []byte
|
2019-04-01 15:57:22 +00:00
|
|
|
if err := Run(ctx,
|
2019-03-21 16:39:10 +00:00
|
|
|
emulation.SetDeviceMetricsOverride(int64(width), int64(height), 1.0, false),
|
2019-03-20 17:27:57 +00:00
|
|
|
WaitVisible(`#icon-brankas`, ByID), // for image.html
|
|
|
|
CaptureScreenshot(&buf),
|
2019-04-01 15:57:22 +00:00
|
|
|
); err != nil {
|
2017-02-20 13:23:41 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2019-03-21 16:39:10 +00:00
|
|
|
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)
|
2017-02-20 13:23:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-09 01:40:29 +00:00
|
|
|
/*func TestAddOnLoadScript(t *testing.T) {
|
2017-02-20 13:23:41 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
2019-03-05 13:14:50 +00:00
|
|
|
ctx, cancel := testAllocate(t, "")
|
|
|
|
defer cancel()
|
2017-02-20 13:23:41 +00:00
|
|
|
|
|
|
|
var scriptID page.ScriptIdentifier
|
2019-03-05 13:14:50 +00:00
|
|
|
err = Run(ctx, AddOnLoadScript(`window.alert("TEST")`, &scriptID))
|
2017-02-20 13:23:41 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2019-03-05 13:14:50 +00:00
|
|
|
err = Run(ctx, Navigate(testdataDir+"/form.html"))
|
2017-02-20 13:23:41 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if scriptID == "" {
|
|
|
|
t.Fatal("got empty script ID")
|
|
|
|
}
|
|
|
|
// TODO: Handle javascript dialog.
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRemoveOnLoadScript(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
2019-03-05 13:14:50 +00:00
|
|
|
ctx, cancel := testAllocate(t, "")
|
|
|
|
defer cancel()
|
2017-02-20 13:23:41 +00:00
|
|
|
|
|
|
|
var scriptID page.ScriptIdentifier
|
2019-03-05 13:14:50 +00:00
|
|
|
err = Run(ctx, AddOnLoadScript(`window.alert("TEST")`, &scriptID))
|
2017-02-20 13:23:41 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if scriptID == "" {
|
|
|
|
t.Fatal("got empty script ID")
|
|
|
|
}
|
|
|
|
|
2019-03-05 13:14:50 +00:00
|
|
|
err = Run(ctx, RemoveOnLoadScript(scriptID))
|
2017-02-20 13:23:41 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2019-03-05 13:14:50 +00:00
|
|
|
err = Run(ctx, Navigate(testdataDir+"/form.html"))
|
2017-02-20 13:23:41 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-07-09 01:40:29 +00:00
|
|
|
}*/
|
2017-02-20 13:23:41 +00:00
|
|
|
|
|
|
|
func TestLocation(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
2019-03-20 12:05:04 +00:00
|
|
|
ctx, cancel := testAllocate(t, "form.html")
|
2019-03-05 13:14:50 +00:00
|
|
|
defer cancel()
|
2017-02-20 13:23:41 +00:00
|
|
|
|
2017-02-22 10:44:07 +00:00
|
|
|
var urlstr string
|
2019-04-01 15:57:22 +00:00
|
|
|
if err := Run(ctx,
|
2019-03-20 17:27:57 +00:00
|
|
|
WaitVisible(`#form`, ByID), // for form.html
|
|
|
|
Location(&urlstr),
|
2019-04-01 15:57:22 +00:00
|
|
|
); err != nil {
|
2017-02-20 13:23:41 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2019-03-20 12:05:04 +00:00
|
|
|
if !strings.HasSuffix(urlstr, "form.html") {
|
2018-03-26 21:30:59 +00:00
|
|
|
t.Fatalf("expected to be on form.html, got: %s", urlstr)
|
2017-02-20 13:23:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTitle(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
2019-03-20 12:05:04 +00:00
|
|
|
ctx, cancel := testAllocate(t, "image.html")
|
2019-03-05 13:14:50 +00:00
|
|
|
defer cancel()
|
2017-02-20 13:23:41 +00:00
|
|
|
|
|
|
|
var title string
|
2019-04-01 15:57:22 +00:00
|
|
|
if err := Run(ctx,
|
2019-03-20 17:27:57 +00:00
|
|
|
WaitVisible(`#icon-brankas`, ByID), // for image.html
|
|
|
|
Title(&title),
|
2019-04-01 15:57:22 +00:00
|
|
|
); err != nil {
|
2017-02-20 13:23:41 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2019-03-20 12:05:04 +00:00
|
|
|
exptitle := "this is title"
|
2017-02-22 10:44:07 +00:00
|
|
|
if title != exptitle {
|
|
|
|
t.Fatalf("expected title to be %s, got: %s", exptitle, title)
|
2017-02-20 13:23:41 +00:00
|
|
|
}
|
|
|
|
}
|
2019-03-28 21:21:52 +00:00
|
|
|
|
|
|
|
func TestLoadIframe(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
ctx, cancel := testAllocate(t, "iframe.html")
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
if err := Run(ctx, Tasks{
|
|
|
|
// TODO: remove the sleep once we have better support for
|
|
|
|
// iframes.
|
|
|
|
Sleep(10 * time.Millisecond),
|
|
|
|
//WaitVisible(`#form`, ByID), // for the nested form.html
|
|
|
|
}); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|