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(fetch): browser proxy credentials (#8760)
Yury Semikhatsky6263361284ab
diff --git a/src/server/fetch.ts b/src/server/fetch.tsindex 340fbb0872..0142938e3d 100644--- a/src/server/fetch.ts+++ b/src/server/fetch.ts@@ -40,12 +40,13 @@ export async function playwrightFetch(context: BrowserContext, params: types.Fet}const method = params.method?.toUpperCase() || 'GET';+ const proxy = context._options.proxy || context._browser.options.proxy;let agent;- if (context._options.proxy) {+ if (proxy) {// TODO: support bypass proxy- const proxyOpts = url.parse(context._options.proxy.server);- if (context._options.proxy.username)- proxyOpts.auth = `${context._options.proxy.username}:${context._options.proxy.password || ''}`;+ const proxyOpts = url.parse(proxy.server);+ if (proxy.username)+ proxyOpts.auth = `${proxy.username}:${proxy.password || ''}`;agent = new HttpsProxyAgent(proxyOpts);}diff --git a/tests/browsercontext-fetch.spec.ts b/tests/browsercontext-fetch.spec.tsindex e3ba1e6de7..8ad3617ed2 100644--- a/tests/browsercontext-fetch.spec.ts+++ b/tests/browsercontext-fetch.spec.ts@@ -282,6 +282,26 @@ it('should work with context level proxy', async ({browserOptions, browserType,}});+it('should pass proxy credentials', async ({browserType, browserOptions, server, proxyServer}) => {+ proxyServer.forwardTo(server.PORT);+ let auth;+ proxyServer.setAuthHandler(req => {+ auth = req.headers['proxy-authorization'];+ return !!auth;+ });+ const browser = await browserType.launch({+ ...browserOptions,+ proxy: { server: `localhost:${proxyServer.PORT}`, username: 'user', password: 'secret' }+ });+ const context = await browser.newContext();+ // @ts-expect-error+ const response = await context._fetch('http://non-existent.com/simple.json');+ expect(proxyServer.connectHosts).toContain('non-existent.com:80');+ expect(auth).toBe('Basic ' + Buffer.from('user:secret').toString('base64'));+ expect(await response.json()).toEqual({foo: 'bar'});+ await browser.close();+});+it('should work with http credentials', async ({context, server}) => {server.setAuth('/empty.html', 'user', 'pass');