mirror of
https://github.com/MontFerret/ferret.git
synced 2025-08-15 20:02:56 +02:00
Bugfix/http driver xpath (#725)
* Fixed XPath eval in http driver for attrribute values * Added unit test to cover http XPath func eval * Added integration test for covering XPath by CDP driver
This commit is contained in:
8
e2e/tests/dynamic/element/xpath/attrs.fql
Normal file
8
e2e/tests/dynamic/element/xpath/attrs.fql
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
LET url = @lab.cdn.dynamic
|
||||||
|
LET page = DOCUMENT(url, true)
|
||||||
|
|
||||||
|
LET actual = XPATH(page, "//body/@class")
|
||||||
|
|
||||||
|
T::NOT::EMPTY(actual)
|
||||||
|
|
||||||
|
RETURN T::EQ(actual[0], "text-center")
|
@@ -6,6 +6,7 @@ import (
|
|||||||
"github.com/MontFerret/ferret/pkg/drivers"
|
"github.com/MontFerret/ferret/pkg/drivers"
|
||||||
"github.com/MontFerret/ferret/pkg/drivers/http"
|
"github.com/MontFerret/ferret/pkg/drivers/http"
|
||||||
"github.com/MontFerret/ferret/pkg/runtime/values"
|
"github.com/MontFerret/ferret/pkg/runtime/values"
|
||||||
|
"github.com/MontFerret/ferret/pkg/runtime/values/types"
|
||||||
"github.com/PuerkitoBio/goquery"
|
"github.com/PuerkitoBio/goquery"
|
||||||
. "github.com/smartystreets/goconvey/convey"
|
. "github.com/smartystreets/goconvey/convey"
|
||||||
"testing"
|
"testing"
|
||||||
@@ -447,5 +448,55 @@ func TestElement(t *testing.T) {
|
|||||||
So(err, ShouldBeNil)
|
So(err, ShouldBeNil)
|
||||||
So(nt.String(), ShouldEqual, "[\"Album example for Bootstrap\"]")
|
So(nt.String(), ShouldEqual, "[\"Album example for Bootstrap\"]")
|
||||||
})
|
})
|
||||||
|
|
||||||
|
Convey("Func", func() {
|
||||||
|
buff := bytes.NewBuffer([]byte(doc))
|
||||||
|
|
||||||
|
buff.Write([]byte(doc))
|
||||||
|
|
||||||
|
doc, err := goquery.NewDocumentFromReader(buff)
|
||||||
|
|
||||||
|
So(err, ShouldBeNil)
|
||||||
|
|
||||||
|
el, err := http.NewHTMLElement(doc.Find("html"))
|
||||||
|
|
||||||
|
So(err, ShouldBeNil)
|
||||||
|
|
||||||
|
nt, err := el.XPath(context.Background(), values.NewString("count(//div)"))
|
||||||
|
|
||||||
|
So(err, ShouldBeNil)
|
||||||
|
So(nt.Type().String(), ShouldEqual, types.Float.String())
|
||||||
|
})
|
||||||
|
|
||||||
|
Convey("Attributes", func() {
|
||||||
|
buff := bytes.NewBuffer([]byte(`<!DOCTYPE html><body><div><a title="30"/></div></body></html>`))
|
||||||
|
godoc, err := goquery.NewDocumentFromReader(buff)
|
||||||
|
So(err, ShouldBeNil)
|
||||||
|
|
||||||
|
doc, err := http.NewRootHTMLDocument(godoc, "localhost:9090")
|
||||||
|
So(err, ShouldBeNil)
|
||||||
|
|
||||||
|
nt, err := doc.XPath(context.Background(), values.NewString("//a/@title"))
|
||||||
|
|
||||||
|
So(err, ShouldBeNil)
|
||||||
|
So(nt.Type().String(), ShouldEqual, types.Array.String())
|
||||||
|
So(nt.(*values.Array).First().Type().String(), ShouldEqual, types.String.String())
|
||||||
|
So(nt.(*values.Array).First().String(), ShouldEqual, "30")
|
||||||
|
})
|
||||||
|
|
||||||
|
Convey("Element node", func() {
|
||||||
|
buff := bytes.NewBuffer([]byte(`<!DOCTYPE html><body><div><a title="30"/></div></body></html>`))
|
||||||
|
godoc, err := goquery.NewDocumentFromReader(buff)
|
||||||
|
So(err, ShouldBeNil)
|
||||||
|
|
||||||
|
doc, err := http.NewRootHTMLDocument(godoc, "localhost:9090")
|
||||||
|
So(err, ShouldBeNil)
|
||||||
|
|
||||||
|
nt, err := doc.XPath(context.Background(), values.NewString("//div"))
|
||||||
|
|
||||||
|
So(err, ShouldBeNil)
|
||||||
|
So(nt.Type().String(), ShouldEqual, types.Array.String())
|
||||||
|
So(nt.(*values.Array).First().Type().String(), ShouldEqual, drivers.HTMLElementType.String())
|
||||||
|
})
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@@ -84,12 +84,15 @@ func EvalXPathTo(selection *goquery.Selection, expression string) (core.Value, e
|
|||||||
for res.MoveNext() {
|
for res.MoveNext() {
|
||||||
var item core.Value
|
var item core.Value
|
||||||
|
|
||||||
node := res.Current().(*htmlquery.NodeNavigator).Current()
|
node := res.Current()
|
||||||
|
|
||||||
if node.Type == html.TextNode {
|
switch node.NodeType() {
|
||||||
item = values.NewString(node.Data)
|
case xpath.TextNode:
|
||||||
} else {
|
item = values.NewString(node.Value())
|
||||||
i, err := parseXPathNode(node)
|
case xpath.AttributeNode:
|
||||||
|
item = values.NewString(node.Value())
|
||||||
|
default:
|
||||||
|
i, err := parseXPathNode(node.(*htmlquery.NodeNavigator).Current())
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
Reference in New Issue
Block a user