空手道UI中的ShadowRoot dom元素访问问题

问题描述

我正在尝试访问ShadowRoot dom树元素,但是我面临的问题是我无法使用Specific html元素调用.ShadowRoot,因为该元素是动态的(请参见下面的html片段id =“ vaadin-text-field -error-0)。因此,我唯一能做的就是访问上一级的.vaadin-text-field-container类。基本上,下面是HTML代码段,它返回“错误消息=最多255个字符” ”,然后在我的KarateUI测试中尝试声明该错误消息。到目前为止,我根据之前阅读的论坛查询尝试了以下选项,但是很幸运,我永远无法访问正确的元素,该元素返回文本“最大255个字符”做断言:

尝试1:

  * def test = script('.vaadin-text-field-container','_.innerHTML')
  * print "Error Message is" + test

输出

evaluation (js) Failed: script('.vaadin-text-field-container','_.innerHTML'),js eval Failed twice:(function(){ var fun = function(_){ return _.innerHTML }; var e = document.querySelector(".vaadin-text-field-container"); return fun(e) })(),error: {"type":"object","subtype":"error","className":"TypeError","description":"TypeError: Cannot read property 'innerHTML' of null\n    at fun (<anonymous>:1:46)\n    at <anonymous>:1:130\n    at <anonymous>:1:139","objectId":"{\"injectedScriptId\":3,\"id\":3}"}

尝试2:

  * def test = script('//*[@id="vaadin-text-field-error-0"]','_.innerHTML')
  * print 'Error Message is :' + test

输出

evaluation (js) Failed: script('//*[@id="vaadin-text-field-error-0"]',js eval Failed twice:(function(){ var fun = function(_){ return _.innerHTML }; var e = document.evaluate("//*[@id="vaadin-text-field-error-0"]",document,null,9,null).singleNodeValue; return fun(e) })(),"className":"SyntaxError","description":"SyntaxError: missing ) after argument list",\"id\":3}"}

尝试3:

* match text('#vaadin-text-field-error-0') == '255 characters max'

输出

evaluation (js) Failed: text('#vaadin-text-field-error-0'),js eval Failed twice:document.querySelector("#vaadin-text-field-error-0")['textContent'],"description":"TypeError: Cannot read property 'textContent' of null\n    at <anonymous>:1:53",\"id\":3}"}

此处的HTML代码段:

<vaadin-text-field class="standardformfield" id="runsetup" tabindex="0" required has-label has-helper has-value invalid has-error-message>
#shadow-root (open)
<div class="vaadin-text-field-container">

      <label part="label" id="vaadin-text-field-label-0">Run Name</label>

      <div part="input-field" id="vaadin-text-field-input-0">

        <slot name="prefix"></slot>

        <slot name="input">
          <input part="value" tabindex="0" aria-labelledby="vaadin-text-field-label-0 vaadin-text-field-input-0" placeholder="Type here..." required="" aria-describedby="vaadin-text-field-helper-0 vaadin-text-field-error-0" invalid="" aria-invalid="true">
        </slot>

        <div part="clear-button" id="clearButton" role="button" aria-label="Clear" hidden="true"></div>
        <slot name="suffix"></slot>

      </div>

      <div part="helper-text" id="vaadin-text-field-helper-0">
        <slot name="helper">No special characters,255 characters max</slot>
      </div>

      <div part="error-message" aria-live="assertive" aria-hidden="false" id="vaadin-text-field-error-0">255 characters max</div>

    </div>

</vaadin-text-field>

有人可以调查一下并指出我在这里做错了什么吗? 谢谢, 桑迪

解决方法

如果此答案不能回答您的问题:https://stackoverflow.com/a/61742555/143475

请遵循此过程,以便将来我们可以(以通用方式)添加对此的支持:https://github.com/intuit/karate/tree/develop/examples/ui-test

对于这些事情,我们确实需要社区的帮助,请考虑一下。