hunter/pre_driver_com_test.go

69 lines
1.3 KiB
Go
Raw Normal View History

2020-04-10 10:54:19 +00:00
package hunter
import (
"log"
"testing"
"github.com/lestrrat-go/libxml2"
"github.com/tebeka/selenium"
)
2020-04-10 10:54:19 +00:00
type WebPreDriverUrl struct {
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())
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) {
preurl := &WebPreDriverUrl{}
preurl.PreUrl = "http://httpbin.org"
hunter := NewHunter(preurl) // first params PreCurlUrl
2020-04-10 10:54:19 +00:00
hunter.Execute()
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
}