【问题标题】:How to access Shadow Dom in Intern / Leadfoot如何在 Intern / Leadfoot 中访问 Shadow Dom
【发布时间】:2014-12-16 20:33:04
【问题描述】:

我正在尝试使用InternJS 对Google Polymer 项目进行功能测试。

Web-Components 部分如下所示:

<custom-element-one flex> 
    <custom-nested-element id="someId">
    </custom-nested-element>
</custom-element-one>

问题是我无法在测试中访问Shadow DOM:

return this.remote
.get(require.toUrl('http://localhost:8500/test.html'))
.then(pollUntil('return document.querySelector("custom-element-one").shadowRoot;', 20000))
.findByTagName('custom-element-one')
.getProperty('shadowRoot')
.then(function (doc) {
    console.log('1--------------------->>>>', doc);
    console.log('2--------------------->>>>', doc.findByTagName('custom-nested-element'));

    doc.findByTagName('custom-nested-element')
        .getAttribute('id')
        .then(function (doc) {
            console.log('3--------------------->>>>', doc);
        });
});

结果:

第一个日志返回以下内容:

1--------------------->>>> { _elementId: '8',
_session:
{ _sessionId: 'xxxx-xxxx-xxx',
_server:
{ url: 'http://localhost:4444/wd/hub/',
sessionConstructor: [Function: ProxiedSession] },
_capabilities:
{ applicationCacheEnabled: false, ...

2--------------------->>>> { cancel: [Function], then: [Function] }


Object #<Promise> has no method 'getAttribute'

欢迎提出任何建议。

我的猜测是 shadowRoot 还不是 leadFoot 库的一部分,并且无法访问嵌套上的影子 DOM

【问题讨论】:

  • 我没有测试过。 Web 组件测试器使用 Polymer.whenReady() 等待聚合物完成所有微任务。也许你可以试试看。我的学习清单上还有实习生
  • Intern 的pollUntil 就是这样的。它一直等到元素在 DOM 中准备好。该元素已在 DOM 中准备就绪,只是由于 shadowRoot 无法访问。

标签: javascript testing polymer intern


【解决方案1】:

这主要是 WebDriver 问题,而不是其他问题。 WebDriver 对 Shadow DOM 的支持非常有限。 (more)。

但作为一种解决方法,您可以使用pollUntil 来获取元素,然后获取它的任何属性或调用它的任何公开方法。

类似这样,如果你想测试id属性的值:

return this.remote
.get(require.toUrl('http://localhost:8500/test.html'))
.then(pollUntil(function () {
    if (document.querySelector('custom-element-one').shadowRoot) {
        return document.querySelector('custom-element-one').shadowRoot.querySelector('custom-nested-element').id;
    }
    return null;
}, [] , 20000))
.then(function (idValue) {
    assert.equal(idValue, 'someId');
});

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-08
  • 1970-01-01
  • 1970-01-01
  • 2021-01-04
  • 2023-03-18
相关资源
最近更新 更多