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) { func TestMain(m *testing.M) {
var err error 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(PoolLog(log.Printf, log.Printf, log.Printf))
pool, err = NewPool() pool, err = NewPool()

View File

@ -16,12 +16,14 @@ func TestNavigate(t *testing.T) {
c := testAllocate(t, "") c := testAllocate(t, "")
defer c.Release() 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 { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
err = c.Run(defaultContext, WaitVisible(`#hplogo`, ByID)) err = c.Run(defaultContext, WaitVisible(`#icon-brankas`, ByID))
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -31,8 +33,8 @@ func TestNavigate(t *testing.T) {
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
if !strings.HasPrefix(urlstr, "https://www.google.") { if !strings.HasPrefix(urlstr, expurl) {
t.Errorf("expected to be on google domain, at: %s", urlstr) t.Errorf("expected to be on image.html, at: %s", urlstr)
} }
var title string var title string
@ -40,7 +42,7 @@ func TestNavigate(t *testing.T) {
if err != nil { if err != nil {
t.Fatal(err) 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) t.Errorf("expected title to contain google, instead title is: %s", title)
} }
} }
@ -54,8 +56,8 @@ func TestNavigationEntries(t *testing.T) {
defer c.Release() defer c.Release()
tests := []string{ tests := []string{
"https://godoc.org/", "form.html",
"https://golang.org/", "image.html",
} }
var entries []*page.NavigationEntry var entries []*page.NavigationEntry
@ -75,7 +77,7 @@ func TestNavigationEntries(t *testing.T) {
expIdx, expEntries := 1, 2 expIdx, expEntries := 1, 2
for i, url := range tests { for i, url := range tests {
err = c.Run(defaultContext, Navigate(url)) err = c.Run(defaultContext, Navigate(testdataDir+"/"+url))
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -112,7 +114,7 @@ func TestNavigateToHistoryEntry(t *testing.T) {
var entries []*page.NavigationEntry var entries []*page.NavigationEntry
var index int64 var index int64
err = c.Run(defaultContext, Navigate("https://godoc.org/")) err = c.Run(defaultContext, Navigate(testdataDir+"/"+"image.html"))
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -127,7 +129,7 @@ func TestNavigateToHistoryEntry(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
err = c.Run(defaultContext, Navigate("https://golang.org/")) err = c.Run(defaultContext, Navigate(testdataDir+"/"+"form.html"))
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -165,7 +167,7 @@ func TestNavigateBack(t *testing.T) {
c := testAllocate(t, "") c := testAllocate(t, "")
defer c.Release() defer c.Release()
err = c.Run(defaultContext, Navigate("https://godoc.org/")) err = c.Run(defaultContext, Navigate(testdataDir+"/"+"form.html"))
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -175,13 +177,13 @@ func TestNavigateBack(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
var expTitle string var exptitle string
err = c.Run(defaultContext, Title(&expTitle)) err = c.Run(defaultContext, Title(&exptitle))
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
err = c.Run(defaultContext, Navigate("https://golang.org/")) err = c.Run(defaultContext, Navigate(testdataDir+"/"+"image.html"))
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -206,8 +208,8 @@ func TestNavigateBack(t *testing.T) {
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
if title != expTitle { if title != exptitle {
t.Errorf("expected title to be %s, instead title is: %s", expTitle, title) 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, "") c := testAllocate(t, "")
defer c.Release() defer c.Release()
err = c.Run(defaultContext, Navigate("https://godoc.org/")) err = c.Run(defaultContext, Navigate(testdataDir+"/"+"form.html"))
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -229,7 +231,7 @@ func TestNavigateForward(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
err = c.Run(defaultContext, Navigate("https://golang.org/")) err = c.Run(defaultContext, Navigate(testdataDir+"/"+"image.html"))
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -239,8 +241,8 @@ func TestNavigateForward(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
var expTitle string var exptitle string
err = c.Run(defaultContext, Title(&expTitle)) err = c.Run(defaultContext, Title(&exptitle))
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -270,8 +272,8 @@ func TestNavigateForward(t *testing.T) {
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
if title != expTitle { if title != exptitle {
t.Errorf("expected title to be %s, instead title is: %s", expTitle, title) 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, "") c := testAllocate(t, "")
defer c.Release() defer c.Release()
err = c.Run(defaultContext, Navigate("https://godoc.org/")) err = c.Run(defaultContext, Navigate(testdataDir+"/"+"form.html"))
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -302,7 +304,7 @@ func TestReload(t *testing.T) {
c := testAllocate(t, "") c := testAllocate(t, "")
defer c.Release() defer c.Release()
err = c.Run(defaultContext, Navigate("https://godoc.org/")) err = c.Run(defaultContext, Navigate(testdataDir+"/"+"form.html"))
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -312,8 +314,8 @@ func TestReload(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
var expTitle string var exptitle string
err = c.Run(defaultContext, Title(&expTitle)) err = c.Run(defaultContext, Title(&exptitle))
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -333,8 +335,8 @@ func TestReload(t *testing.T) {
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
if title != expTitle { if title != exptitle {
t.Errorf("expected title to be %s, instead title is: %s", expTitle, title) 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, "") c := testAllocate(t, "")
defer c.Release() defer c.Release()
err = c.Run(defaultContext, Navigate("https://godoc.org/")) err = c.Run(defaultContext, Navigate(testdataDir+"/"+"image.html"))
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -382,7 +384,7 @@ func TestAddOnLoadScript(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
err = c.Run(defaultContext, Navigate("https://godoc.org/")) err = c.Run(defaultContext, Navigate(testdataDir+"/"+"form.html"))
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -421,7 +423,7 @@ func TestRemoveOnLoadScript(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
err = c.Run(defaultContext, Navigate("https://godoc.org/")) err = c.Run(defaultContext, Navigate(testdataDir+"/"+"form.html"))
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -436,11 +438,12 @@ func TestLocation(t *testing.T) {
t.Parallel() t.Parallel()
var err error var err error
expurl := testdataDir + "/" + "form.html"
c := testAllocate(t, "") c := testAllocate(t, "")
defer c.Release() defer c.Release()
err = c.Run(defaultContext, Navigate("https://godoc.org/")) err = c.Run(defaultContext, Navigate(expurl))
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -450,14 +453,14 @@ func TestLocation(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
var url string var urlstr string
err = c.Run(defaultContext, Location(&url)) err = c.Run(defaultContext, Location(&urlstr))
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
if url != "https://godoc.org/" { if urlstr != expurl {
t.Fatalf("expected url to be https://godoc.org/ ,got: %s", url) t.Fatalf("expected to be on form.html,got: %s", urlstr)
} }
} }
@ -465,11 +468,12 @@ func TestTitle(t *testing.T) {
t.Parallel() t.Parallel()
var err error var err error
expurl, exptitle := testdataDir+"/image.html", "this is title"
c := testAllocate(t, "") c := testAllocate(t, "")
defer c.Release() defer c.Release()
err = c.Run(defaultContext, Navigate("https://godoc.org/")) err = c.Run(defaultContext, Navigate(expurl))
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -485,7 +489,7 @@ func TestTitle(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
if title != "GoDoc" { if title != exptitle {
t.Fatalf("expected title to be GoDoc, got: %s", title) t.Fatalf("expected title to be %s, got: %s", exptitle, title)
} }
} }

View File

@ -4,6 +4,7 @@ import (
"fmt" "fmt"
"reflect" "reflect"
"testing" "testing"
"time"
"github.com/knq/chromedp/cdp" "github.com/knq/chromedp/cdp"
"github.com/knq/chromedp/cdp/css" "github.com/knq/chromedp/cdp/css"
@ -601,7 +602,7 @@ func TestClick(t *testing.T) {
t.Fatalf("got error: %v", err) t.Fatalf("got error: %v", err)
} }
err = c.Run(defaultContext, WaitVisible("#logo", ByID)) err = c.Run(defaultContext, WaitVisible("#icon-brankas", ByID))
if err != nil { if err != nil {
t.Fatalf("got error: %v", err) t.Fatalf("got error: %v", err)
} }
@ -611,7 +612,7 @@ func TestClick(t *testing.T) {
if err != nil { if err != nil {
t.Fatalf("got error: %v", err) 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) 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) 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 { if err != nil {
t.Fatalf("got error: %v", err) t.Fatalf("got error: %v", err)
} }
@ -756,7 +757,7 @@ func TestSubmit(t *testing.T) {
if err != nil { if err != nil {
t.Fatalf("got error: %v", err) 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) 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") c := testAllocate(t, "js.html")
defer c.Release() defer c.Release()
err := c.Run(defaultContext, Sleep(time.Millisecond*50))
if err != nil {
t.Fatalf("got error: %v", err)
}
var styles []*css.ComputedProperty 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 { if err != nil {
t.Fatalf("got error: %v", err) t.Fatalf("got error: %v", err)
} }
@ -800,6 +806,11 @@ func TestComputedStyle(t *testing.T) {
t.Fatalf("got error: %v", err) 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)) err = c.Run(defaultContext, ComputedStyle(test.sel, &styles, test.by))
if err != nil { if err != nil {
t.Fatalf("got error: %v", err) t.Fatalf("got error: %v", err)

3
testdata/form.html vendored
View File

@ -1,6 +1,7 @@
<!doctype html> <!doctype html>
<html> <html>
<head> <head>
<title>this is form title</title>
<style> <style>
input, textarea { input, textarea {
margin-top: 5px margin-top: 5px
@ -8,7 +9,7 @@
</style> </style>
</head> </head>
<body> <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> <span id="foo">insert</span> <span>keyword</span><br>
<input id="keyword" type="text" name="q" value="chromedp"/><br> <input id="keyword" type="text" name="q" value="chromedp"/><br>
<input id="foo" type="text" name="foo" value="foo"/><br> <input id="foo" type="text" name="foo" value="foo"/><br>

3
testdata/image.html vendored
View File

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