Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.
TypeScript automationchromechromiume2e-testingelectronend-to-end-testingfirefoxjavascriptplaywrighttesttest-automationtestingtesting-toolswebwebkit
feat(trace-viewer): Format XML bodies in network tab (#39405)
Chris414de1429ab0
diff --git a/packages/playwright-core/src/utils/isomorphic/mimeType.ts b/packages/playwright-core/src/utils/isomorphic/mimeType.tsindex 9db6ea9376..b58bd543bb 100644--- a/packages/playwright-core/src/utils/isomorphic/mimeType.ts+++ b/packages/playwright-core/src/utils/isomorphic/mimeType.ts@@ -18,6 +18,10 @@ export function isJsonMimeType(mimeType: string) {return !!mimeType.match(/^(application\/json|application\/.*?\+json|text\/(x-)?json)(;\s*charset=.*)?$/);}+export function isXmlMimeType(mimeType: string) {+ return !!mimeType.match(/^(application\/xml|application\/.*?\+xml|text\/xml)(;\s*charset=.*)?$/);+}+export function isTextualMimeType(mimeType: string) {return !!mimeType.match(/^(text\/.*?|application\/(json|(x-)?javascript|xml.*?|ecmascript|graphql|x-www-form-urlencoded)|image\/svg(\+xml)?|application\/.*?(\+json|\+xml))(;\s*charset=.*)?$/);}diff --git a/packages/trace-viewer/src/ui/networkResourceDetails.tsx b/packages/trace-viewer/src/ui/networkResourceDetails.tsxindex 855e7538aa..4ca5efc710 100644--- a/packages/trace-viewer/src/ui/networkResourceDetails.tsx+++ b/packages/trace-viewer/src/ui/networkResourceDetails.tsx@@ -24,7 +24,7 @@ import { generateCurlCommand, generateFetchCall } from '../third_party/devtools'import { CopyToClipboardTextButton } from './copyToClipboard';import { getAPIRequestCodeGen } from './codegen';import type { Language } from '@isomorphic/locatorGenerators';-import { isJsonMimeType } from '@isomorphic/mimeType';+import { isJsonMimeType, isXmlMimeType } from '@isomorphic/mimeType';import { msToString, useAsyncMemo, useSetting } from '@web/uiUtils';import type { Entry } from '@trace/har';import { useTraceModel } from './traceModelContext';@@ -260,6 +260,34 @@ function statusClass(statusCode: number): string {return 'red-circle';}+const kInlineTagPattern = /<[^>]+>[^<]*<\//;++function formatXml(xml: string, indent = ' ') {+ let depth = 0;+ const lines: string[] = [];+ const tokens = xml.replace(/>\s*</g, '>\n<').split('\n');++ for (const token of tokens) {+ const trimmed = token.trim();+ if (!trimmed)+ continue;++ if (trimmed.startsWith('</')) {+ depth = Math.max(depth - 1, 0);+ lines.push(indent.repeat(depth) + trimmed);+ } else if (trimmed.endsWith('/>') || trimmed.startsWith('<?') || kInlineTagPattern.test(trimmed)) {+ lines.push(indent.repeat(depth) + trimmed);+ } else if (trimmed.startsWith('<')) {+ lines.push(indent.repeat(depth) + trimmed);+ depth++;+ } else {+ lines.push(indent.repeat(depth) + trimmed);+ }+ }++ return lines.join('\n');+}+function formatBody(body: string | null, contentType: string): string {if (body === null)return 'Loading...';@@ -276,6 +304,14 @@ function formatBody(body: string | null, contentType: string): string {}}+ if (isXmlMimeType(contentType)) {+ try {+ return formatXml(bodyStr);+ } catch {+ return bodyStr;+ }+ }+if (contentType.includes('application/x-www-form-urlencoded'))return decodeURIComponent(bodyStr);diff --git a/tests/assets/network-tab/network.html b/tests/assets/network-tab/network.htmlindex 2544a7bf9d..3bcd58dba3 100644--- a/tests/assets/network-tab/network.html+++ b/tests/assets/network-tab/network.html@@ -32,6 +32,11 @@headers: { 'Content-Type': 'application/json' },body,}),+ fetch('/post-xml-data', {+ method: 'POST',+ headers: { 'Content-Type': 'application/xml' },+ body: `<?xml version="1.0"?><note to="Alice" from="Bob"><body>Hello & welcome!</body></note>`,+ }),];window.donePromise = Promise.all(fetches.map(f => f.then(r => r.body()).then(() => null).catch(e => {})));</script>diff --git a/tests/library/trace-viewer.spec.ts b/tests/library/trace-viewer.spec.tsindex 656c0fa702..c09c020cd5 100644--- a/tests/library/trace-viewer.spec.ts+++ b/tests/library/trace-viewer.spec.ts@@ -478,7 +478,7 @@ test('should filter network requests by multiple resource types', async ({ page,await expect(networkRequests.getByText('image.png')).toBeVisible();await traceViewer.page.getByText('All', { exact: true }).click();- await expect(networkRequests).toHaveCount(9);+ await expect(networkRequests).toHaveCount(10);});test('should show font preview', async ({ page, runAndTrace, server }) => {diff --git a/tests/playwright-test/ui-mode-test-network-tab.spec.ts b/tests/playwright-test/ui-mode-test-network-tab.spec.tsindex 968a91e5c0..49220c5b16 100644--- a/tests/playwright-test/ui-mode-test-network-tab.spec.ts+++ b/tests/playwright-test/ui-mode-test-network-tab.spec.ts@@ -80,7 +80,7 @@ test('should filter network requests by multiple resource types', async ({ runUIawait page.getByText('Network', { exact: true }).click();const networkItems = page.getByRole('list', { name: 'Network requests' }).getByRole('listitem');- await expect(networkItems).toHaveCount(9);+ await expect(networkItems).toHaveCount(10);await page.getByText('JS', { exact: true }).click();await expect(networkItems).toHaveCount(1);@@ -101,7 +101,7 @@ test('should filter network requests by multiple resource types', async ({ runUIawait expect(networkItems.getByText('image.png')).toBeVisible();await page.getByText('All', { exact: true }).click();- await expect(networkItems).toHaveCount(9);+ await expect(networkItems).toHaveCount(10);});test('should filter network requests by url', async ({ runUITest, server }) => {@@ -189,6 +189,32 @@ test('should format JSON request body', async ({ runUITest, server }) => {], { useInnerText: true });});+test('should format XML request body', async ({ runUITest, server }) => {+ const { page } = await runUITest({+ 'network-tab.test.ts': `+ import { test, expect } from '@playwright/test';+ test('network tab test', async ({ page }) => {+ await page.goto('${server.PREFIX}/network-tab/network.html');+ await page.evaluate(() => (window as any).donePromise);+ });+ `,+ });++ await page.getByText('network tab test').dblclick();+ await expect(page.getByTestId('workbench-run-status')).toContainText('Passed');++ await page.getByText('Network', { exact: true }).click();+ await page.getByText('post-xml-data').click();+ await page.getByRole('tabpanel', { name: 'Network' }).getByRole('tab', { name: 'Payload' }).click();+ const payloadPanel = page.getByRole('tabpanel', { name: 'Payload' });+ await expect(payloadPanel.locator('.CodeMirror-code .CodeMirror-line')).toHaveText([+ '<?xml version="1.0"?>',+ '<note to="Alice" from="Bob">',+ ' <body>Hello & welcome!</body>',+ '</note>'+ ], { useInnerText: true });+});+test('should display list of query parameters (only if present)', async ({ runUITest, server }) => {const { page } = await runUITest({'network-tab.test.ts': `