cypress-io / cypress

Fast, easy and reliable testing for anything that runs in a browser.
https://cypress.io
MIT License
46.84k stars 3.17k forks source link

Support connection to remote browser #5984

Open seyfer opened 4 years ago

seyfer commented 4 years ago

It is a proposal.

Current behavior:

Currently, all browsers for Cypress runner should be installed on the same host (or same container). It makes it hard to have custom setups in complex projects with a lot of services connected via Docker.

Desired behavior:

Support possibility to connect to remote Chrome browser via WS, running locally or in the container.

For example, when using e2e testing with Jest + Puppeteer setup, I can use a separate Chrome container and just set a remote chrome address in config.

connect: {
    browserURL: 'http://127.0.0.1:9222'
}

Steps to reproduce: (app code and test code)

There is no such option yet.

Versions

Cypress 3.8.0

bubblegumsoldier commented 4 years ago

Are there any news on this one?

mrmeku commented 4 years ago

I'm very interested in using this feature! Is there a design document for the feature? Anyway I can help to implement it?

estefafdez commented 4 years ago

+1 to this one!

paneq commented 4 years ago

This could work really nicely with https://github.com/browserless/chrome

redReno commented 3 years ago

I would love this feature. Right now we have to use Selenium and Puppeteer but I generally prefer to work with Cypress.

aalexgabi commented 3 years ago

It would be nice if Cypress can be used exclusively over Chrome Devtools Protocol to test Adobe CEP and other Chromium Embedded Framework applications. Those apps are running in a controlled environment. I guess it could also work for Electron type of apps as well. Currently I have to use puppeteer and manually configure everything.

upMKuhn commented 2 years ago

Yeah that would be really handy to test Hybrid apps! If Cypress could only be combined with Appium. So basically iOS safari and Chrome remote debugger protocol support.

furkanmustafa commented 1 year ago

Same need here as well

furkanmustafa commented 1 year ago

looks like there is an env var for this; https://github.com/cypress-io/cypress/blob/develop/packages/server/lib/browsers/protocol.ts#L36

called CYPRESS_REMOTE_DEBUGGING_PORT. Browser launching code can be stubbed to noop, and this port could be configured on browser side to achieve the same effect? Sounds like worth giving it a try.

tmaier commented 1 year ago

@furkanmustafa interesting idea. Did you try it out?

furkanmustafa commented 1 year ago

@tmaier sorry, didn't have enough time to finish it yet. probably not too easy, but still worth keep trying though.

zborro commented 1 year ago

I spent some time forcing Cypress to connect to remote browser. Initially I wanted to do something similar to what @furkanmustafa suggested. However, after looking at the code I changed direction to write small script that would just pretend to be chrome, but actually forward all stuff to manually started instance.

#!/usr/bin/env python3
# don't judge me by following code ;-)
import sys, subprocess
args = sys.argv[1:]
cdp_host = [arg.split('=')[1] for arg in args if arg.startswith('--remote-debugging-address=')][0]
cdp_port = [int(arg.split('=')[1]) for arg in args if arg.startswith('--remote-debugging-port=')][0]
subprocess.Popen(["socat", f"tcp-l:{cdp_port},fork,reuseaddr", "tcp:127.0.0.1:9222"]).communicate()

Then, Customize available browsers can be used to railroad Cypress into using the script. Other necessary hack is to open about:blank in the remote browser.

However, it still won't work. Cypress will successfully connect to the browser, but will replace about:blank with a warning that states:

Whoops, we can't run your tests.

This browser was not launched through Cypress. Tests cannot run.

I believe it's due to the fact that one of the parameters passed to Chromium is --proxy-server. It speaks for itself - L7 traffic is proxied through localhost. With extra effort and some creativity this isn't huge obstacle - maybe something like redsocks (or even simpler socat) could be used to beat that limitation.

Hopefully you find this information helpful. I think for my simple use case it's too much hacking now and I'm gonna switch to playwright. It markets itself as being able to do what we talk about here and I don't have any existing code base anyway.

triwav commented 1 year ago

@zborro I have a feeling I might be trying to do something different than what you are talking about but have been running into a lot of dead ends for trying to figure out what I'm doing so wondering if you might have some ideas.

Basically my goal is to try and run automated tests on a webview inside of a native application like a firetv. I have connected my device with adb and if I go to chrome://inspect in Chrome then I can see the chrome inspector for the webview for the current application. I would like to be able to use that same connection to run automated tests on the device. Any help or ideas is appreciated. Thank you.

zborro commented 1 year ago

@triwav does it open a port on your local box that you can connect to? I'm not sure how adb does what it does. Maybe it works similarly to how I used socat. You should look for DevTools port, usually it's 9222. But once you find it, you'll probably hit the issue I descibed above - remote browser session requires the remote part to use Cypress proxy.

Also, have you thought about testing it externally? Since it's running in WebView, you can run it outside the app (given that the API to the native part can be emulated). Maybe it would be much easier to achieve your goals that way. Good luck!

EDIT: if you're on Linux, there's handy tool to see which ports are opened: netstat -pln :smile_cat:

triwav commented 1 year ago

Thanks @zborro I finally found this https://playwright.dev/docs/api/class-android which seems to be working the way I need it to. It is experimental but so far seems pretty good. In answer to your question, the idea is to be able to both test on device and off device in case any device specific issues may arise

zborro commented 1 year ago

@triwav happy to hear that. I also ended up using Playwright :smile_cat:

I think that Cypress losses some user base by not supporting this kind of features.