Skip to content
Merged
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: 12 additions & 0 deletions packages/playwright-core/src/cli/daemon/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -667,6 +667,17 @@ const unroute = declareCommand({
toolParams: ({ pattern }) => ({ pattern }),
});

const networkStateSet = declareCommand({
name: 'network-state-set',
description: 'Set the browser network state to online or offline',
category: 'network',
args: z.object({
state: z.enum(['online', 'offline']).describe('Set to "offline" to simulate offline mode, "online" to restore network connectivity'),
}),
toolName: 'browser_network_state_set',
toolParams: ({ state }) => ({ state }),
});

// Export

const screenshot = declareCommand({
Expand Down Expand Up @@ -950,6 +961,7 @@ const commandsArray: AnyCommandSchema[] = [
routeMock,
routeList,
unroute,
networkStateSet,

// config category
configPrint,
Expand Down
25 changes: 24 additions & 1 deletion packages/playwright-core/src/mcp/browser/tools/network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/

import { z } from '../../../mcpBundle';
import { defineTabTool } from './tool';
import { defineTool, defineTabTool } from './tool';

import type * as playwright from '../../../..';

Expand Down Expand Up @@ -83,7 +83,30 @@ export async function renderRequest(request: playwright.Request): Promise<string
return result.join(' ');
}

const networkStateSet = defineTool({
capability: 'network',

schema: {
name: 'browser_network_state_set',
title: 'Set network state',
description: 'Sets the browser network state to online or offline. When offline, all network requests will fail.',
inputSchema: z.object({
state: z.enum(['online', 'offline']).describe('Set to "offline" to simulate offline mode, "online" to restore network connectivity'),
}),
type: 'action',
},

handle: async (context, params, response) => {
const browserContext = await context.ensureBrowserContext();
const offline = params.state === 'offline';
await browserContext.setOffline(offline);
response.addTextResult(`Network is now ${params.state}`);
response.addCode(`await page.context().setOffline(${offline});`);
},
});

export default [
requests,
networkClear,
networkStateSet,
];
29 changes: 29 additions & 0 deletions tests/mcp/cli-network-offline.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* Copyright (c) Microsoft Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { test, expect } from './cli-fixtures';

test('network-state-set toggles offline state', async ({ cli, server }) => {
await cli('open', server.EMPTY_PAGE);

// Set offline
const { output: offlineOutput } = await cli('network-state-set', 'offline');
expect(offlineOutput).toContain('offline');

// Restore online
const { output: onlineOutput } = await cli('network-state-set', 'online');
expect(onlineOutput).toContain('online');
});
99 changes: 99 additions & 0 deletions tests/mcp/network-offline.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/**
* Copyright (c) Microsoft Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { test, expect, parseResponse } from './fixtures';

// Enable the 'network' capability for context-level tools
test.use({
mcpCaps: ['network'],
});

test('browser_network_state_set sets network to offline', async ({ client }) => {
// Set offline
const setResponse = parseResponse(await client.callTool({
name: 'browser_network_state_set',
arguments: { state: 'offline' },
}));
expect(setResponse.result).toContain('Network is now offline');
});

test('browser_network_state_set restores network to online', async ({ client }) => {
// Set offline first
await client.callTool({
name: 'browser_network_state_set',
arguments: { state: 'offline' },
});

// Restore online
const setResponse = parseResponse(await client.callTool({
name: 'browser_network_state_set',
arguments: { state: 'online' },
}));
expect(setResponse.result).toContain('Network is now online');
});

test('network requests fail when offline', async ({ client, server }) => {
// Navigate to a page first
await client.callTool({
name: 'browser_navigate',
arguments: { url: server.EMPTY_PAGE },
});

// Set offline
await client.callTool({
name: 'browser_network_state_set',
arguments: { state: 'offline' },
});

// Try to navigate - should fail
// Error messages vary by browser:
// - Chrome: net::ERR_INTERNET_DISCONNECTED
// - Firefox: NS_ERROR_OFFLINE
// - WebKit: WebKit encountered an internal error
expect(await client.callTool({
name: 'browser_navigate',
arguments: { url: server.PREFIX + '/one-style.html' },
})).toHaveResponse({
isError: true,
error: expect.stringMatching(/net::ERR_INTERNET_DISCONNECTED|NS_ERROR_OFFLINE|WebKit encountered an internal error/),
});
});

test('network requests succeed after restoring online', async ({ client, server }) => {
// Navigate to initial page
await client.callTool({
name: 'browser_navigate',
arguments: { url: server.EMPTY_PAGE },
});

// Set offline then back online
await client.callTool({
name: 'browser_network_state_set',
arguments: { state: 'offline' },
});
await client.callTool({
name: 'browser_network_state_set',
arguments: { state: 'online' },
});

// Navigate should succeed
expect(await client.callTool({
name: 'browser_navigate',
arguments: { url: server.PREFIX + '/one-style.html' },
})).toHaveResponse({
isError: undefined,
});
});
Loading