英文:
Stencil's Puppeteer sometimes does not renders text or fonts
问题
以下是您要求的代码部分的翻译:
const componentStatesTemplate = `
<div style="display: flex; flex-direction: column;">
<nl-button>Button</nl-button>
<nl-button is-disabled>Button</nl-button>
<nl-button is-full-width>button</nl-button>
<nl-button><nl-icon-heart></nl-icon-heart></nl-button>
<nl-button><nl-icon-heart></nl-icon-heart>Button</nl-button>
<nl-button variant="secondary">Button</nl-button>
<nl-button variant="secondary"><nl-icon-heart></nl-icon-heart></nl-button>
<nl-button variant="secondary"><nl-icon-heart></nl-icon-heart>Button</nl-button>
</div>
`;
describe("button component", () => {
it("is rendered", async () => {
const page = await newE2EPage();
await page.setContent(html);
await page.addStyleTag({
content: `
@font-face {
font-family: "Source Sans 3";
src: url("${getAssetPath("../assets/fonts/SourceSans3-VariableFont_wght.ttf")}");
font-weight: 100 900;
}
body {
-webkit-font-smoothing: antialiased;
}
`,
});
const results = await page.compareScreenshot();
expect(results).toMatchScreenshot({allowableMismatchedPixels: E2EConstants.DEFAULT_allowableMismatchedPixels});
expect(results).toMatchScreenshot({allowableMismatchedRatio: E2EConstants.DEFAULT_allowableMismatchedRatio});
});
});
希望这有助于您理解代码的内容。如果您有任何其他问题,或需要进一步的翻译,请告诉我。
英文:
I'm trying to use Stencil e2e screenshot tests which have Puppeteer v10 under the hood.
I'm facing the problem with texts and fonts. Sometimes they are hidden and sometimes they appear as they should.
I'm using the following code for my tests:
const componentStatesTemplate = `
<div style="display: flex; flex-direction: column;">
<nl-button>Button</nl-button>
<nl-button is-disabled>Button</nl-button>
<nl-button is-full-width>button</nl-button>
<nl-button><nl-icon-heart></nl-icon-heart></nl-button>
<nl-button><nl-icon-heart></nl-icon-heart>Button</nl-button>
<nl-button variant="secondary">Button</nl-button>
<nl-button variant="secondary"><nl-icon-heart></nl-icon-heart></nl-button>
<nl-button variant="secondary"><nl-icon-heart></nl-icon-heart>Button</nl-button>
</div>
`;
describe("button component", () => {
it("is rendered", async () => {
const page = await newE2EPage();
await page.setContent(html);
await page.addStyleTag({
content: `
@font-face {
font-family: "Source Sans 3";
src: url("${getAssetPath("../assets/fonts/SourceSans3-VariableFont_wght.ttf")}");
font-weight: 100 900;
}
body {
-webkit-font-smoothing: antialiased;
}
`,
});
const results = await page.compareScreenshot();
expect(results).toMatchScreenshot({allowableMismatchedPixels: E2EConstants.DEFAULT_allowableMismatchedPixels});
expect(results).toMatchScreenshot({allowableMismatchedRatio: E2EConstants.DEFAULT_allowableMismatchedRatio});
});
});
Environment:
- I'm running tests in docker, using alpine linux with Chromium installed as described in Puppeteer docs.
- I'm also applying
--font-render-hinting=none
flag as described in many articles on the subject. - Using
waitForOptions.waitUntil
set tonetworkidle0
ordomcontentloaded
did not help as well.
However, the text in my components is not shown stable. Sometimes tests run as supposed and sometimes the components are rendered as if text was hidden.
How do I make them stable?
答案1
得分: 0
添加`await page.evaluateHandle("document.fonts.ready");` 有所帮助,现在字体可以正确加载。
示例:
const page = await newE2EPage();
await page.setContent(html);
await page.addStyleTag({
content: `
@font-face {
font-family: "Source Sans 3";
src: url("${getAssetPath("../assets/fonts/SourceSans3-VariableFont_wght.ttf")}");
font-weight: 100 900;
}
body {
-webkit-font-smoothing: antialiased;
}
`,
});
// 在比较元素之前插入等待
await page.evaluateHandle("document.fonts.ready");
const results = await page.compareScreenshot();
英文:
Adding await page.evaluateHandle("document.fonts.ready");
helped, now fonts load properly.
Example:
const page = await newE2EPage();
await page.setContent(html);
await page.addStyleTag({
content: `
@font-face {
font-family: "Source Sans 3";
src: url("${getAssetPath("../assets/fonts/SourceSans3-VariableFont_wght.ttf")}");
font-weight: 100 900;
}
body {
-webkit-font-smoothing: antialiased;
}
`,
});
// Inserting the wait before comparing elements
await page.evaluateHandle("document.fonts.ready");
const results = await page.compareScreenshot();
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论