diff --git a/chromedp_test.go b/chromedp_test.go index 4d082a1..fce39ec 100644 --- a/chromedp_test.go +++ b/chromedp_test.go @@ -57,7 +57,7 @@ func testAllocate(t *testing.T, path string) *Res { func TestMain(m *testing.M) { var err error - testdataDir = "file:" + os.Getenv("GOPATH") + "/src/github.com/knq/chromedp/testdata" + testdataDir = "file://" + os.Getenv("GOPATH") + "/src/github.com/knq/chromedp/testdata" //pool, err = NewPool(PoolLog(log.Printf, log.Printf, log.Printf)) pool, err = NewPool() diff --git a/nav_test.go b/nav_test.go index a3958aa..9f38ff9 100644 --- a/nav_test.go +++ b/nav_test.go @@ -16,12 +16,14 @@ func TestNavigate(t *testing.T) { c := testAllocate(t, "") defer c.Release() - err = c.Run(defaultContext, Navigate("https://www.google.com/")) + expurl, exptitle := testdataDir+"/image.html", "this is title" + + err = c.Run(defaultContext, Navigate(expurl)) if err != nil { t.Fatal(err) } - err = c.Run(defaultContext, WaitVisible(`#hplogo`, ByID)) + err = c.Run(defaultContext, WaitVisible(`#icon-brankas`, ByID)) if err != nil { t.Fatal(err) } @@ -31,8 +33,8 @@ func TestNavigate(t *testing.T) { if err != nil { t.Fatal(err) } - if !strings.HasPrefix(urlstr, "https://www.google.") { - t.Errorf("expected to be on google domain, at: %s", urlstr) + if !strings.HasPrefix(urlstr, expurl) { + t.Errorf("expected to be on image.html, at: %s", urlstr) } var title string @@ -40,7 +42,7 @@ func TestNavigate(t *testing.T) { if err != nil { t.Fatal(err) } - if !strings.Contains(strings.ToLower(title), "google") { + if title != exptitle { t.Errorf("expected title to contain google, instead title is: %s", title) } } @@ -54,8 +56,8 @@ func TestNavigationEntries(t *testing.T) { defer c.Release() tests := []string{ - "https://godoc.org/", - "https://golang.org/", + "form.html", + "image.html", } var entries []*page.NavigationEntry @@ -75,7 +77,7 @@ func TestNavigationEntries(t *testing.T) { expIdx, expEntries := 1, 2 for i, url := range tests { - err = c.Run(defaultContext, Navigate(url)) + err = c.Run(defaultContext, Navigate(testdataDir+"/"+url)) if err != nil { t.Fatal(err) } @@ -112,7 +114,7 @@ func TestNavigateToHistoryEntry(t *testing.T) { var entries []*page.NavigationEntry var index int64 - err = c.Run(defaultContext, Navigate("https://godoc.org/")) + err = c.Run(defaultContext, Navigate(testdataDir+"/"+"image.html")) if err != nil { t.Fatal(err) } @@ -127,7 +129,7 @@ func TestNavigateToHistoryEntry(t *testing.T) { t.Fatal(err) } - err = c.Run(defaultContext, Navigate("https://golang.org/")) + err = c.Run(defaultContext, Navigate(testdataDir+"/"+"form.html")) if err != nil { t.Fatal(err) } @@ -165,7 +167,7 @@ func TestNavigateBack(t *testing.T) { c := testAllocate(t, "") defer c.Release() - err = c.Run(defaultContext, Navigate("https://godoc.org/")) + err = c.Run(defaultContext, Navigate(testdataDir+"/"+"form.html")) if err != nil { t.Fatal(err) } @@ -175,13 +177,13 @@ func TestNavigateBack(t *testing.T) { t.Fatal(err) } - var expTitle string - err = c.Run(defaultContext, Title(&expTitle)) + var exptitle string + err = c.Run(defaultContext, Title(&exptitle)) if err != nil { t.Fatal(err) } - err = c.Run(defaultContext, Navigate("https://golang.org/")) + err = c.Run(defaultContext, Navigate(testdataDir+"/"+"image.html")) if err != nil { t.Fatal(err) } @@ -206,8 +208,8 @@ func TestNavigateBack(t *testing.T) { if err != nil { t.Fatal(err) } - if title != expTitle { - t.Errorf("expected title to be %s, instead title is: %s", expTitle, title) + if title != exptitle { + t.Errorf("expected title to be %s, instead title is: %s", exptitle, title) } } @@ -219,7 +221,7 @@ func TestNavigateForward(t *testing.T) { c := testAllocate(t, "") defer c.Release() - err = c.Run(defaultContext, Navigate("https://godoc.org/")) + err = c.Run(defaultContext, Navigate(testdataDir+"/"+"form.html")) if err != nil { t.Fatal(err) } @@ -229,7 +231,7 @@ func TestNavigateForward(t *testing.T) { t.Fatal(err) } - err = c.Run(defaultContext, Navigate("https://golang.org/")) + err = c.Run(defaultContext, Navigate(testdataDir+"/"+"image.html")) if err != nil { t.Fatal(err) } @@ -239,8 +241,8 @@ func TestNavigateForward(t *testing.T) { t.Fatal(err) } - var expTitle string - err = c.Run(defaultContext, Title(&expTitle)) + var exptitle string + err = c.Run(defaultContext, Title(&exptitle)) if err != nil { t.Fatal(err) } @@ -270,8 +272,8 @@ func TestNavigateForward(t *testing.T) { if err != nil { t.Fatal(err) } - if title != expTitle { - t.Errorf("expected title to be %s, instead title is: %s", expTitle, title) + if title != exptitle { + t.Errorf("expected title to be %s, instead title is: %s", exptitle, title) } } @@ -283,7 +285,7 @@ func TestStop(t *testing.T) { c := testAllocate(t, "") defer c.Release() - err = c.Run(defaultContext, Navigate("https://godoc.org/")) + err = c.Run(defaultContext, Navigate(testdataDir+"/"+"form.html")) if err != nil { t.Fatal(err) } @@ -302,7 +304,7 @@ func TestReload(t *testing.T) { c := testAllocate(t, "") defer c.Release() - err = c.Run(defaultContext, Navigate("https://godoc.org/")) + err = c.Run(defaultContext, Navigate(testdataDir+"/"+"form.html")) if err != nil { t.Fatal(err) } @@ -312,8 +314,8 @@ func TestReload(t *testing.T) { t.Fatal(err) } - var expTitle string - err = c.Run(defaultContext, Title(&expTitle)) + var exptitle string + err = c.Run(defaultContext, Title(&exptitle)) if err != nil { t.Fatal(err) } @@ -333,8 +335,8 @@ func TestReload(t *testing.T) { if err != nil { t.Fatal(err) } - if title != expTitle { - t.Errorf("expected title to be %s, instead title is: %s", expTitle, title) + if title != exptitle { + t.Errorf("expected title to be %s, instead title is: %s", exptitle, title) } } @@ -346,7 +348,7 @@ func TestCaptureScreenshot(t *testing.T) { c := testAllocate(t, "") defer c.Release() - err = c.Run(defaultContext, Navigate("https://godoc.org/")) + err = c.Run(defaultContext, Navigate(testdataDir+"/"+"image.html")) if err != nil { t.Fatal(err) } @@ -382,7 +384,7 @@ func TestAddOnLoadScript(t *testing.T) { t.Fatal(err) } - err = c.Run(defaultContext, Navigate("https://godoc.org/")) + err = c.Run(defaultContext, Navigate(testdataDir+"/"+"form.html")) if err != nil { t.Fatal(err) } @@ -421,7 +423,7 @@ func TestRemoveOnLoadScript(t *testing.T) { t.Fatal(err) } - err = c.Run(defaultContext, Navigate("https://godoc.org/")) + err = c.Run(defaultContext, Navigate(testdataDir+"/"+"form.html")) if err != nil { t.Fatal(err) } @@ -436,11 +438,12 @@ func TestLocation(t *testing.T) { t.Parallel() var err error + expurl := testdataDir + "/" + "form.html" c := testAllocate(t, "") defer c.Release() - err = c.Run(defaultContext, Navigate("https://godoc.org/")) + err = c.Run(defaultContext, Navigate(expurl)) if err != nil { t.Fatal(err) } @@ -450,14 +453,14 @@ func TestLocation(t *testing.T) { t.Fatal(err) } - var url string - err = c.Run(defaultContext, Location(&url)) + var urlstr string + err = c.Run(defaultContext, Location(&urlstr)) if err != nil { t.Fatal(err) } - if url != "https://godoc.org/" { - t.Fatalf("expected url to be https://godoc.org/ ,got: %s", url) + if urlstr != expurl { + t.Fatalf("expected to be on form.html,got: %s", urlstr) } } @@ -465,11 +468,12 @@ func TestTitle(t *testing.T) { t.Parallel() var err error + expurl, exptitle := testdataDir+"/image.html", "this is title" c := testAllocate(t, "") defer c.Release() - err = c.Run(defaultContext, Navigate("https://godoc.org/")) + err = c.Run(defaultContext, Navigate(expurl)) if err != nil { t.Fatal(err) } @@ -485,7 +489,7 @@ func TestTitle(t *testing.T) { t.Fatal(err) } - if title != "GoDoc" { - t.Fatalf("expected title to be GoDoc, got: %s", title) + if title != exptitle { + t.Fatalf("expected title to be %s, got: %s", exptitle, title) } } diff --git a/query_test.go b/query_test.go index 01cb3b4..1bcf36d 100644 --- a/query_test.go +++ b/query_test.go @@ -4,6 +4,7 @@ import ( "fmt" "reflect" "testing" + "time" "github.com/knq/chromedp/cdp" "github.com/knq/chromedp/cdp/css" @@ -601,7 +602,7 @@ func TestClick(t *testing.T) { t.Fatalf("got error: %v", err) } - err = c.Run(defaultContext, WaitVisible("#logo", ByID)) + err = c.Run(defaultContext, WaitVisible("#icon-brankas", ByID)) if err != nil { t.Fatalf("got error: %v", err) } @@ -611,7 +612,7 @@ func TestClick(t *testing.T) { if err != nil { t.Fatalf("got error: %v", err) } - if title != "chromedp - Google Search" { + if title != "this is title" { t.Errorf("expected title to be 'chromedp - Google Search', got: '%s'", title) } }) @@ -746,7 +747,7 @@ func TestSubmit(t *testing.T) { t.Fatalf("got error: %v", err) } - err = c.Run(defaultContext, WaitVisible("#logo > img", ByQuery)) + err = c.Run(defaultContext, WaitVisible("#icon-brankas", ByID)) if err != nil { t.Fatalf("got error: %v", err) } @@ -756,7 +757,7 @@ func TestSubmit(t *testing.T) { if err != nil { t.Fatalf("got error: %v", err) } - if title != "chromedp - Google Search" { + if title != "this is title" { t.Errorf("expected title to be 'chromedp - Google Search', got: '%s'", title) } }) @@ -781,8 +782,13 @@ func TestComputedStyle(t *testing.T) { c := testAllocate(t, "js.html") defer c.Release() + err := c.Run(defaultContext, Sleep(time.Millisecond*50)) + if err != nil { + t.Fatalf("got error: %v", err) + } + var styles []*css.ComputedProperty - err := c.Run(defaultContext, ComputedStyle(test.sel, &styles, test.by)) + err = c.Run(defaultContext, ComputedStyle(test.sel, &styles, test.by)) if err != nil { t.Fatalf("got error: %v", err) } @@ -800,6 +806,11 @@ func TestComputedStyle(t *testing.T) { t.Fatalf("got error: %v", err) } + err = c.Run(defaultContext, Sleep(time.Millisecond*50)) + if err != nil { + t.Fatalf("got error: %v", err) + } + err = c.Run(defaultContext, ComputedStyle(test.sel, &styles, test.by)) if err != nil { t.Fatalf("got error: %v", err) diff --git a/testdata/form.html b/testdata/form.html index 715b467..fc4cdf5 100644 --- a/testdata/form.html +++ b/testdata/form.html @@ -1,6 +1,7 @@ + this is form title -
+ insert keyword


diff --git a/testdata/image.html b/testdata/image.html index 910c9b5..010eebe 100644 --- a/testdata/image.html +++ b/testdata/image.html @@ -1,5 +1,8 @@ + + this is title + Brankas - Easy Money Management How people build software