fix(scrapeURL/fire-engine): perform format screenshot after specified actions (#1192)

This commit is contained in:
Gergő Móricz 2025-02-17 16:35:55 +01:00 committed by GitHub
parent 7ecbff3b20
commit 445b906af1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 29 additions and 5 deletions

View File

@ -88,5 +88,27 @@ describe("Scrape tests", () => {
const obj = JSON.parse(response.body.data.rawHtml);
expect(obj.id).toBe(1);
}, 25000); // TODO: mock and shorten
});
describe("Screenshot", () => {
it.concurrent("screenshot format works", async () => {
const response = await scrape({
url: "http://firecrawl.dev",
formats: ["screenshot"]
});
expectScrapeToSucceed(response);
expect(response.body.data.screenshot).toBeTruthy();
}, 15000);
it.concurrent("screenshot@fullPage format works", async () => {
const response = await scrape({
url: "http://firecrawl.dev",
formats: ["screenshot@fullPage"]
});
expectScrapeToSucceed(response);
expect(response.body.data.screenshot).toBeTruthy();
}, 15000);
})
});

View File

@ -167,6 +167,9 @@ export async function scrapeURLWithFireEngineChromeCDP(
]
: []),
// Include specified actions
...(meta.options.actions ?? []),
// Transform screenshot format into an action (unsupported by chrome-cdp)
...(meta.options.formats.includes("screenshot") ||
meta.options.formats.includes("screenshot@fullPage")
@ -177,9 +180,6 @@ export async function scrapeURLWithFireEngineChromeCDP(
},
]
: []),
// Include specified actions
...(meta.options.actions ?? []),
];
const totalWait = actions.reduce(
@ -228,8 +228,10 @@ export async function scrapeURLWithFireEngineChromeCDP(
"Transforming screenshots from actions into screenshot field",
{ screenshots: response.screenshots },
);
response.screenshot = (response.screenshots ?? [])[0];
(response.screenshots ?? []).splice(0, 1);
if (response.screenshots) {
response.screenshot = response.screenshots.slice(-1, 0)[0];
response.screenshots = response.screenshots.slice(0, -1);
}
meta.logger.debug("Screenshot transformation done", {
screenshots: response.screenshots,
screenshot: response.screenshot,