Stencil的Puppeteer有时不渲染文本或字体。

huangapple go评论57阅读模式
英文:

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.

Stencil的Puppeteer有时不渲染文本或字体。

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 to networkidle0 or domcontentloaded 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();

huangapple
  • 本文由 发表于 2023年2月14日 19:33:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/75447235.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定