2020-04-10 10:54:19 +00:00
|
|
|
package hunter
|
|
|
|
|
2020-04-13 10:04:13 +00:00
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/lestrrat-go/libxml2"
|
|
|
|
"github.com/tebeka/selenium"
|
|
|
|
)
|
2020-04-10 10:54:19 +00:00
|
|
|
|
|
|
|
type WebPreDriverUrl struct {
|
2020-04-13 10:04:13 +00:00
|
|
|
PreChromeUrl
|
2020-04-10 10:54:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (web *WebPreDriverUrl) Execute(cxt *TaskContext) {
|
|
|
|
resp, err := cxt.Hunt()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
cxt.SetShare("test", resp.Content())
|
2020-04-13 10:04:13 +00:00
|
|
|
wd := resp.GetResponse().(selenium.WebDriver)
|
|
|
|
ele, err := wd.FindElement(selenium.ByXPATH, "//title")
|
|
|
|
if err != nil {
|
|
|
|
log.Panic(err)
|
|
|
|
}
|
|
|
|
title, err := ele.GetAttribute("text")
|
|
|
|
if err != nil {
|
|
|
|
log.Panic(err)
|
|
|
|
}
|
|
|
|
cxt.SetShare("driver-title", title)
|
2020-04-10 10:54:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestDriver(t *testing.T) {
|
2020-04-13 10:04:13 +00:00
|
|
|
preurl := &WebPreDriverUrl{}
|
|
|
|
preurl.PreUrl = "http://httpbin.org"
|
|
|
|
|
|
|
|
hunter := NewHunter(preurl) // first params PreCurlUrl
|
2020-04-10 10:54:19 +00:00
|
|
|
hunter.Execute()
|
2020-04-13 10:04:13 +00:00
|
|
|
defer hunter.Stop()
|
|
|
|
|
|
|
|
content := hunter.GetShare("test").([]byte)
|
|
|
|
if content != nil {
|
|
|
|
doc, err := libxml2.ParseHTML(content)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
} else {
|
|
|
|
if result, err := doc.Find("//title"); err == nil {
|
|
|
|
iter := result.NodeIter()
|
|
|
|
if iter.Next() {
|
|
|
|
n := iter.Node()
|
|
|
|
if n.TextContent() != "httpbin.org" {
|
|
|
|
t.Error(n.TextContent())
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
t.Error("can't xpath title")
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
title := hunter.GetShare("driver-title").(string)
|
|
|
|
if title != "httpbin.org" {
|
|
|
|
t.Error(title)
|
|
|
|
}
|
2020-04-10 10:54:19 +00:00
|
|
|
}
|