Skip to content

Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.

feat(mcp): add network offline state tools (#39459)

Mat Stankiewicz6fd3cb4c45cb
diff --git a/packages/playwright-core/src/cli/daemon/commands.ts b/packages/playwright-core/src/cli/daemon/commands.ts
index eb6f58657b..9a289fa35d 100644
--- a/packages/playwright-core/src/cli/daemon/commands.ts
+++ b/packages/playwright-core/src/cli/daemon/commands.ts
@@ -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({
@@ -950,6 +961,7 @@ const commandsArray: AnyCommandSchema[] = [
routeMock,
routeList,
unroute,
+ networkStateSet,
// config category
configPrint,
diff --git a/packages/playwright-core/src/mcp/browser/tools/network.ts b/packages/playwright-core/src/mcp/browser/tools/network.ts
index b04c24e2b8..361d22fb0a 100644
--- a/packages/playwright-core/src/mcp/browser/tools/network.ts
+++ b/packages/playwright-core/src/mcp/browser/tools/network.ts
@@ -15,7 +15,7 @@
*/
import { z } from '../../../mcpBundle';
-import { defineTabTool } from './tool';
+import { defineTool, defineTabTool } from './tool';
import type * as playwright from '../../../..';
@@ -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,
];
diff --git a/tests/mcp/cli-network-offline.spec.ts b/tests/mcp/cli-network-offline.spec.ts
new file mode 100644
index 0000000000..082ae6de6f
--- /dev/null
+++ b/tests/mcp/cli-network-offline.spec.ts
@@ -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');
+});
diff --git a/tests/mcp/network-offline.spec.ts b/tests/mcp/network-offline.spec.ts
new file mode 100644
index 0000000000..bbe1f3d132
--- /dev/null
+++ b/tests/mcp/network-offline.spec.ts
@@ -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,
+ });
+});