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
test(proxy): add a test for a second page against same proxy (#4185)
Pavel Feldmanefac743695fb
diff --git a/test/proxy.spec.ts b/test/proxy.spec.tsindex 59fb030bff..35a74092a6 100644--- a/test/proxy.spec.ts+++ b/test/proxy.spec.ts@@ -14,10 +14,40 @@* limitations under the License.*/-import { it, expect } from './fixtures';+import { folio as baseFolio } from './fixtures';import socks from 'socksv5';+const builder = baseFolio.extend<{}, {+ socksPort: number,+}>();++builder.socksPort.init(async ({ testWorkerIndex }, run) => {+ const server = socks.createServer((info, accept, deny) => {+ let socket;+ if ((socket = accept(true))) {+ // Catch and ignore ECONNRESET errors.+ socket.on('error', () => {});+ const body = '<html><title>Served by the SOCKS proxy</title></html>';+ socket.end([+ 'HTTP/1.1 200 OK',+ 'Connection: close',+ 'Content-Type: text/html',+ 'Content-Length: ' + Buffer.byteLength(body),+ '',+ body+ ].join('\r\n'));+ }+ });+ const socksPort = 9107 + testWorkerIndex * 2;+ server.listen(socksPort, 'localhost');+ server.useAuth(socks.auth.None());+ await run(socksPort);+ server.close();+}, { scope: 'worker' });++const { it, expect } = builder.build();+it('should throw for bad server value', async ({browserType, browserOptions}) => {const error = await browserType.launch({...browserOptions,@@ -41,6 +71,26 @@ it('should use proxy', async ({browserType, browserOptions, server}) => {await browser.close();});+it('should use proxy for second page', async ({browserType, browserOptions, server}) => {+ server.setRoute('/target.html', async (req, res) => {+ res.end('<html><title>Served by the proxy</title></html>');+ });+ const browser = await browserType.launch({+ ...browserOptions,+ proxy: { server: `localhost:${server.PORT}` }+ });++ const page = await browser.newPage();+ await page.goto('http://non-existent.com/target.html');+ expect(await page.title()).toBe('Served by the proxy');++ const page2 = await browser.newPage();+ await page2.goto('http://non-existent.com/target.html');+ expect(await page2.title()).toBe('Served by the proxy');++ await browser.close();+});+it('should work with IP:PORT notion', async ({browserType, browserOptions, server}) => {server.setRoute('/target.html', async (req, res) => {res.end('<html><title>Served by the proxy</title></html>');@@ -121,36 +171,34 @@ it('should exclude patterns', (test, { browserName, headful }) => {it('should use socks proxy', (test, { browserName, platform }) => {test.flaky(platform === 'darwin' && browserName === 'webkit', 'Intermittent page.goto: The network connection was lost error on bots');-}, async ({ browserType, browserOptions, testWorkerIndex }) => {- const server = socks.createServer((info, accept, deny) => {- let socket;- if ((socket = accept(true))) {- // Catch and ignore ECONNRESET errors.- socket.on('error', () => {});- const body = '<html><title>Served by the SOCKS proxy</title></html>';- socket.end([- 'HTTP/1.1 200 OK',- 'Connection: close',- 'Content-Type: text/html',- 'Content-Length: ' + Buffer.byteLength(body),- '',- body- ].join('\r\n'));- }+}, async ({ browserType, browserOptions, socksPort }) => {+ const browser = await browserType.launch({+ ...browserOptions,+ proxy: { server: `socks5://localhost:${socksPort}` }});- const socksPort = 9107 + testWorkerIndex * 2;- server.listen(socksPort, 'localhost');- server.useAuth(socks.auth.None());+ const page = await browser.newPage();+ await page.goto('http://non-existent.com');+ expect(await page.title()).toBe('Served by the SOCKS proxy');+ await browser.close();+});+it('should use socks proxy in second page', (test, { browserName, platform }) => {+ test.flaky(platform === 'darwin' && browserName === 'webkit', 'Intermittent page.goto: The network connection was lost error on bots');+}, async ({ browserType, browserOptions, socksPort }) => {const browser = await browserType.launch({...browserOptions,proxy: { server: `socks5://localhost:${socksPort}` }});+const page = await browser.newPage();await page.goto('http://non-existent.com');expect(await page.title()).toBe('Served by the SOCKS proxy');++ const page2 = await browser.newPage();+ await page2.goto('http://non-existent.com');+ expect(await page2.title()).toBe('Served by the SOCKS proxy');+await browser.close();- server.close();});it('does launch without a port', async ({ browserType, browserOptions }) => {