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()
|
|
|
|
|
2017-02-15 14:46:07 +00:00
|
|
|
var err error
|
|
|
|
|
|
|
|
c := testAllocate(t, "")
|
|
|
|
defer c.Release()
|
|
|
|
|
2017-02-22 10:44:07 +00:00
|
|
|
expurl, exptitle := testdataDir+"/image.html", "this is title"
|
|
|
|
|
|
|
|
err = c.Run(defaultContext, Navigate(expurl))
|
2017-02-15 14:46:07 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2017-02-22 10:44:07 +00:00
|
|
|
err = c.Run(defaultContext, WaitVisible(`#icon-brankas`, ByID))
|
2017-02-15 14:46:07 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var urlstr string
|
|
|
|
err = c.Run(defaultContext, Location(&urlstr))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
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
|
|
|
|
err = c.Run(defaultContext, Title(&title))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
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()
|
|
|
|
|
|
|
|
var err error
|
|
|
|
|
|
|
|
c := testAllocate(t, "")
|
|
|
|
defer c.Release()
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
err = c.Run(defaultContext, NavigationEntries(&index, &entries))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(entries) != 1 {
|
|
|
|
t.Errorf("expected to have 1 navigation entry: got %d", len(entries))
|
|
|
|
}
|
|
|
|
if index != 0 {
|
|
|
|
t.Errorf("expected navigation index is 0, got: %d", index)
|
|
|
|
}
|
|
|
|
|
|
|
|
expIdx, expEntries := 1, 2
|
|
|
|
for i, url := range tests {
|
2017-02-22 10:44:07 +00:00
|
|
|
err = c.Run(defaultContext, Navigate(testdataDir+"/"+url))
|
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
|
|
|
|
|
|
|
err = c.Run(defaultContext, NavigationEntries(&index, &entries))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(entries) != expEntries {
|
|
|
|
t.Errorf("test %d expected to have %d navigation entry: got %d", i, expEntries, len(entries))
|
|
|
|
}
|
|
|
|
if index != int64(i+1) {
|
|
|
|
t.Errorf("test %d expected navigation index is %d, got: %d", i, i, index)
|
|
|
|
}
|
|
|
|
|
|
|
|
expIdx++
|
|
|
|
expEntries++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNavigateToHistoryEntry(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
var err error
|
|
|
|
|
|
|
|
c := testAllocate(t, "")
|
|
|
|
defer c.Release()
|
|
|
|
|
|
|
|
var entries []*page.NavigationEntry
|
|
|
|
var index int64
|
2017-02-22 10:44:07 +00:00
|
|
|
err = c.Run(defaultContext, Navigate(testdataDir+"/"+"image.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
|
|
|
|
|
|
|
err = c.Run(defaultContext, NavigationEntries(&index, &entries))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2017-02-22 10:44:07 +00:00
|
|
|
err = c.Run(defaultContext, 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
|
|
|
|
|
|
|
err = c.Run(defaultContext, NavigateToHistoryEntry(entries[index].ID))
|
|
|
|
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
|
|
|
|
|
|
|
var title string
|
|
|
|
err = c.Run(defaultContext, Title(&title))
|
|
|
|
if err != nil {
|
|
|
|
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()
|
|
|
|
|
|
|
|
var err error
|
|
|
|
|
|
|
|
c := testAllocate(t, "")
|
|
|
|
defer c.Release()
|
|
|
|
|
2017-02-22 10:44:07 +00:00
|
|
|
err = c.Run(defaultContext, 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
|
|
|
|
2017-02-22 10:44:07 +00:00
|
|
|
var exptitle string
|
|
|
|
err = c.Run(defaultContext, Title(&exptitle))
|
2017-02-20 13:23:41 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2017-02-22 10:44:07 +00:00
|
|
|
err = c.Run(defaultContext, Navigate(testdataDir+"/"+"image.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
|
|
|
|
2017-02-21 04:25:36 +00:00
|
|
|
err = c.Run(defaultContext, NavigateBack())
|
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
|
|
|
|
|
|
|
var title string
|
|
|
|
err = c.Run(defaultContext, Title(&title))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
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()
|
|
|
|
|
|
|
|
var err error
|
|
|
|
|
|
|
|
c := testAllocate(t, "")
|
|
|
|
defer c.Release()
|
|
|
|
|
2017-02-22 10:44:07 +00:00
|
|
|
err = c.Run(defaultContext, 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
|
|
|
|
2017-02-22 10:44:07 +00:00
|
|
|
err = c.Run(defaultContext, Navigate(testdataDir+"/"+"image.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
|
|
|
|
2017-02-22 10:44:07 +00:00
|
|
|
var exptitle string
|
|
|
|
err = c.Run(defaultContext, Title(&exptitle))
|
2017-02-20 13:23:41 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2017-02-21 04:25:36 +00:00
|
|
|
err = c.Run(defaultContext, NavigateBack())
|
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
|
|
|
|
2017-02-21 04:25:36 +00:00
|
|
|
err = c.Run(defaultContext, NavigateForward())
|
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
|
|
|
|
|
|
|
var title string
|
|
|
|
err = c.Run(defaultContext, Title(&title))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
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()
|
|
|
|
|
|
|
|
var err error
|
|
|
|
|
|
|
|
c := testAllocate(t, "")
|
|
|
|
defer c.Release()
|
|
|
|
|
2017-02-22 10:44:07 +00:00
|
|
|
err = c.Run(defaultContext, Navigate(testdataDir+"/"+"form.html"))
|
2017-02-20 13:23:41 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = c.Run(defaultContext, Stop())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestReload(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
var err error
|
|
|
|
|
|
|
|
c := testAllocate(t, "")
|
|
|
|
defer c.Release()
|
|
|
|
|
2017-02-22 10:44:07 +00:00
|
|
|
err = c.Run(defaultContext, 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
|
|
|
|
2017-02-22 10:44:07 +00:00
|
|
|
var exptitle string
|
|
|
|
err = c.Run(defaultContext, Title(&exptitle))
|
2017-02-20 13:23:41 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = c.Run(defaultContext, Reload())
|
|
|
|
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
|
|
|
|
|
|
|
var title string
|
|
|
|
err = c.Run(defaultContext, Title(&title))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
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()
|
|
|
|
|
|
|
|
var err error
|
|
|
|
|
|
|
|
c := testAllocate(t, "")
|
|
|
|
defer c.Release()
|
|
|
|
|
2017-02-22 10:44:07 +00:00
|
|
|
err = c.Run(defaultContext, Navigate(testdataDir+"/"+"image.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
|
|
|
|
|
|
|
var buf []byte
|
|
|
|
err = c.Run(defaultContext, CaptureScreenshot(&buf))
|
|
|
|
if err != nil {
|
|
|
|
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()
|
|
|
|
|
|
|
|
var err error
|
|
|
|
|
|
|
|
c := testAllocate(t, "")
|
|
|
|
defer c.Release()
|
|
|
|
|
|
|
|
var scriptID page.ScriptIdentifier
|
|
|
|
err = c.Run(defaultContext, AddOnLoadScript(`window.alert("TEST")`, &scriptID))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2017-02-22 10:44:07 +00:00
|
|
|
err = c.Run(defaultContext, 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()
|
|
|
|
|
|
|
|
var err error
|
|
|
|
|
|
|
|
c := testAllocate(t, "")
|
|
|
|
defer c.Release()
|
|
|
|
|
|
|
|
var scriptID page.ScriptIdentifier
|
|
|
|
err = c.Run(defaultContext, AddOnLoadScript(`window.alert("TEST")`, &scriptID))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if scriptID == "" {
|
|
|
|
t.Fatal("got empty script ID")
|
|
|
|
}
|
|
|
|
|
|
|
|
err = c.Run(defaultContext, RemoveOnLoadScript(scriptID))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2017-02-22 10:44:07 +00:00
|
|
|
err = c.Run(defaultContext, 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()
|
|
|
|
|
|
|
|
var err error
|
2017-02-22 10:44:07 +00:00
|
|
|
expurl := testdataDir + "/" + "form.html"
|
2017-02-20 13:23:41 +00:00
|
|
|
|
|
|
|
c := testAllocate(t, "")
|
|
|
|
defer c.Release()
|
|
|
|
|
2017-02-22 10:44:07 +00:00
|
|
|
err = c.Run(defaultContext, Navigate(expurl))
|
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
|
|
|
|
2017-02-22 10:44:07 +00:00
|
|
|
var urlstr string
|
|
|
|
err = c.Run(defaultContext, Location(&urlstr))
|
2017-02-20 13:23:41 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2017-02-22 10:44:07 +00:00
|
|
|
if urlstr != expurl {
|
|
|
|
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()
|
|
|
|
|
|
|
|
var err error
|
2017-02-22 10:44:07 +00:00
|
|
|
expurl, exptitle := testdataDir+"/image.html", "this is title"
|
2017-02-20 13:23:41 +00:00
|
|
|
|
|
|
|
c := testAllocate(t, "")
|
|
|
|
defer c.Release()
|
|
|
|
|
2017-02-22 10:44:07 +00:00
|
|
|
err = c.Run(defaultContext, Navigate(expurl))
|
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
|
|
|
|
|
|
|
var title string
|
|
|
|
err = c.Run(defaultContext, Title(&title))
|
|
|
|
if err != nil {
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|