Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use external Chrome process whenever its DevTools URL is provided via an env var #681

Closed
wants to merge 1 commit into from

Conversation

andrewslotin
Copy link
Contributor

This is a proof-of-concept to make xk6-browser use an external Chrome process to run tests.

Whenever the DevTools URL is provided via the CHROME_DEVTOOLS_URL= env var, chromium.launch() skips launching the browser process altogether and uses provided URL instead. browser.close() is a noop in this case, since we don't own the process and it can be reused by other test runs. It is possible though to terminate the browser process via CDP.

This code by no means should be considered production-ready and provided only to prove the idea.

@andrewslotin andrewslotin marked this pull request as draft January 2, 2023 23:28
@inancgumus
Copy link
Member

PS: I added #682 for tracking the general work about this.

Copy link
Member

@inancgumus inancgumus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, @andrewslotin!

I've tried this with the example JavaScripts (examples/) and they seem to work 🥳

For now, some Go tests don't pass (see below). But, we can figure it out later on. As a POC, it looks OK. I believe we still need to do some more tests to ensure 😄

$ chromium --remote-debugging-port=8079 &
...
$ CHROME_DEVTOOLS_URL=ws://127.0.0.1:8079/devtools/browser/5e49211e-d8f3-4f79-8646-7a2a8b90027d go test ./tests -race -v
...
=== RUN   TestBrowserContextOptionsExtraHTTPHeaders
    browser_context_options_test.go:90: 
                Error Trace:    /Users/inanc/grafana/k6browser/main/tests/browser_context_options_test.go:90
                Error:          Received unexpected error:
                                test timed out after 5s
                Test:           TestBrowserContextOptionsExtraHTTPHeaders
--- FAIL: TestBrowserContextOptionsExtraHTTPHeaders (5.12s)
...
--- PASS: TestBrowserNewPage (0.21s)
=== RUN   TestTmpDirCleanup
    browser_test.go:59: 
                Error Trace:    /Users/inanc/grafana/k6browser/main/tests/browser_test.go:59
                Error:          Should be empty, but was [xk6-browser-data-3329750862]
                Test:           TestTmpDirCleanup
                Messages:       a dir shouldn't exist which matches the pattern `xk6-browser-data-*
...
=== RUN   TestBrowserUserAgent
    browser_test.go:145: 
                Error Trace:    /Users/inanc/grafana/k6browser/main/tests/browser_test.go:145
                Error:          "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4753.0 Safari/537.36" does not contain "Headless"
                Test:           TestBrowserUserAgent
...
=== RUN   TestElementHandleClickConcealedLink
DEADLOCK HERE
...
and so on.

@ankur22
Copy link
Collaborator

ankur22 commented Jan 4, 2023

This is a great POC! Simple to understand and it works 💪

It confirms that we can indeed quite easily hook into an existing Chrome browser process ✅

I guess the next steps are to identify the best strategy based on how we can retrieve the URL of the running browser process from the container or browser farm. Would it be best to think about the next strategy based on the simplest and most secure approach, which is the browser running in a container? I think we need to have a discussion with the k6 and backend teams to identify what's the best approach of retrieving the URL from a running browser process.

@andrewslotin
Copy link
Contributor Author

@ankur22, Chrome can be launched with predefined CDP port by providing --remote-debugging-port=, so there is no need to retrieve it from the log output. There is a chance that desired port is already taken when running on a local machine, so I'm not suggesting to get rid of launching the process completely. In more controlled envs though, like Docker containers, where there are less random processes listening on random ports, I believe we're safe use a predefined port and inject it via configuration.

@inancgumus inancgumus added the remote remote browser related label Mar 6, 2023
@inancgumus
Copy link
Member

Closing as it is solved by the having Connect API.

@inancgumus inancgumus closed this Mar 28, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
remote remote browser related
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants