Skip to content

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

chore: only throw the proxy on launch required on win/CR (#6350)

Max Schmitt1c40c94ed7bd
diff --git a/docs/src/api/params.md b/docs/src/api/params.md
index 27c1cfcc49..6e9a4593d8 100644
--- a/docs/src/api/params.md
+++ b/docs/src/api/params.md
@@ -447,9 +447,13 @@ Actual picture of each page will be scaled down if necessary to fit the specifie
- `username` <[string]> Optional username to use if HTTP proxy requires authentication.
- `password` <[string]> Optional password to use if HTTP proxy requires authentication.
-Network proxy settings to use with this context. Note that browser needs to be launched with the global proxy for this
-option to work. If all contexts override the proxy, global proxy will be never used and can be any string, for example
-`launch({ proxy: { server: 'per-context' } })`.
+Network proxy settings to use with this context.
+
+:::note
+For Chromium on Windows the browser needs to be launched with the global proxy for this option to work. If all
+contexts override the proxy, global proxy will be never used and can be any string, for example
+`launch({ proxy: { server: 'http://per-context' } })`.
+:::
## select-options-values
* langs: java, js
diff --git a/src/server/browserContext.ts b/src/server/browserContext.ts
index 97e1b579a3..6c7a8bb8b3 100644
--- a/src/server/browserContext.ts
+++ b/src/server/browserContext.ts
@@ -15,6 +15,7 @@
* limitations under the License.
*/
+import * as os from 'os';
import { TimeoutSettings } from '../utils/timeoutSettings';
import { debugMode, mkdirIfNeeded, createGuid } from '../utils/utils';
import { Browser, BrowserOptions } from './browser';
@@ -405,8 +406,8 @@ export function validateBrowserContextOptions(options: types.BrowserContextOptio
options.recordVideo.size!.height &= ~1;
}
if (options.proxy) {
- if (!browserOptions.proxy)
- throw new Error(`Browser needs to be launched with the global proxy. If all contexts override the proxy, global proxy will be never used and can be any string, for example "launch({ proxy: { server: 'per-context' } })"`);
+ if (!browserOptions.proxy && browserOptions.isChromium && os.platform() === 'win32')
+ throw new Error(`Browser needs to be launched with the global proxy. If all contexts override the proxy, global proxy will be never used and can be any string, for example "launch({ proxy: { server: 'http://per-context' } })"`);
options.proxy = normalizeProxySettings(options.proxy);
}
if (debugMode() === 'inspector')
diff --git a/tests/browsercontext-proxy.spec.ts b/tests/browsercontext-proxy.spec.ts
index 8db7198de3..557cbe4743 100644
--- a/tests/browsercontext-proxy.spec.ts
+++ b/tests/browsercontext-proxy.spec.ts
@@ -26,7 +26,9 @@ it.afterAll(async () => {
await browser.close();
});
-it('should throw for missing global proxy', async ({ browserType, browserOptions, server }) => {
+it('should throw for missing global proxy on Chromium Windows', async ({ browserName, platform, browserType, browserOptions, server }) => {
+ it.skip(browserName !== 'chromium' || platform !== 'win32');
+
delete browserOptions.proxy;
const browser = await browserType.launch(browserOptions);
const error = await browser.newContext({ proxy: { server: `localhost:${server.PORT}` } }).catch(e => e);
@@ -34,6 +36,27 @@ it('should throw for missing global proxy', async ({ browserType, browserOptions
await browser.close();
});
+it('should work when passing the proxy only on the context level', async ({browserName, platform, browserType, browserOptions, contextOptions, server}) => {
+ // Currently an upstream bug in the network stack of Chromium which leads that
+ // the wrong proxy gets used in the BrowserContext.
+ it.fixme(browserName === 'chromium' && platform === 'win32');
+
+ server.setRoute('/target.html', async (req, res) => {
+ res.end('<html><title>Served by the proxy</title></html>');
+ });
+ delete browserOptions.proxy;
+ const browserWithoutProxyInLaunch = await browserType.launch(browserOptions);
+ const context = await browserWithoutProxyInLaunch.newContext({
+ ...contextOptions,
+ proxy: { server: `localhost:${server.PORT}` }
+ });
+
+ const page = await context.newPage();
+ await page.goto('http://non-existent.com/target.html');
+ expect(await page.title()).toBe('Served by the proxy');
+ await browserWithoutProxyInLaunch.close();
+});
+
it('should throw for bad server value', async ({ contextOptions }) => {
const error = await browser.newContext({
...contextOptions,
diff --git a/types/types.d.ts b/types/types.d.ts
index e9cee28f75..693a1520fb 100644
--- a/types/types.d.ts
+++ b/types/types.d.ts
@@ -8694,9 +8694,11 @@ export interface Browser extends EventEmitter {
permissions?: Array<string>;
/**
- * Network proxy settings to use with this context. Note that browser needs to be launched with the global proxy for this
- * option to work. If all contexts override the proxy, global proxy will be never used and can be any string, for example
- * `launch({ proxy: { server: 'per-context' } })`.
+ * Network proxy settings to use with this context.
+ *
+ * > NOTE: For Chromium on Windows the browser needs to be launched with the global proxy for this option to work. If all
+ * contexts override the proxy, global proxy will be never used and can be any string, for example `launch({ proxy: {
+ * server: 'http://per-context' } })`.
*/
proxy?: {
/**
@@ -10545,9 +10547,11 @@ export interface BrowserContextOptions {
permissions?: Array<string>;
/**
- * Network proxy settings to use with this context. Note that browser needs to be launched with the global proxy for this
- * option to work. If all contexts override the proxy, global proxy will be never used and can be any string, for example
- * `launch({ proxy: { server: 'per-context' } })`.
+ * Network proxy settings to use with this context.
+ *
+ * > NOTE: For Chromium on Windows the browser needs to be launched with the global proxy for this option to work. If all
+ * contexts override the proxy, global proxy will be never used and can be any string, for example `launch({ proxy: {
+ * server: 'http://per-context' } })`.
*/
proxy?: {
/**