From 8d8afed51fa4b2e9308e1e74b78a9cb7a8b0870d Mon Sep 17 00:00:00 2001 From: Tim Voronov Date: Mon, 17 Jan 2022 10:24:27 -0500 Subject: [PATCH] Fixed element not found error (#724) --- .../element/query/element_not_found_by_css.fql | 8 ++++++++ .../element/query/element_not_found_by_xpath.fql | 8 ++++++++ .../dynamic/element/siblings/next_not_found.fql | 8 ++++++++ .../dynamic/element/siblings/prev_not_found.fql | 8 ++++++++ pkg/drivers/cdp/eval/runtime.go | 6 +++++- pkg/drivers/cdp/templates/query.go | 12 +++--------- 6 files changed, 40 insertions(+), 10 deletions(-) create mode 100644 e2e/tests/dynamic/element/query/element_not_found_by_css.fql create mode 100644 e2e/tests/dynamic/element/query/element_not_found_by_xpath.fql create mode 100644 e2e/tests/dynamic/element/siblings/next_not_found.fql create mode 100644 e2e/tests/dynamic/element/siblings/prev_not_found.fql diff --git a/e2e/tests/dynamic/element/query/element_not_found_by_css.fql b/e2e/tests/dynamic/element/query/element_not_found_by_css.fql new file mode 100644 index 00000000..2e5d4983 --- /dev/null +++ b/e2e/tests/dynamic/element/query/element_not_found_by_css.fql @@ -0,0 +1,8 @@ +LET url = @lab.cdn.dynamic +LET doc = DOCUMENT(url, { driver: "cdp" }) + +LET el = ELEMENT(doc, "#do-not-exist") + +T::NONE(el) + +RETURN TRUE diff --git a/e2e/tests/dynamic/element/query/element_not_found_by_xpath.fql b/e2e/tests/dynamic/element/query/element_not_found_by_xpath.fql new file mode 100644 index 00000000..5ee8c35c --- /dev/null +++ b/e2e/tests/dynamic/element/query/element_not_found_by_xpath.fql @@ -0,0 +1,8 @@ +LET url = @lab.cdn.dynamic +LET doc = DOCUMENT(url, true) + +LET el = ELEMENT(doc, X("//*[@id='do-not-exist']"))? + +T::NONE(el) + +RETURN TRUE diff --git a/e2e/tests/dynamic/element/siblings/next_not_found.fql b/e2e/tests/dynamic/element/siblings/next_not_found.fql new file mode 100644 index 00000000..575088fe --- /dev/null +++ b/e2e/tests/dynamic/element/siblings/next_not_found.fql @@ -0,0 +1,8 @@ +LET doc = DOCUMENT(@lab.cdn.dynamic + "/#/lists", { driver:"cdp" }) + +LET current = ELEMENT(doc, "body") +T::NOT::NONE(current) +LET next = current.nextElementSibling +T::NONE(next) + +RETURN NONE \ No newline at end of file diff --git a/e2e/tests/dynamic/element/siblings/prev_not_found.fql b/e2e/tests/dynamic/element/siblings/prev_not_found.fql new file mode 100644 index 00000000..6d16a880 --- /dev/null +++ b/e2e/tests/dynamic/element/siblings/prev_not_found.fql @@ -0,0 +1,8 @@ +LET doc = DOCUMENT(@lab.cdn.dynamic + "/#/lists", { driver:"cdp" }) + +LET current = ELEMENT(doc, 'head') +T::NOT::NONE(current) +LET prev = current.previousElementSibling +T::NONE(prev) + +RETURN NONE \ No newline at end of file diff --git a/pkg/drivers/cdp/eval/runtime.go b/pkg/drivers/cdp/eval/runtime.go index cfeb1163..844be884 100644 --- a/pkg/drivers/cdp/eval/runtime.go +++ b/pkg/drivers/cdp/eval/runtime.go @@ -98,13 +98,17 @@ func (rt *Runtime) EvalValue(ctx context.Context, fn *Function) (core.Value, err return rt.resolver.ToValue(ctx, out) } -func (rt *Runtime) EvalElement(ctx context.Context, fn *Function) (drivers.HTMLElement, error) { +func (rt *Runtime) EvalElement(ctx context.Context, fn *Function) (core.Value, error) { ref, err := rt.EvalRef(ctx, fn) if err != nil { return nil, err } + if ref.ObjectID == nil { + return values.None, nil + } + return rt.resolver.ToElement(ctx, ref) } diff --git a/pkg/drivers/cdp/templates/query.go b/pkg/drivers/cdp/templates/query.go index 382f4413..14e2e44d 100644 --- a/pkg/drivers/cdp/templates/query.go +++ b/pkg/drivers/cdp/templates/query.go @@ -14,27 +14,21 @@ const ( ) var ( - queryCSSSelector = fmt.Sprintf(` + queryCSSSelector = ` (el, selector) => { const found = el.querySelector(selector); - %s - return found; } - `, - notFoundErrorFragment, - ) + ` queryXPathSelector = fmt.Sprintf(` (el, selector) => { %s - - %s return found; } `, - xpathAsElementFragment, notFoundErrorFragment, + xpathAsElementFragment, ) )