Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
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
12 changes: 2 additions & 10 deletions packages/web-integration/src/puppeteer/agent-launcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,7 @@ export async function launchPuppeteerPage(
// prepare the environment
const ua = target.userAgent || defaultUA;
let width = defaultViewportWidth;
let preferMaximizedWindow = true;
if (target.viewportWidth) {
preferMaximizedWindow = false;
assert(
typeof target.viewportWidth === 'number',
'viewportWidth must be a number',
Expand All @@ -48,7 +46,6 @@ export async function launchPuppeteerPage(
}
let height = defaultViewportHeight;
if (target.viewportHeight) {
preferMaximizedWindow = false;
assert(
typeof target.viewportHeight === 'number',
'viewportHeight must be a number',
Expand All @@ -61,7 +58,6 @@ export async function launchPuppeteerPage(
}
let dpr = defaultViewportScale;
if (target.viewportScale) {
preferMaximizedWindow = false;
assert(
typeof target.viewportScale === 'number',
'viewportScale must be a number',
Expand All @@ -76,9 +72,7 @@ export async function launchPuppeteerPage(
};

const headed = preference?.headed || preference?.keepWindow;

// only maximize window in headed mode
preferMaximizedWindow = preferMaximizedWindow && !!headed;
const windowSizeArg = `--window-size=${width},${height + (headed ? 200 : 0)}`; // add 200px for the address bar in headed mode

// launch the browser
if (headed && process.env.CI === '1') {
Expand All @@ -94,9 +88,7 @@ export async function launchPuppeteerPage(
'--disable-features=PasswordLeakDetection',
'--disable-save-password-bubble',
`--user-agent="${ua}"`,
preferMaximizedWindow
? '--start-maximized'
: `--window-size=${width},${height + 200}`, // add 200px for the address bar
windowSizeArg,
];

launcherDebug(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import {
defaultViewportHeight,
defaultViewportWidth,
launchPuppeteerPage,
} from '@/puppeteer/agent-launcher';
import { beforeEach, describe, expect, it, vi } from 'vitest';

const { mockLaunch } = vi.hoisted(() => ({
mockLaunch: vi.fn(),
}));

const mockNewPage = vi.fn();
const browserMock = {
newPage: mockNewPage,
setCookie: vi.fn(),
close: vi.fn(),
};

const createPageMock = () => ({
setUserAgent: vi.fn().mockResolvedValue(undefined),
setViewport: vi.fn().mockResolvedValue(undefined),
goto: vi.fn().mockResolvedValue(undefined),
waitForNetworkIdle: vi.fn().mockResolvedValue(undefined),
});

vi.mock('puppeteer', () => ({
__esModule: true,
default: { launch: mockLaunch },
launch: mockLaunch,
}));

describe('launchPuppeteerPage', () => {
beforeEach(() => {
vi.clearAllMocks();
mockLaunch.mockResolvedValue(browserMock);
const page = createPageMock();
mockNewPage.mockResolvedValue(page as any);
});

it('uses default viewport window size for headed runs', async () => {
await launchPuppeteerPage({ url: 'https://example.com' }, { headed: true });

const args = mockLaunch.mock.calls[0][0].args;
expect(args).toContain(
`--window-size=${defaultViewportWidth},${defaultViewportHeight + 200}`,
);
expect(args).not.toContain('--start-maximized');
});

it('respects provided viewport dimensions for headed runs', async () => {
await launchPuppeteerPage(
{
url: 'https://example.com',
viewportWidth: 1000,
viewportHeight: 700,
},
{ headed: true },
);

const args = mockLaunch.mock.calls[0][0].args;
expect(args).toContain('--window-size=1000,900');
expect(args).not.toContain('--start-maximized');
});
});
Loading