mirror of
https://github.com/MontFerret/ferret.git
synced 2025-01-26 03:51:57 +02:00
Fixed dot notation for HTML elements (#239)
* Fixed dot notation for HTML elements
This commit is contained in:
parent
14dd7ac40b
commit
63dfcdeefd
@ -3,7 +3,7 @@ const NavLink = ReactRouterDOM.NavLink;
|
||||
|
||||
export default function Layout({ children }) {
|
||||
return e("div", { id: "layout"}, [
|
||||
e("nav", { className: "navbar navbar-expand-md navbar-dark bg-dark mb-4" }, [
|
||||
e("nav", { className: "navbar navbar-expand-md navbar-dark bg-dark mb-4", id: "navbar" }, [
|
||||
e(NavLink, { className: "navbar-brand", to: "/"}, "Ferret"),
|
||||
e("button", { className: "navbar-toggler", type: "button"}, [
|
||||
e("span", { className: "navbar-toggler-icon" })
|
||||
|
@ -1,8 +1,12 @@
|
||||
const e = React.createElement;
|
||||
|
||||
export default function IndexPage() {
|
||||
return e("div", { className: "jumbotron" }, [
|
||||
e("div", null, e("h1", null, "Welcome to Ferret E2E test page!")),
|
||||
e("div", null, e("p", { className: "lead" }, "It has several pages for testing different possibilities of the library"))
|
||||
return e("div", { className: "jumbotron", "data-type": "page", id: "index" }, [
|
||||
e("div", null,
|
||||
e("h1", null, "Welcome to Ferret E2E test page!")
|
||||
),
|
||||
e("div", null,
|
||||
e("p", { className: "lead" }, "It has several pages for testing different possibilities of the library")
|
||||
)
|
||||
])
|
||||
}
|
File diff suppressed because it is too large
Load Diff
@ -2,25 +2,25 @@ LET url = @dynamic
|
||||
LET doc = DOCUMENT(url, true)
|
||||
|
||||
LET expected = `<!DOCTYPE html><html lang="en"><head>
|
||||
<meta charset="utf-8">
|
||||
<meta http-equiv="x-ua-compatible" content="ie=edge">
|
||||
<title>Ferret E2E SPA</title>
|
||||
<meta name="description" content="">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
|
||||
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
|
||||
<link rel="stylesheet" href="index.css">
|
||||
</head>
|
||||
<body class="text-center">
|
||||
<div id="root"><div id="layout"><nav class="navbar navbar-expand-md navbar-dark bg-dark mb-4"><a class="navbar-brand active" aria-current="page" href="/">Ferret</a><button class="navbar-toggler" type="button"><span class="navbar-toggler-icon"></span></button><div class="collapse navbar-collapse"><ul class="navbar-nav mr-auto"><li class="nav-item"><a class="nav-link" href="/forms">Forms</a></li><li class="nav-item"><a class="nav-link" href="/navigation">Navigation</a></li><li class="nav-item"><a class="nav-link" href="/events">Events</a></li></ul></div></nav><main class="container"><div class="jumbotron"><div><h1>Welcome to Ferret E2E test page!</h1></div><div><p class="lead">It has several pages for testing different possibilities of the library</p></div></div></main></div></div>
|
||||
<script src="https://unpkg.com/react@16.6.1/umd/react.production.min.js"></script>
|
||||
<script src="https://unpkg.com/react-dom@16.6.1/umd/react-dom.production.min.js"></script>
|
||||
<script src="https://unpkg.com/history@4.7.2/umd/history.min.js"></script>
|
||||
<script src="https://unpkg.com/react-router@4.3.1/umd/react-router.js"></script>
|
||||
<script src="https://unpkg.com/react-router-dom@4.3.1/umd/react-router-dom.js"></script>
|
||||
<script src="index.js" type="module"></script>
|
||||
<meta charset="utf-8">
|
||||
<meta http-equiv="x-ua-compatible" content="ie=edge">
|
||||
<title>Ferret E2E SPA</title>
|
||||
<meta name="description" content="">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
|
||||
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
|
||||
<link rel="stylesheet" href="index.css">
|
||||
</head>
|
||||
<body class="text-center">
|
||||
<div id="root"><div id="layout"><nav class="navbar navbar-expand-md navbar-dark bg-dark mb-4" id="navbar"><a class="navbar-brand active" aria-current="page" href="/">Ferret</a><button class="navbar-toggler" type="button"><span class="navbar-toggler-icon"></span></button><div class="collapse navbar-collapse"><ul class="navbar-nav mr-auto"><li class="nav-item"><a class="nav-link" href="/forms">Forms</a></li><li class="nav-item"><a class="nav-link" href="/navigation">Navigation</a></li><li class="nav-item"><a class="nav-link" href="/events">Events</a></li></ul></div></nav><main class="container"><div class="jumbotron" data-type="page" id="index"><div><h1>Welcome to Ferret E2E test page!</h1></div><div><p class="lead">It has several pages for testing different possibilities of the library</p></div></div></main></div></div>
|
||||
<script src="https://unpkg.com/react@16.6.1/umd/react.production.min.js"></script>
|
||||
<script src="https://unpkg.com/react-dom@16.6.1/umd/react-dom.production.min.js"></script>
|
||||
<script src="https://unpkg.com/history@4.7.2/umd/history.min.js"></script>
|
||||
<script src="https://unpkg.com/react-router@4.3.1/umd/react-router.js"></script>
|
||||
<script src="https://unpkg.com/react-router-dom@4.3.1/umd/react-router-dom.js"></script>
|
||||
<script src="index.js" type="module"></script>
|
||||
|
||||
|
||||
</body></html>`
|
||||
</body></html>`
|
||||
LET actual = INNER_HTML(doc)
|
||||
|
||||
LET r1 = '(\s|\")'
|
||||
|
13
e2e/tests/el_attrs.fql
Normal file
13
e2e/tests/el_attrs.fql
Normal file
@ -0,0 +1,13 @@
|
||||
LET url = @static + '/overview.html'
|
||||
LET doc = DOCUMENT(url)
|
||||
|
||||
LET el = ELEMENT(doc, "body > header > a")
|
||||
LET attrs = [
|
||||
el.attributes.class,
|
||||
el.attributes.href
|
||||
]
|
||||
|
||||
LET expected = '["navbar-brand mr-0 mr-md-2","http://getbootstrap.com/"]'
|
||||
LET actual = TO_STRING(attrs)
|
||||
|
||||
RETURN EXPECT(expected, actual)
|
13
e2e/tests/el_attrs_d.fql
Normal file
13
e2e/tests/el_attrs_d.fql
Normal file
@ -0,0 +1,13 @@
|
||||
LET url = @dynamic
|
||||
LET doc = DOCUMENT(url, { driver: "cdp" })
|
||||
|
||||
LET el = ELEMENT(doc, "#index")
|
||||
LET attrs = [
|
||||
el.attributes.class,
|
||||
el.attributes["data-type"]
|
||||
]
|
||||
|
||||
LET expected = '["jumbotron","page"]'
|
||||
LET actual = TO_STRING(attrs)
|
||||
|
||||
RETURN EXPECT(expected, actual)
|
@ -2,7 +2,7 @@ LET url = @dynamic
|
||||
LET doc = DOCUMENT(url, true)
|
||||
LET el = ELEMENT(doc, "#root")
|
||||
|
||||
LET expected = `<div id="layout"><nav class="navbar navbar-expand-md navbar-dark bg-dark mb-4"><a class="navbar-brand active" aria-current="page" href="/">Ferret</a><button class="navbar-toggler" type="button"><span class="navbar-toggler-icon"></span></button><div class="collapse navbar-collapse"><ul class="navbar-nav mr-auto"><li class="nav-item"><a class="nav-link" href="/forms">Forms</a></li><li class="nav-item"><a class="nav-link" href="/navigation">Navigation</a></li><li class="nav-item"><a class="nav-link" href="/events">Events</a></li></ul></div></nav><main class="container"><div class="jumbotron"><div><h1>Welcome to Ferret E2E test page!</h1></div><div><p class="lead">It has several pages for testing different possibilities of the library</p></div></div></main></div>`
|
||||
LET expected = `<div id="layout"><nav class="navbar navbar-expand-md navbar-dark bg-dark mb-4" id="navbar"><a class="navbar-brand active" aria-current="page" href="/">Ferret</a><button class="navbar-toggler" type="button"><span class="navbar-toggler-icon"></span></button><div class="collapse navbar-collapse"><ul class="navbar-nav mr-auto"><li class="nav-item"><a class="nav-link" href="/forms">Forms</a></li><li class="nav-item"><a class="nav-link" href="/navigation">Navigation</a></li><li class="nav-item"><a class="nav-link" href="/events">Events</a></li></ul></div></nav><main class="container"><div class="jumbotron" data-type="page" id="index"><div><h1>Welcome to Ferret E2E test page!</h1></div><div><p class="lead">It has several pages for testing different possibilities of the library</p></div></div></main></div>`
|
||||
LET actual = INNER_HTML(el)
|
||||
|
||||
LET r1 = '(\s|\")'
|
||||
|
@ -297,15 +297,17 @@ func (el *HTMLElement) Length() values.Int {
|
||||
return values.NewInt(len(el.children))
|
||||
}
|
||||
|
||||
func (el *HTMLElement) GetAttributes(ctx context.Context) core.Value {
|
||||
func (el *HTMLElement) GetAttributes(ctx context.Context) *values.Object {
|
||||
val, err := el.attributes.Read(ctx)
|
||||
|
||||
if err != nil {
|
||||
return values.None
|
||||
return values.NewObject()
|
||||
}
|
||||
|
||||
attrs := val.(*values.Object)
|
||||
|
||||
// returning shallow copy
|
||||
return val.Copy()
|
||||
return attrs.Copy().(*values.Object)
|
||||
}
|
||||
|
||||
func (el *HTMLElement) GetAttribute(ctx context.Context, name values.String) core.Value {
|
||||
|
@ -52,7 +52,13 @@ func GetInElement(ctx context.Context, el drivers.HTMLElement, path []core.Value
|
||||
case "value":
|
||||
return el.GetValue(ctx), nil
|
||||
case "attributes":
|
||||
return el.GetAttributes(ctx), nil
|
||||
attrs := el.GetAttributes(ctx)
|
||||
|
||||
if len(path) == 1 {
|
||||
return attrs, nil
|
||||
}
|
||||
|
||||
return values.GetIn(ctx, attrs, path[1:])
|
||||
default:
|
||||
return GetInNode(ctx, el, path)
|
||||
}
|
||||
@ -70,17 +76,16 @@ func GetInNode(ctx context.Context, node drivers.HTMLNode, path []core.Value) (c
|
||||
segment := path[0]
|
||||
st := segment.Type()
|
||||
|
||||
if st == types.Int {
|
||||
switch st {
|
||||
case types.Int:
|
||||
if nt == drivers.HTMLElementType || nt == drivers.HTMLDocumentType {
|
||||
re := node.(drivers.HTMLNode)
|
||||
|
||||
return re.GetChildNode(ctx, segment.(values.Int)), nil
|
||||
}
|
||||
|
||||
return values.GetIn(ctx, node, path[0:])
|
||||
}
|
||||
|
||||
if st == types.String {
|
||||
return values.GetIn(ctx, node, path[1:])
|
||||
case types.String:
|
||||
segment := segment.(values.String)
|
||||
|
||||
switch segment {
|
||||
@ -89,13 +94,19 @@ func GetInNode(ctx context.Context, node drivers.HTMLNode, path []core.Value) (c
|
||||
case "nodeName":
|
||||
return node.NodeName(), nil
|
||||
case "children":
|
||||
return node.GetChildNodes(ctx), nil
|
||||
children := node.GetChildNodes(ctx)
|
||||
|
||||
if len(path) == 1 {
|
||||
return children, nil
|
||||
}
|
||||
|
||||
return values.GetIn(ctx, children, path[1:])
|
||||
case "length":
|
||||
return node.Length(), nil
|
||||
default:
|
||||
return values.None, nil
|
||||
}
|
||||
default:
|
||||
return values.None, core.TypeError(st, types.Int, types.String)
|
||||
}
|
||||
|
||||
return values.None, core.TypeError(st, types.Int, types.String)
|
||||
}
|
||||
|
@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"hash/fnv"
|
||||
"strings"
|
||||
|
||||
"github.com/MontFerret/ferret/pkg/drivers"
|
||||
"github.com/MontFerret/ferret/pkg/drivers/common"
|
||||
@ -134,7 +135,7 @@ func (nd *HTMLElement) InnerHTML(_ context.Context) values.String {
|
||||
return values.NewString(h)
|
||||
}
|
||||
|
||||
func (nd *HTMLElement) GetAttributes(_ context.Context) core.Value {
|
||||
func (nd *HTMLElement) GetAttributes(_ context.Context) *values.Object {
|
||||
if nd.attrs == nil {
|
||||
nd.attrs = nd.parseAttrs()
|
||||
}
|
||||
@ -232,7 +233,7 @@ func (nd *HTMLElement) InnerHTMLBySelectorAll(_ context.Context, selector values
|
||||
|
||||
// TODO: log error
|
||||
if err == nil {
|
||||
arr.Push(values.NewString(str))
|
||||
arr.Push(values.NewString(strings.TrimSpace(str)))
|
||||
}
|
||||
})
|
||||
|
||||
|
@ -50,7 +50,7 @@ type (
|
||||
|
||||
SetValue(ctx context.Context, value core.Value) error
|
||||
|
||||
GetAttributes(ctx context.Context) core.Value
|
||||
GetAttributes(ctx context.Context) *values.Object
|
||||
|
||||
GetAttribute(ctx context.Context, name values.String) core.Value
|
||||
|
||||
|
@ -26,6 +26,12 @@ func Navigate(ctx context.Context, args ...core.Value) (core.Value, error) {
|
||||
return values.None, err
|
||||
}
|
||||
|
||||
err = core.ValidateType(args[1], types.String)
|
||||
|
||||
if err != nil {
|
||||
return values.None, err
|
||||
}
|
||||
|
||||
timeout := values.NewInt(defaultTimeout)
|
||||
|
||||
if len(args) > 2 {
|
||||
|
Loading…
x
Reference in New Issue
Block a user