Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion docs/public-api/crawlee-playwright.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import type { BrowserPoolOptions } from '@crawlee/browser-pool';
import type { BrowserType } from 'playwright';
import { Cheerio } from 'cheerio';
import { CheerioAPI } from 'cheerio';
import type { CheerioCrawlerOptions } from '@crawlee/cheerio';
import { Configuration } from '@crawlee/browser';
import type { CrawlerRemoteBrowserOptions } from '@crawlee/browser';
import type { CrawlingContext } from '@crawlee/basic';
Expand Down Expand Up @@ -98,7 +99,7 @@ export interface AdaptivePlaywrightCrawlerContext<UserData extends Dictionary =
}

// @public (undocumented)
export interface AdaptivePlaywrightCrawlerOptions<ContextExtension = Dictionary<never>, ExtendedContext extends AdaptivePlaywrightCrawlerContext = AdaptivePlaywrightCrawlerContext & ContextExtension, Routes extends Record<keyof Routes, Dictionary> = Record<string, GetUserDataFromRequest_2<AdaptivePlaywrightCrawlerContext['request']>>, StatisticStateExtension extends AdaptivePlaywrightCrawlerStatisticState = AdaptivePlaywrightCrawlerStatisticState> extends Omit<BasicCrawlerOptions<AdaptivePlaywrightCrawlerContext, ContextExtension, ExtendedContext, Routes, StatisticStateExtension>, 'preNavigationHooks' | 'postNavigationHooks' | 'contextPipelineBuilder'>, Pick<PlaywrightCrawlerOptions, 'launchContext' | 'headless' | 'browserPool' | 'remoteBrowser'> {
export interface AdaptivePlaywrightCrawlerOptions<ContextExtension = Dictionary<never>, ExtendedContext extends AdaptivePlaywrightCrawlerContext = AdaptivePlaywrightCrawlerContext & ContextExtension, Routes extends Record<keyof Routes, Dictionary> = Record<string, GetUserDataFromRequest_2<AdaptivePlaywrightCrawlerContext['request']>>, StatisticStateExtension extends AdaptivePlaywrightCrawlerStatisticState = AdaptivePlaywrightCrawlerStatisticState> extends Omit<BasicCrawlerOptions<AdaptivePlaywrightCrawlerContext, ContextExtension, ExtendedContext, Routes, StatisticStateExtension>, 'preNavigationHooks' | 'postNavigationHooks' | 'contextPipelineBuilder'>, Pick<CheerioCrawlerOptions, 'ignoreTlsErrors' | 'additionalMimeTypes'>, Pick<PlaywrightCrawlerOptions, 'launchContext' | 'headless' | 'browserPool' | 'remoteBrowser' | 'ignoreIframes'> {
navigationTimeoutSecs?: number;
postNavigationHooks?: AdaptivePostNavigationHook<ContextExtension>[];
preNavigationHooks?: AdaptiveHook<ContextExtension>[];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import {
Statistics,
} from '@crawlee/basic';
import { CrawlingRequest, extractUrlsFromPage } from '@crawlee/browser';
import type { CheerioCrawlingContext } from '@crawlee/cheerio';
import type { CheerioCrawlerOptions, CheerioCrawlingContext } from '@crawlee/cheerio';
import { CheerioCrawler } from '@crawlee/cheerio';
import { ResponseWithUrl } from '@crawlee/http-client';
import type {
Expand Down Expand Up @@ -305,7 +305,11 @@ export interface AdaptivePlaywrightCrawlerOptions<
>,
'preNavigationHooks' | 'postNavigationHooks' | 'contextPipelineBuilder'
>,
Pick<PlaywrightCrawlerOptions, 'launchContext' | 'headless' | 'browserPool' | 'remoteBrowser'> {
Pick<CheerioCrawlerOptions, 'ignoreTlsErrors' | 'additionalMimeTypes'>,
Pick<
PlaywrightCrawlerOptions,
'launchContext' | 'headless' | 'browserPool' | 'remoteBrowser' | 'ignoreIframes'
> {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These new options will only work for their respective modes, right (static / browser)?

I can imagine this will be confusing for the users (e.g., with ignoreTlsErrors only being valid for the Cheerio part). Any chance we can at least document this?

/**
* Timeout for the navigation phase of each attempt, in seconds, shared by the `preNavigationHooks`, the
* navigation and the `postNavigationHooks`. Applies to both the HTTP-only and the browser attempt. When not
Expand Down Expand Up @@ -503,6 +507,9 @@ export class AdaptivePlaywrightCrawler<
headless,
browserPool,
remoteBrowser,
ignoreTlsErrors,
additionalMimeTypes,
ignoreIframes,
...rest
} = options;

Expand Down Expand Up @@ -609,6 +616,8 @@ export class AdaptivePlaywrightCrawler<
postNavigationHooks,
extendContext,
navigationTimeoutSecs,
ignoreTlsErrors,
additionalMimeTypes,
});

const browserCrawler = new PlaywrightCrawler({
Expand All @@ -622,6 +631,7 @@ export class AdaptivePlaywrightCrawler<
headless,
browserPool,
remoteBrowser,
ignoreIframes,
});

this.#staticCrawler = staticCrawler;
Expand Down
61 changes: 61 additions & 0 deletions test/core/crawlers/adaptive-playwright-crawler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
MemoryStorageBackend,
serviceLocator,
} from '@crawlee/core';
import { BaseHttpClient, FetchHttpClient } from '@crawlee/http-client';
import type {
AdaptivePlaywrightCrawlerContext,
AdaptivePlaywrightCrawlerOptions,
Expand Down Expand Up @@ -146,6 +147,10 @@ describe('AdaptivePlaywrightCrawler', () => {
res.status(200).type('text/plain').send('hello');
});

app.get('/iframe', (_req, res) => {
res.status(200).send(`<html><body><iframe srcdoc="<p id='inner'>inside</p>"></iframe></body></html>`);
});

app.get('/repeated-headers', (_req, res) => {
res.cookie('a', '1');
res.cookie('b', '2');
Expand Down Expand Up @@ -1679,6 +1684,62 @@ describe('AdaptivePlaywrightCrawler', () => {
);
});

test('forwards HTTP options to the inner CheerioCrawler', async () => {
const captured: (boolean | undefined)[] = [];
const fetchClient = new FetchHttpClient();
// Carries the BaseHttpClient prototype so the crawler's `z.instanceof` validation accepts it.
const capturingClient = Object.assign(Object.create(BaseHttpClient.prototype) as BaseHttpClient, {
sendRequest: async (request: globalThis.Request, options?: { ignoreTlsErrors?: boolean }) => {
captured.push(options?.ignoreTlsErrors);
return fetchClient.sendRequest(request, options);
},
});
const requestHandler = vi.fn();

const crawler = await makeOneshotCrawler(
{
httpClient: capturingClient,
ignoreTlsErrors: true,
additionalMimeTypes: ['text/plain'],
renderingTypePredictor: makeRiggedRenderingTypePredictor({
renderingType: 'static',
detectionProbabilityRecommendation: 0,
}),
// Surface the HTTP-only attempt's error instead of falling back to a browser.
shouldPropagateError: () => true,
requestHandler,
},
[`http://${HOSTNAME}:${port}/plain`],
);
await crawler.run();

expect(requestHandler).toHaveBeenCalledTimes(1);
expect(captured).toEqual([true]);
});

test.each([
[false, 1],
[true, 0],
])('forwards ignoreIframes: %s to the inner PlaywrightCrawler', async (ignoreIframes, expectedMatches) => {
const matches: number[] = [];
const crawler = await makeOneshotCrawler(
{
ignoreIframes,
renderingTypePredictor: makeRiggedRenderingTypePredictor({
renderingType: 'clientOnly',
detectionProbabilityRecommendation: 0,
}),
requestHandler: async ({ parseWithCheerio }) => {
matches.push((await parseWithCheerio())('#inner').length);
},
},
[`http://${HOSTNAME}:${port}/iframe`],
);
await crawler.run();

expect(matches).toEqual([expectedMatches]);
});

test('launchContext reaches the browser launched for the browser-rendering path', async () => {
const renderingTypePredictor = makeRiggedRenderingTypePredictor({
renderingType: 'clientOnly',
Expand Down
Loading