From 8629b57fa7d13e06d16c12cf22ef369578a98808 Mon Sep 17 00:00:00 2001 From: Tim Voronov Date: Fri, 20 Nov 2020 23:09:12 -0500 Subject: [PATCH] Added GetParentElement to HTMLElement struct (#571) * Added GetParentElement to HTMLElement struct * Fixed linter issue * Fixed formatting --- e2e/tests/dynamic/element/parent/get.fql | 9 +++++++++ e2e/tests/static/element/parent/get.fql | 10 ++++++++++ pkg/drivers/cdp/dom/element.go | 10 +++++++--- pkg/drivers/cdp/templates/parent.go | 7 +++++++ pkg/drivers/common/getter.go | 2 ++ pkg/drivers/http/element.go | 10 ++++++++++ pkg/drivers/value.go | 2 ++ 7 files changed, 47 insertions(+), 3 deletions(-) create mode 100644 e2e/tests/dynamic/element/parent/get.fql create mode 100644 e2e/tests/static/element/parent/get.fql create mode 100644 pkg/drivers/cdp/templates/parent.go diff --git a/e2e/tests/dynamic/element/parent/get.fql b/e2e/tests/dynamic/element/parent/get.fql new file mode 100644 index 00000000..0049cf22 --- /dev/null +++ b/e2e/tests/dynamic/element/parent/get.fql @@ -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 \ No newline at end of file diff --git a/e2e/tests/static/element/parent/get.fql b/e2e/tests/static/element/parent/get.fql new file mode 100644 index 00000000..744f0890 --- /dev/null +++ b/e2e/tests/static/element/parent/get.fql @@ -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 \ No newline at end of file diff --git a/pkg/drivers/cdp/dom/element.go b/pkg/drivers/cdp/dom/element.go index 795b4643..2d5925b2 100644 --- a/pkg/drivers/cdp/dom/element.go +++ b/pkg/drivers/cdp/dom/element.go @@ -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 } diff --git a/pkg/drivers/cdp/templates/parent.go b/pkg/drivers/cdp/templates/parent.go new file mode 100644 index 00000000..ee466d1a --- /dev/null +++ b/pkg/drivers/cdp/templates/parent.go @@ -0,0 +1,7 @@ +package templates + +const getParent = "(el) => el.parentElement" + +func GetParent() string { + return getParent +} diff --git a/pkg/drivers/common/getter.go b/pkg/drivers/common/getter.go index 063ad735..3db55437 100644 --- a/pkg/drivers/common/getter.go +++ b/pkg/drivers/common/getter.go @@ -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) } diff --git a/pkg/drivers/http/element.go b/pkg/drivers/http/element.go index a9143520..9aa6d245 100644 --- a/pkg/drivers/http/element.go +++ b/pkg/drivers/http/element.go @@ -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() diff --git a/pkg/drivers/value.go b/pkg/drivers/value.go index 14032fa6..ab549f3c 100644 --- a/pkg/drivers/value.go +++ b/pkg/drivers/value.go @@ -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