javascript - Access Web Component's local DOM (from outside) and set value to its children - Stack Overflow

I have a web app which is developed using Polymer and want to implement end-to-end testing using JavaSc

I have a web app which is developed using Polymer and want to implement end-to-end testing using JavaScript. That's why I am trying to programmatically set some value, but I'm unable to access the element's local DOM by either:

document.getElementById('nativeInput'); 
document.querySelector('#nativeInput');

Someone told me that it is not possible to access shadow-root elements directly and set a value on them. So my basic question is: How can I access an element's shadow DOM and set some value to its children?. Please let me know whether that is possible and if so, please share some solution.

See the screenshot to get a clearer understanding of what I am trying to acplish, on the HTML element with my JavaScript selector, on #shadow-root (open).

I have a web app which is developed using Polymer and want to implement end-to-end testing using JavaScript. That's why I am trying to programmatically set some value, but I'm unable to access the element's local DOM by either:

document.getElementById('nativeInput'); 
document.querySelector('#nativeInput');

Someone told me that it is not possible to access shadow-root elements directly and set a value on them. So my basic question is: How can I access an element's shadow DOM and set some value to its children?. Please let me know whether that is possible and if so, please share some solution.

See the screenshot to get a clearer understanding of what I am trying to acplish, on the HTML element with my JavaScript selector, on #shadow-root (open).

Share Improve this question edited Nov 28, 2017 at 11:12 craPkit 6531 gold badge8 silver badges20 bronze badges asked Nov 27, 2017 at 17:45 A l w a y s S u n n yA l w a y s S u n n y 38.6k9 gold badges66 silver badges117 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 3

If you want to send some value with javascript outside of polymer element, you may add at root level, like you may bind value into polymer. Sometine like :

index.html

<dom-bind>
<template> 
  <my-app my-prop = "{{myProp}}" ></my-app>
</template>
</dom-bind>
<script>
  var bindVal = document.querySelector('dom-bind');
  bindVal.myProp = "Test"
</script>

so in <my-app> element, you have a myProp property with "Test" value.

Since it was created in open mode you should be able to access it via node.shadowRoot. That said, shadow dom exists to isolate internal structures of aggregate elements, so you should first check if the iron-input custom element provides a public API which allows you to set the value.

Since you're mentioning end-to-end testing, I want to point out that Polymer provides its own, separate tool kit for that purpose.

Regardless of the technology/testing framework you'll end up with, take a look at the implementation of paper-input's tests, since they solve the exact problem you're facing. Understanding concepts like ponent lifecycle might be essential here.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745315906a4622218.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信