我正在尝试访问ShadowRoot dom树元素,但我面临的问题是我无法使用Specific html元素来调用。ShadowRoot,因为该元素是动态的(请参阅下面的html片段id="vaadin-ext-field-error-0(。所以我唯一能做的就是访问一级以上的class.vadin文本字段容器。基本上,下面是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 nulln at fun (<anonymous>:1:46)n at <anonymous>:1:130n 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"]', '_.innerHTML'), 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) })(), error: {"type":"object","subtype":"error","className":"SyntaxError","description":"SyntaxError: missing ) after argument list","objectId":"{"injectedScriptId":3,"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'], error: {"type":"object","subtype":"error","className":"TypeError","description":"TypeError: Cannot read property 'textContent' of nulln at <anonymous>:1:53","objectId":"{"injectedScriptId":3,"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>
有人能调查一下吗;告诉我我做错了什么?谢谢Sandy
如果这个答案不能回答您的问题:https://stackoverflow.com/a/61742555/143475
请遵循此流程,以便我们能够在未来(以通用方式(为此添加支持:https://github.com/intuit/karate/tree/develop/examples/ui-test
我们真的需要社区的帮助,一定要考虑一下