简体   繁体   English

Stenciljs E2E 测试:如何在 Shadow Dom 中找到孩子的孩子

[英]Stenciljs E2E Testing: how to find a child of a child in the Shadow Dom

I have components arranged like so:我的组件排列如下:

app-home
  shadow-root
    component1
      shadow-root
        div id=div1

In other words both my app-home and component1 have shadow doms.换句话说,我的 app-home 和 component1 都有 shadow dom。

I can find component1 easily in Stenciljs E2E Tests like so:我可以在 Stenciljs E2E 测试中轻松找到 component1,如下所示:

const page = await newE2EPage();
await page.setContent('<app-home></app-home>');
const component1 = await page.find('app-home >>> component1');

But, whatever I've tried I cannot get #div1 inside component1.但是,无论我尝试过什么,我都无法在 component1 中获得 #div1。 It's important that I get back an E2EElement so I can use its methods such as click which trigger changes in the page.取回 E2EElement 很重要,这样我就可以使用它的方法,例如 click 触发页面中的更改。

Here's what I've tried:这是我尝试过的:

  1. page.find('app-home >>> component1 >>> #div1')

    Returns null返回空

  2. component1.find(':host >>> #div1') or component1.find(':root >>> #div1') or component1.find('>>> #div1') or component1.find('#div1') or component1.find('component1 >>> #div1') component1.find(':host >>> #div1')component1.find(':root >>> #div1')component1.find('>>> #div1')component1.find('#div1')component1.find('component1 >>> #div1')

    Returns null返回空

  3. component1.shadowRoot.querySelector('#div1')

    This method gets an element but clicking on it or dispatching an event to it has no effect on app-root or component1 in the page.此方法获取一个元素,但单击它或向它分派事件对页面中的 app-root 或 component1 没有影响。

Any suggestions then on how to find a child of a child when both elements have shadowDOMs?那么当两个元素都有shadowDOM时如何找到一个孩子的孩子有什么建议吗?

Note: I'm assuming that component1 is in reality a valid custom element tag name (containing a dash).注意:我假设component1实际上是一个有效的自定义元素标签名称(包含破折号)。


page.find('app-home >>> component1 >>> #div1')

Using the >>> multiple times is currently not possible (see source ).目前无法多次使用>>> (参见源代码)。 >>> is not an official CSS selector, so its implementation is entirely up to Stencil.js. >>>不是官方的 CSS 选择器,所以它的实现完全取决于 Stencil.js。


component1.find(':host >>> #div1')

This or your similar approaches are also not possible because calling .find on an E2EElement can only find elements that are children of that element, but not the host element itself.这种方法或您的类似方法也是不可能的,因为在.find上调用E2EElement只能找到作为该元素的子元素的元素,而不能找到宿主元素本身。


One solution is to switch entirely into the browser context:一种解决方案是完全切换到浏览器上下文:

await page.evaluate(() => {
  // this function will run in the browser context, not
  // in the Node.js context that the test is executed in.
  const appHome = document.querySelector('app-home');
  const comp1 = appHome.shadowRoot.querySelector('component1');
  const div1 = comp1.shadowRoot.querySelector('#div1');

  div1.click();
});

await page.waitForChanges();

You could refactor that into a helper, however I agree that it would be good if Stencil.js provided a proper API for this.您可以将其重构为帮助程序,但我同意如果 Stencil.js 为此提供适当的 API 会很好。 Allowing the >>> selector multiple times would be a nice feature.多次允许>>>选择器将是一个不错的功能。 I suggest you open a feature request in the Stencil.js repository.我建议您在 Stencil.js 存储库中打开一个功能请求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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