简体   繁体   中英

ShadowRoot dom elements access issue in Karate UI

I am trying to access ShadowRoot dom tree elements, but the problem I face is I cannot use the Specific html element to call on .ShadowRoot, as that element is dynamic (see below html snippet id="vaadin-text-field-error-0). So only way I can do is by accessing the class .vaadin-text-field-container which is one level up. Basically, below is the html code snippet where it returns an "error message = 255 characters max" and in my KarateUI test I am trying to assert that error message. So far I tried the below options based on previous forum queries I read, but no luck I could never access the right element that returns the text "255 characters max" to do the assertion:

Try 1:

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

Output :

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}"}

Try 2:

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

Output:

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}"}

Try 3:

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

Output:

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 null\n    at <anonymous>:1:53","objectId":"{\"injectedScriptId\":3,\"id\":3}"}

HTML code snippet here:

<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>

Can someone please look into this & point me what I am doing wrong here? Thanks, Sandy

If this answer does not answer your question: https://stackoverflow.com/a/61742555/143475

Please follow this process, so that we can work on adding support for this (in a generic way) in the future: https://github.com/intuit/karate/tree/develop/examples/ui-test

We really need community help for some of these things, do consider it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM