mirror of
https://github.com/MontFerret/ferret.git
synced 2025-01-04 03:02:02 +02:00
Added GetParentElement to HTMLElement struct (#571)
* Added GetParentElement to HTMLElement struct * Fixed linter issue * Fixed formatting
This commit is contained in:
parent
7eed93721c
commit
8629b57fa7
9
e2e/tests/dynamic/element/parent/get.fql
Normal file
9
e2e/tests/dynamic/element/parent/get.fql
Normal file
@ -0,0 +1,9 @@
|
||||
LET doc = DOCUMENT(@lab.cdn.dynamic + "/#/lists", { driver:"cdp" })
|
||||
|
||||
LET child = ELEMENT(doc, ".track-list")
|
||||
LET parent = child.parentElement
|
||||
|
||||
T::NOT::NONE(parent)
|
||||
T::EQ(parent.attributes.id, "tracks")
|
||||
|
||||
RETURN NONE
|
10
e2e/tests/static/element/parent/get.fql
Normal file
10
e2e/tests/static/element/parent/get.fql
Normal file
@ -0,0 +1,10 @@
|
||||
LET url = @lab.cdn.static + '/list.html'
|
||||
LET doc = DOCUMENT(url)
|
||||
|
||||
LET child = ELEMENT(doc, ".track-list")
|
||||
LET parent = child.parentElement
|
||||
|
||||
T::NOT::NONE(parent)
|
||||
T::EQ(parent.attributes.id, "tracks")
|
||||
|
||||
RETURN NONE
|
@ -575,15 +575,19 @@ func (el *HTMLElement) GetChildNode(ctx context.Context, idx values.Int) (core.V
|
||||
)
|
||||
}
|
||||
|
||||
func (el *HTMLElement) GetParentElement(ctx context.Context) (core.Value, error) {
|
||||
return el.evalAndGetElement(ctx, templates.GetParent())
|
||||
}
|
||||
|
||||
func (el *HTMLElement) GetPreviousElementSibling(ctx context.Context) (core.Value, error) {
|
||||
return el.getSibling(ctx, templates.GetPreviousElementSibling())
|
||||
return el.evalAndGetElement(ctx, templates.GetPreviousElementSibling())
|
||||
}
|
||||
|
||||
func (el *HTMLElement) GetNextElementSibling(ctx context.Context) (core.Value, error) {
|
||||
return el.getSibling(ctx, templates.GetNextElementSibling())
|
||||
return el.evalAndGetElement(ctx, templates.GetNextElementSibling())
|
||||
}
|
||||
|
||||
func (el *HTMLElement) getSibling(ctx context.Context, expr string) (core.Value, error) {
|
||||
func (el *HTMLElement) evalAndGetElement(ctx context.Context, expr string) (core.Value, error) {
|
||||
if el.IsDetached() {
|
||||
return values.None, drivers.ErrDetached
|
||||
}
|
||||
|
7
pkg/drivers/cdp/templates/parent.go
Normal file
7
pkg/drivers/cdp/templates/parent.go
Normal file
@ -0,0 +1,7 @@
|
||||
package templates
|
||||
|
||||
const getParent = "(el) => el.parentElement"
|
||||
|
||||
func GetParent() string {
|
||||
return getParent
|
||||
}
|
@ -198,6 +198,8 @@ func GetInElement(ctx context.Context, el drivers.HTMLElement, path []core.Value
|
||||
return el.GetPreviousElementSibling(ctx)
|
||||
case "nextElementSibling":
|
||||
return el.GetNextElementSibling(ctx)
|
||||
case "parentElement":
|
||||
return el.GetParentElement(ctx)
|
||||
default:
|
||||
return GetInNode(ctx, el, path)
|
||||
}
|
||||
|
@ -497,6 +497,16 @@ func (el *HTMLElement) Iterate(_ context.Context) (core.Iterator, error) {
|
||||
return common.NewIterator(el)
|
||||
}
|
||||
|
||||
func (el *HTMLElement) GetParentElement(_ context.Context) (core.Value, error) {
|
||||
parent := el.selection.Parent()
|
||||
|
||||
if parent == nil {
|
||||
return values.None, nil
|
||||
}
|
||||
|
||||
return NewHTMLElement(parent)
|
||||
}
|
||||
|
||||
func (el *HTMLElement) GetPreviousElementSibling(_ context.Context) (core.Value, error) {
|
||||
sibling := el.selection.Prev()
|
||||
|
||||
|
@ -97,6 +97,8 @@ type (
|
||||
|
||||
GetNextElementSibling(ctx context.Context) (core.Value, error)
|
||||
|
||||
GetParentElement(ctx context.Context) (core.Value, error)
|
||||
|
||||
Click(ctx context.Context, count values.Int) error
|
||||
|
||||
ClickBySelector(ctx context.Context, selector values.String, count values.Int) error
|
||||
|
Loading…
Reference in New Issue
Block a user