我正在尝试做的事情:
我正在尝试获取我的故事书项目中每个元素示例的屏幕截图。我试图做到这一点的方式是点击元素,然后截图,点击下一个,截图等。
这是附加的代码:
test('no visual regression for button', async () => {
const selector = 'a[href*="?selectedKind=Buttons&selectedStory="]';
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto('http://localhost:8080');
let examples = await page.$$(selector);
await examples.map( async(example) => {
await example.click();
const screen = await page.screenshot();
expect(screen).toMatchImageSnapshot();
});
await browser.close();
});
但是当我运行此代码时,我收到以下错误:
Protocol error (Runtime.callFunctionOn): Target closed.
at Session._onClosed (../../node_modules/puppeteer/lib/Connection.js:209:23)
at Connection._onClose (../../node_modules/puppeteer/lib/Connection.js:116:15)
at Connection.dispose (../../node_modules/puppeteer/lib/Connection.js:121:10)
at Browser.close (../../node_modules/puppeteer/lib/Browser.js:60:22)
at Object.<anonymous>.test (__tests__/visual.spec.js:21:17)
at <anonymous>
at process._tickCallback (internal/process/next_tick.js:169:7)
我相信这是因为元素失去了它的上下文或类似的东西,我不知道用什么方法来解决这个问题。您能否提供更深入的解释或可能的解决方案?我觉得 API 文档一点用都没有。