Skip to content

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

fix: API response to string (#10364)

Yury Semikhatsky08a7470b0a56
diff --git a/packages/playwright-core/src/client/consoleMessage.ts b/packages/playwright-core/src/client/consoleMessage.ts
index 9c6d5afeb2..58caf3e97c 100644
--- a/packages/playwright-core/src/client/consoleMessage.ts
+++ b/packages/playwright-core/src/client/consoleMessage.ts
@@ -47,7 +47,7 @@ export class ConsoleMessage extends ChannelOwner<channels.ConsoleMessageChannel,
return this._initializer.location;
}
- [(util.inspect as any).custom]() {
+ [util.inspect.custom]() {
return this.text();
}
}
diff --git a/packages/playwright-core/src/client/fetch.ts b/packages/playwright-core/src/client/fetch.ts
index 2f74bdfe2d..d794a079fc 100644
--- a/packages/playwright-core/src/client/fetch.ts
+++ b/packages/playwright-core/src/client/fetch.ts
@@ -17,6 +17,7 @@
import fs from 'fs';
import path from 'path';
import * as mime from 'mime';
+import * as util from 'util';
import { Serializable } from '../../types/structs';
import * as api from '../../types/types';
import { HeadersArray } from '../common/types';
@@ -272,6 +273,11 @@ export class APIResponse implements api.APIResponse {
});
}
+ [util.inspect.custom]() {
+ const headers = this.headersArray().map(({ name, value }) => ` ${name}: ${value}`);
+ return `APIResponse: ${this.status()} ${this.statusText()}\n${headers.join('\n')}`;
+ }
+
_fetchUid(): string {
return this._initializer.fetchUid;
}
diff --git a/packages/playwright-core/src/client/locator.ts b/packages/playwright-core/src/client/locator.ts
index 706d4381a8..8c0fdb6b86 100644
--- a/packages/playwright-core/src/client/locator.ts
+++ b/packages/playwright-core/src/client/locator.ts
@@ -241,7 +241,7 @@ export class Locator implements api.Locator {
});
}
- [(util.inspect as any).custom]() {
+ [util.inspect.custom]() {
return this.toString();
}
diff --git a/tests/global-fetch.spec.ts b/tests/global-fetch.spec.ts
index 002969fd77..1aa36e4383 100644
--- a/tests/global-fetch.spec.ts
+++ b/tests/global-fetch.spec.ts
@@ -15,6 +15,7 @@
*/
import http from 'http';
+import * as util from 'util';
import { getPlaywrightVersion } from 'playwright-core/lib/utils/utils';
import { expect, playwrightTest as it } from './config/browserTest';
@@ -294,3 +295,18 @@ it(`should accept already serialized data as Buffer when content-type is applica
expect(body.toString()).toEqual(value);
await request.dispose();
});
+
+it(`should have nice toString`, async ({ playwright, server }) => {
+ const request = await playwright.request.newContext();
+ const response = await request.post(server.EMPTY_PAGE, {
+ headers: {
+ 'content-type': 'application/json'
+ },
+ data: 'My post data'
+ });
+ const str = response[util.inspect.custom]();
+ expect(str).toContain('APIResponse: 200 OK');
+ for (const { name, value } of response.headersArray())
+ expect(str).toContain(` ${name}: ${value}`);
+ await request.dispose();
+});