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-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 {
|
|
|
|
if err := Run(ctx, Tasks{
|
|
|
|
Navigate(testdataDir + "/" + test.file),
|
|
|
|
WaitVisible(test.waitID, ByID),
|
|
|
|
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-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-03-20 17:27:57 +00:00
|
|
|
if err := Run(ctx, Tasks{
|
|
|
|
WaitVisible(`#icon-brankas`, ByID), // for image.html
|
|
|
|
NavigationEntries(&index, &entries),
|
2017-02-20 13:23:41 +00:00
|
|
|
|
2019-03-20 17:27:57 +00:00
|
|
|
Navigate(testdataDir + "/form.html"),
|
|
|
|
WaitVisible(`#form`, ByID), // for form.html
|
|
|
|
}); err != nil {
|
2017-02-20 13:23:41 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var title string
|
2019-03-20 17:27:57 +00:00
|
|
|
if err := Run(ctx, Tasks{
|
|
|
|
NavigateToHistoryEntry(entries[index].ID),
|
|
|
|
WaitVisible(`#icon-brankas`, ByID), // for image.html
|
|
|
|
Title(&title),
|
|
|
|
}); 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
|
|
|
|
if err := Run(ctx, Tasks{
|
|
|
|
WaitVisible(`#form`, ByID), // for form.html
|
|
|
|
Title(&exptitle),
|
2017-02-20 13:23:41 +00:00
|
|
|
|
2019-03-20 17:27:57 +00:00
|
|
|
Navigate(testdataDir + "/image.html"),
|
|
|
|
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),
|
|
|
|
}); 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
|
|
|
|
if err := Run(ctx, Tasks{
|
|
|
|
WaitVisible(`#form`, ByID), // for form.html
|
2017-02-20 13:23:41 +00:00
|
|
|
|
2019-03-20 17:27:57 +00:00
|
|
|
Navigate(testdataDir + "/image.html"),
|
|
|
|
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),
|
|
|
|
}); 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-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
|
|
|
|
if err := Run(ctx, Tasks{
|
|
|
|
WaitVisible(`#form`, ByID), // for form.html
|
|
|
|
Title(&exptitle),
|
|
|
|
|
|
|
|
Reload(),
|
|
|
|
// TODO: rewrite test to change the content after a reload, so
|
|
|
|
// we can wait on a selector.
|
|
|
|
Sleep(10 * time.Millisecond),
|
|
|
|
WaitVisible(`#form`, ByID), // for form.html
|
|
|
|
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-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 buf []byte
|
2019-03-20 17:27:57 +00:00
|
|
|
if err := Run(ctx, Tasks{
|
|
|
|
WaitVisible(`#icon-brankas`, ByID), // for image.html
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
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-03-20 17:27:57 +00:00
|
|
|
if err := Run(ctx, Tasks{
|
|
|
|
WaitVisible(`#form`, ByID), // for form.html
|
|
|
|
Location(&urlstr),
|
|
|
|
}); 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-03-20 17:27:57 +00:00
|
|
|
if err := Run(ctx, Tasks{
|
|
|
|
WaitVisible(`#icon-brankas`, ByID), // for image.html
|
|
|
|
Title(&title),
|
|
|
|
}); 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
|
|
|
}
|
|
|
|
}
|