Skip to content

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

feat(fetch): send Playwright as default user-agent for global fetch (#9195)

Yury Semikhatsky64657c3b657b
diff --git a/src/server/fetch.ts b/src/server/fetch.ts
index 23824dd6ba..471358a7db 100644
--- a/src/server/fetch.ts
+++ b/src/server/fetch.ts
@@ -23,7 +23,7 @@ import zlib from 'zlib';
import { HTTPCredentials } from '../../types/types';
import { NameValue, NewRequestOptions } from '../common/types';
import { TimeoutSettings } from '../utils/timeoutSettings';
-import { createGuid, isFilePayload, monotonicTime } from '../utils/utils';
+import { createGuid, getPlaywrightVersion, isFilePayload, monotonicTime } from '../utils/utils';
import { BrowserContext } from './browserContext';
import { MultipartFormData } from './formData';
import { SdkObject } from './instrumentation';
@@ -352,7 +352,7 @@ export class GlobalFetchRequest extends FetchRequest {
}
this._options = {
baseURL: options.baseURL,
- userAgent: options.userAgent || '',
+ userAgent: options.userAgent || `Playwright/${getPlaywrightVersion()}`,
extraHTTPHeaders: options.extraHTTPHeaders,
ignoreHTTPSErrors: !!options.ignoreHTTPSErrors,
httpCredentials: options.httpCredentials,
diff --git a/tests/global-fetch.spec.ts b/tests/global-fetch.spec.ts
index 28ecc82c92..279958e3e0 100644
--- a/tests/global-fetch.spec.ts
+++ b/tests/global-fetch.spec.ts
@@ -15,6 +15,7 @@
*/
import http from 'http';
+import { getPlaywrightVersion } from '../lib/utils/utils';
import { expect, playwrightTest as it } from './config/browserTest';
it.skip(({ mode }) => mode !== 'default');
@@ -143,3 +144,11 @@ it('should resolve url relative to gobal baseURL option', async ({ playwright, s
expect(response.url()).toBe(server.EMPTY_PAGE);
});
+it('should set playwright as user-agent', async ({ playwright, server }) => {
+ const request = await playwright._newRequest();
+ const [serverRequest] = await Promise.all([
+ server.waitForRequest('/empty.html'),
+ request.get(server.EMPTY_PAGE)
+ ]);
+ expect(serverRequest.headers['user-agent']).toBe('Playwright/' + getPlaywrightVersion());
+});