2017-02-15 14:46:07 +00:00
|
|
|
package chromedp
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
"testing"
|
2017-02-20 13:23:41 +00:00
|
|
|
"time"
|
|
|
|
|
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-05 13:14:50 +00:00
|
|
|
ctx, cancel := testAllocate(t, "")
|
|
|
|
defer cancel()
|
2017-02-15 14:46:07 +00:00
|
|
|
|
2017-02-22 10:44:07 +00:00
|
|
|
expurl, exptitle := testdataDir+"/image.html", "this is title"
|
2019-03-05 13:14:50 +00:00
|
|
|
if err := Run(ctx, Navigate(expurl)); err != nil {
|
2017-02-15 14:46:07 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
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
|
|
|
|
2017-02-22 10:44:07 +00:00
|
|
|
if !strings.HasPrefix(urlstr, expurl) {
|
|
|
|
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
|
|
|
|
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()
|
|
|
|
time.Sleep(50 * time.Millisecond)
|
2017-02-20 13:23:41 +00:00
|
|
|
|
|
|
|
tests := []string{
|
2017-02-22 10:44:07 +00:00
|
|
|
"form.html",
|
|
|
|
"image.html",
|
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-05 13:14:50 +00:00
|
|
|
if len(entries) != 2 {
|
|
|
|
t.Errorf("expected to have 2 navigation entry: got %d", len(entries))
|
2017-02-20 13:23:41 +00:00
|
|
|
}
|
2019-03-05 13:14:50 +00:00
|
|
|
if index != 1 {
|
|
|
|
t.Errorf("expected navigation index is 1, got: %d", index)
|
2017-02-20 13:23:41 +00:00
|
|
|
}
|
|
|
|
|
2019-03-05 13:14:50 +00:00
|
|
|
expIdx, expEntries := 2, 3
|
2017-02-20 13:23:41 +00:00
|
|
|
for i, url := range tests {
|
2019-03-05 13:14:50 +00:00
|
|
|
if err := Run(ctx, Navigate(testdataDir+"/"+url)); err != nil {
|
2017-02-20 13:23:41 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2017-06-18 02:32:12 +00:00
|
|
|
time.Sleep(50 * time.Millisecond)
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(entries) != expEntries {
|
|
|
|
t.Errorf("test %d expected to have %d navigation entry: got %d", i, expEntries, len(entries))
|
|
|
|
}
|
2019-03-05 13:14:50 +00:00
|
|
|
if want := int64(i + 2); index != want {
|
|
|
|
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-05 13:14:50 +00:00
|
|
|
ctx, cancel := testAllocate(t, "")
|
|
|
|
defer cancel()
|
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, Navigate(testdataDir+"/image.html")); err != nil {
|
2017-02-20 13:23:41 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2017-06-18 02:32:12 +00:00
|
|
|
time.Sleep(50 * time.Millisecond)
|
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-05 13:14:50 +00:00
|
|
|
if err := Run(ctx, Navigate(testdataDir+"/form.html")); err != nil {
|
2017-02-20 13:23:41 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2017-06-18 02:32:12 +00:00
|
|
|
time.Sleep(50 * time.Millisecond)
|
2019-03-05 13:14:50 +00:00
|
|
|
if err := Run(ctx, NavigateToHistoryEntry(entries[index].ID)); err != nil {
|
2017-02-20 13:23:41 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2017-06-18 02:32:12 +00:00
|
|
|
time.Sleep(50 * time.Millisecond)
|
2017-02-20 13:23:41 +00:00
|
|
|
|
|
|
|
var title string
|
2019-03-05 13:14:50 +00:00
|
|
|
if err := Run(ctx, Title(&title)); err != nil {
|
2017-02-20 13:23:41 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2019-03-05 13:14:50 +00:00
|
|
|
|
2017-02-20 13:23:41 +00:00
|
|
|
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-05 13:14:50 +00:00
|
|
|
ctx, cancel := testAllocate(t, "")
|
|
|
|
defer cancel()
|
|
|
|
if err := Run(ctx, Navigate(testdataDir+"/form.html")); err != nil {
|
2017-02-20 13:23:41 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2017-06-18 02:32:12 +00:00
|
|
|
time.Sleep(50 * time.Millisecond)
|
2017-02-20 13:23:41 +00:00
|
|
|
|
2017-02-22 10:44:07 +00:00
|
|
|
var exptitle string
|
2019-03-05 13:14:50 +00:00
|
|
|
if err := Run(ctx, Title(&exptitle)); err != nil {
|
2017-02-20 13:23:41 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2019-03-05 13:14:50 +00:00
|
|
|
if err := Run(ctx, Navigate(testdataDir+"/image.html")); err != nil {
|
2017-02-20 13:23:41 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2017-06-18 02:32:12 +00:00
|
|
|
time.Sleep(50 * time.Millisecond)
|
2019-03-05 13:14:50 +00:00
|
|
|
if err := Run(ctx, NavigateBack()); err != nil {
|
2017-02-20 13:23:41 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2017-06-18 02:32:12 +00:00
|
|
|
time.Sleep(50 * time.Millisecond)
|
2017-02-20 13:23:41 +00:00
|
|
|
|
|
|
|
var title string
|
2019-03-05 13:14:50 +00:00
|
|
|
if err := Run(ctx, Title(&title)); 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-05 13:14:50 +00:00
|
|
|
ctx, cancel := testAllocate(t, "")
|
|
|
|
defer cancel()
|
|
|
|
if err := Run(ctx, Navigate(testdataDir+"/form.html")); err != nil {
|
2017-02-20 13:23:41 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2017-06-18 02:32:12 +00:00
|
|
|
time.Sleep(50 * time.Millisecond)
|
2019-03-05 13:14:50 +00:00
|
|
|
if err := Run(ctx, Navigate(testdataDir+"/image.html")); err != nil {
|
2017-02-20 13:23:41 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2017-06-18 02:32:12 +00:00
|
|
|
time.Sleep(50 * time.Millisecond)
|
2017-02-20 13:23:41 +00:00
|
|
|
|
2017-02-22 10:44:07 +00:00
|
|
|
var exptitle string
|
2019-03-05 13:14:50 +00:00
|
|
|
if err := Run(ctx, Title(&exptitle)); err != nil {
|
2017-02-20 13:23:41 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2019-03-05 13:14:50 +00:00
|
|
|
if err := Run(ctx, NavigateBack()); err != nil {
|
2017-02-20 13:23:41 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2017-06-18 02:32:12 +00:00
|
|
|
time.Sleep(50 * time.Millisecond)
|
2019-03-05 13:14:50 +00:00
|
|
|
if err := Run(ctx, NavigateForward()); err != nil {
|
2017-02-20 13:23:41 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2017-06-18 02:32:12 +00:00
|
|
|
time.Sleep(50 * time.Millisecond)
|
2017-02-20 13:23:41 +00:00
|
|
|
|
|
|
|
var title string
|
2019-03-05 13:14:50 +00:00
|
|
|
if err := Run(ctx, Title(&title)); 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-05 13:14:50 +00:00
|
|
|
ctx, cancel := testAllocate(t, "")
|
|
|
|
defer cancel()
|
|
|
|
if err := Run(ctx, Navigate(testdataDir+"/form.html")); err != nil {
|
2017-02-20 13:23:41 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2019-03-05 13:14:50 +00:00
|
|
|
if err := Run(ctx, Stop()); err != nil {
|
2017-02-20 13:23:41 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2019-03-05 13:14:50 +00:00
|
|
|
|
2017-02-20 13:23:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestReload(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
2019-03-05 13:14:50 +00:00
|
|
|
ctx, cancel := testAllocate(t, "")
|
|
|
|
defer cancel()
|
|
|
|
if err := Run(ctx, Navigate(testdataDir+"/form.html")); err != nil {
|
2017-02-20 13:23:41 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2017-06-18 02:32:12 +00:00
|
|
|
time.Sleep(50 * time.Millisecond)
|
2017-02-20 13:23:41 +00:00
|
|
|
|
2017-02-22 10:44:07 +00:00
|
|
|
var exptitle string
|
2019-03-05 13:14:50 +00:00
|
|
|
if err := Run(ctx, Title(&exptitle)); err != nil {
|
2017-02-20 13:23:41 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2019-03-05 13:14:50 +00:00
|
|
|
if err := Run(ctx, Reload()); err != nil {
|
2017-02-20 13:23:41 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2017-06-18 02:32:12 +00:00
|
|
|
time.Sleep(50 * time.Millisecond)
|
2017-02-20 13:23:41 +00:00
|
|
|
|
|
|
|
var title string
|
2019-03-05 13:14:50 +00:00
|
|
|
if err := Run(ctx, Title(&title)); 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-05 13:14:50 +00:00
|
|
|
ctx, cancel := testAllocate(t, "")
|
|
|
|
defer cancel()
|
|
|
|
if err := Run(ctx, Navigate(testdataDir+"/image.html")); err != nil {
|
2017-02-20 13:23:41 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2017-06-18 02:32:12 +00:00
|
|
|
time.Sleep(50 * time.Millisecond)
|
2017-02-20 13:23:41 +00:00
|
|
|
|
|
|
|
var buf []byte
|
2019-03-05 13:14:50 +00:00
|
|
|
if err := Run(ctx, CaptureScreenshot(&buf)); err != nil {
|
2017-02-20 13:23:41 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(buf) == 0 {
|
2017-03-02 02:53:47 +00:00
|
|
|
t.Fatal("failed to capture screenshot")
|
2017-02-20 13:23:41 +00:00
|
|
|
}
|
|
|
|
//TODO: test image
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2017-06-18 02:32:12 +00:00
|
|
|
time.Sleep(50 * time.Millisecond)
|
2017-02-20 13:23:41 +00:00
|
|
|
|
|
|
|
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-06-18 02:32:12 +00:00
|
|
|
time.Sleep(50 * time.Millisecond)
|
2017-07-09 01:40:29 +00:00
|
|
|
}*/
|
2017-02-20 13:23:41 +00:00
|
|
|
|
|
|
|
func TestLocation(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
2018-03-26 21:30:59 +00:00
|
|
|
expurl := testdataDir + "/form.html"
|
2017-02-20 13:23:41 +00:00
|
|
|
|
2019-03-05 13:14:50 +00:00
|
|
|
ctx, cancel := testAllocate(t, "")
|
|
|
|
defer cancel()
|
|
|
|
if err := Run(ctx, Navigate(expurl)); err != nil {
|
2017-02-20 13:23:41 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2017-06-18 02:32:12 +00:00
|
|
|
time.Sleep(50 * time.Millisecond)
|
2017-02-20 13:23:41 +00:00
|
|
|
|
2017-02-22 10:44:07 +00:00
|
|
|
var urlstr string
|
2019-03-05 13:14:50 +00:00
|
|
|
if err := Run(ctx, Location(&urlstr)); err != nil {
|
2017-02-20 13:23:41 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2017-02-22 10:44:07 +00:00
|
|
|
if urlstr != expurl {
|
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()
|
|
|
|
|
2017-02-22 10:44:07 +00:00
|
|
|
expurl, exptitle := testdataDir+"/image.html", "this is title"
|
2017-02-20 13:23:41 +00:00
|
|
|
|
2019-03-05 13:14:50 +00:00
|
|
|
ctx, cancel := testAllocate(t, "")
|
|
|
|
defer cancel()
|
|
|
|
if err := Run(ctx, Navigate(expurl)); err != nil {
|
2017-02-20 13:23:41 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2017-06-18 02:32:12 +00:00
|
|
|
time.Sleep(50 * time.Millisecond)
|
2017-02-20 13:23:41 +00:00
|
|
|
|
|
|
|
var title string
|
2019-03-05 13:14:50 +00:00
|
|
|
if err := Run(ctx, Title(&title)); err != nil {
|
2017-02-20 13:23:41 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|