playwright icon indicating copy to clipboard operation
playwright copied to clipboard

[Feature] Unable to route CDP connection request through Proxy

Open Adarsh2692 opened this issue 2 years ago • 11 comments

Here, I am trying to connect to BrowserStack using the CDP url. My system has a proxy in place and all the network requests should be routed through the proxy. In this case, I am unable to do that even after setting the Proxy in the environment.

System info

  • Playwright Version: [v1.34]
  • Operating System: [All, Windows 11, Ubuntu 20, macOS 13.2, etc.]
  • Browser: [All, Chromium, Firefox, WebKit]
  • Other info:

Source code

import json
import urllib
from playwright.sync_api import sync_playwright

import os

proxy = 'http://localhost:8888'

os.environ['http_proxy'] = "http://127.0.0.1:8888" 
os.environ['HTTP_PROXY'] = "http://127.0.0.1:8888"
os.environ['https_proxy'] = "http://127.0.0.1:8888"
os.environ['HTTPS_PROXY'] = "http://127.0.0.1:8888"


proxy_server_url = "http://localhost:8888"


desired_cap = {
  'browser': 'chrome', 
  'browser_version': 'latest', 
  'os': 'osx',
  'os_version': 'catalina',
  'name': 'Branded Google Chrome on Catalina',
  'build': 'playwright-python-1',
  'browserstack.username': 'user',
  'browserstack.accessKey': 'key'
}

def run_session(playwright):
  clientPlaywrightVersion = str(subprocess.getoutput('playwright --version')).strip().split(" ")[1]
  desired_cap['client.playwrightVersion'] = clientPlaywrightVersion
  proxy = {
    "server": "http://localhost:8888",
    # "username": "REDACTED",
    # "password": "REDACTED",
  }


  cdpUrl = 'ws://cdp.browserstack.com/playwright?caps=' + urllib.parse.quote(json.dumps(desired_cap))

  browser = playwright.chromium.connect(cdpUrl)
  page = browser.new_page()
  try:
    page.goto("https://www.google.co.in/")
    page.fill("[aria-label='Search']", 'Browserstack')
    locator = page.locator("[aria-label='Google Search'] >> nth=0")
    locator.click()
    title = page.title()

    if title == "Browserstack - Google Search":
      mark_test_status("passed", "Title matched", page)
    else:
      mark_test_status("failed", "Title did not match", page)
  except Exception as err:
      mark_test_status("failed", str(err), page)

  browser.close()

def mark_test_status(status, reason, page):
  page.evaluate("_ => {}", f'browserstack_executor: {{"action": "setSessionStatus", "arguments": {{"status":"{status}", "reason": "{reason}"}}}}')

with sync_playwright() as playwright:
  run_session(playwright)
  • [x] I provided exact source code that allows reproducing the issue locally.

Link to the GitHub repository with the repro

[https://github.com/your_profile/playwright_issue_title]

or

Test file (self-contained)

from playwright.sync_api import sync_playwright
with sync_playwright() as p:
    browser = p.chromium.launch()
    page = browser.new_page()
    # ...
    browser.close()

Steps

  • python3 filename.py

Expected

The connection request to the cdp URL is not getting routed through the proxy ("http://127.0.0.1:8888" in this case)

Actual

The connection request to the CDP endpoint should be routed through the Proxy.

Adarsh2692 avatar Sep 11 '23 05:09 Adarsh2692

@Adarsh2692 I don't think Playwright supports proxy when connecting over websocket url. Let me change this bug into a feature request.

dgozman avatar Sep 12 '23 02:09 dgozman

Hey @dgozman , can we have any ETA in this case?

Adarsh2692 avatar Sep 13 '23 06:09 Adarsh2692

@Adarsh2692 No ETA, this issue is open for prioritization for now.

dgozman avatar Sep 13 '23 14:09 dgozman

@dgozman Thanks for prioritising. Would just like to add that we are seeing a lot of visibility on this issue. Looking forward to your updates on this thread !!

AdityaRoshanG avatar Sep 21 '23 07:09 AdityaRoshanG

Hi team any update here ?

sharutkarsh avatar Oct 18 '23 17:10 sharutkarsh

It would be great to have this implemented as it's pretty common scenario in corporate envs.

LucaLis avatar Jun 25 '24 09:06 LucaLis

Browserstack docs describe workarounds for Node.js and Python binding of Playwright: https://www.browserstack.com/docs/automate/playwright/run-test-from-behind-proxies

Instructions for Python are a bit obsolete becase the file that is supposed to bootstrap global-agent (playwright/driver/package/lib/cli/cli.js) is not there anymore in recent versions of Playwright, however bootstrap method can be called from playwright/driver/package/lib/cli/program.js

Same workaround can be applied in Java: you will need to unzip contents of playwright-driver.jar, follow the instructions for Python and wrap it all (node_modules with global-agent libs and modified program.js) into the jar.

LucaLis avatar Jun 26 '24 18:06 LucaLis

I have the same issue when playwright connecting proxy over the websocket url. any updates?

pavankumardmg avatar Jul 16 '24 15:07 pavankumardmg

I followed workaround described in BrowserStack: https://www.browserstack.com/docs/automate/playwright/run-test-from-behind-proxies I was able to get to the point when BrowserStack Local is started, session is opened and I can see on BrowserStack that my tests are running.

But above workaround is not fixing issue with accessing app running on localhost (even after adding proxyHost and proxyPort to BrowserStack Local)

Does anyone was able to make it work? (working behind corporate proxy and also having app running on localhost)

basia1 avatar Jul 20 '24 14:07 basia1

@basia1 maybe try to add env var like: GLOBAL_AGENT_NO_PROXY=localhost

LucaLis avatar Jul 20 '24 18:07 LucaLis

@dgozman Do we have any updates on this?

amaanbs avatar Oct 11 '24 11:10 amaanbs

As CDP is intended for local connections only, proxying those does not sound like something we should support. I don't think browserstack recommends this either: https://github.com/browserstack/node-js-playwright-browserstack

pavelfeldman avatar Jan 07 '25 01:01 pavelfeldman

Hi @pavelfeldman ! Browserstack team advices to use a workaround in order to resolve this problem in corporate environemnts with a proxy (described here: https://www.browserstack.com/docs/automate/playwright/run-test-from-behind-proxies). Workaround is supplied for Node and Python and similar stuff can be done for Java too. Unfortunately workaround requires to modify Playwright packages which is quite complicated and needs to be applied again for each PW release. It would be really great if Playwright team could take a look at this workaround and reconsider implementation of solution that could help to resolve this issue.

LucaLis avatar Jan 14 '25 12:01 LucaLis