Stenciljs e2e 测试没有明显原因无法工作
render() 内的行:
<div class={{ 'modal-backdrop show': this.show, 'modal-backdrop hide': !this.show }}>
测试:
it('should display correctly', async () => {
const page = await newE2EPage();
await page.setContent('<my-component></my-component>');
let element = await page.find('my-component');
expect(element).not.toBeNull();
element = await page.find('div.modal-backdrop.hide');
expect(element).not.toBeNull();
});
问题描述: 我只提供了部分代码,不幸的是,由于保密原因我无法提供更多内容。不过我会尽力描述这个问题。项目中有两个组件,组件 A 的测试按其应有的方式工作。组件 B(上面提供)的测试则不然。虽然第一个期望通过,但第二个期望失败,因为它为空,但它不应该。
一些事实:
- 该项目可以毫无问题地构建、运行和使用。
- 单元测试按预期工作,包括对 render() 方法的测试。
- 代码本身并没有错,我已经测试、重新测试、再测试,它适用于其他组件,但不适用于这个组件。
- 虽然默认是 .hide,但我尝试过 .hide 和 .show,但都不起作用。
迄今为止最好的猜测: 由于代码的编写方式,我在使测试正常工作时遇到了很多问题。在运行测试时,许多对象未定义,这导致测试失败。从我尝试和测试的所有内容来看,我最好的理论是,由于某种原因,这个组件在木偶浏览器的上下文中失败了一半,只生成了核心对象,但没有其他任何东西。我不知道这是否可能,但看起来就是这样。
Line within render() :
<div class={{ 'modal-backdrop show': this.show, 'modal-backdrop hide': !this.show }}>
Test :
it('should display correctly', async () => {
const page = await newE2EPage();
await page.setContent('<my-component></my-component>');
let element = await page.find('my-component');
expect(element).not.toBeNull();
element = await page.find('div.modal-backdrop.hide');
expect(element).not.toBeNull();
});
Description of the issue:
I have provided only a part of the code and unfortunately I cannot provide much more due to confidentiality. However I will do my best to describe the issue. There are two components in the project, tests for component A work as they should. Tests for component B (provided above) do not. While the first expect passes, the second one fails due to it being null but it shouldn't.
A few facts:
- The project can be built, run and used without a problem.
- Unit tests work as intended, including tests for the render() method.
- The code in it-self is not wrong, I have tested, retested and tested again and it works for other components but not for this one.
- Although the default is .hide, I have tried with both .hide and .show, neither work.
Best guess so far:
I have had many issues getting the tests to work due to how the code is written. While running tests many objects where undefined and that was causing the tests to fail. From everything that I tried and tested my best theory is that for some reason this component half fails in the context of the puppeteer browser, making the core object but nothing else. I don't know if that is possible but it looks like that.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Web 组件使用自己的文档树,称为
shadowDOM
,它在主 DOM (page
) 中不可见;因此你的page.find
失败了。这个概念称为封装。顺便说一句,我无法在 Puppeteer 文档中的page
对象;你能解释一下它是从哪里来的吗?要访问 Web 组件内的影子树,您必须使用
element.shadowRoot
来访问它:有 puppeteer 插件和应用程序可以帮助实现这一点:
要了解更多信息,请查看 https://www.google.com/search?q=puppeteer+shadow+DOM。
Web components use their own document tree called
shadowDOM
, which isn't visible from the main DOM (page
); thus yourpage.find
fails. This concept is called encapsulation. Btw, I wasn't able to find a method calledfind
on thepage
object in Puppeteer's documentation; can you explain where it comes from?To access the shadow tree inside a web component, you'll have to access it using
element.shadowRoot
:There's puppeteer addons and applications which can help with that:
To find more, check https://www.google.com/search?q=puppeteer+shadow+DOM.