Removing external url on navigation and query actions.

This commit is contained in:
Randy Cahyana 2017-02-22 17:44:07 +07:00 committed by Kenneth Shaw
parent f9b98929b0
commit a4cd7f9783
5 changed files with 65 additions and 46 deletions

View File

@ -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()

View File

@ -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)
}
}

View File

@ -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)

3
testdata/form.html vendored
View File

@ -1,6 +1,7 @@
<!doctype html>
<html>
<head>
<title>this is form title</title>
<style>
input, textarea {
margin-top: 5px
@ -8,7 +9,7 @@
</style>
</head>
<body>
<form id="form" action="https://www.google.com/search" name="form">
<form id="form" action="image.html" name="form">
<span id="foo">insert</span> <span>keyword</span><br>
<input id="keyword" type="text" name="q" value="chromedp"/><br>
<input id="foo" type="text" name="foo" value="foo"/><br>

3
testdata/image.html vendored
View File

@ -1,5 +1,8 @@
<!doctype html>
<html>
<head>
<title>this is title</title>
</head>
<body style="background-color: #41a1e1">
<img id="icon-brankas" src="images/brankas.png" alt="Brankas - Easy Money Management">
<img id="icon-github" src="images/github.png" alt="How people build software">