selenium icon indicating copy to clipboard operation
selenium copied to clipboard

[🚀 Feature]: Ability to unsubscribe from CDP events

Open boris-petrov opened this issue 2 years ago • 10 comments

Feature and motivation

Right now we can subscribe for events which is awesome!

devtools = Capybara.current_session.driver.browser.devtools
devtools.network.enable
devtools.network.on(:web_socket_frame_sent) { |params| puts params }

However, it's not possible to unsubscribe from these events (something like an off method).

Usage example

Perhaps an off method similar to on:

devtools = Capybara.current_session.driver.browser.devtools
devtools.network.enable
callback = lambda { |params| puts params }
devtools.network.on(:web_socket_frame_sent, lambda)
devtools.network.off(:web_socket_frame_sent, lambda)

boris-petrov avatar Nov 16 '23 11:11 boris-petrov

@boris-petrov, thank you for creating this issue. We will troubleshoot it as soon as we can.


Info for maintainers

Triage this issue by using labels.

If information is missing, add a helpful comment and then I-issue-template label.

If the issue is a question, add the I-question label.

If the issue is valid but there is no time to troubleshoot it, consider adding the help wanted label.

If the issue requires changes or fixes from an external project (e.g., ChromeDriver, GeckoDriver, MSEdgeDriver, W3C), add the applicable G-* label, and it will provide the correct link and auto-close the issue.

After troubleshooting the issue, please add the R-awaiting answer label.

Thank you!

github-actions[bot] avatar Nov 16 '23 11:11 github-actions[bot]

@pujagani does Java support unsubscribing from events, yet?

titusfortner avatar Nov 16 '23 14:11 titusfortner

@titusfortner sorry, I should have mentioned - in my case the language is Ruby. I don't know if the rest of the languages have support for unsubscription.

boris-petrov avatar Nov 16 '23 14:11 boris-petrov

@boris-petrov yes I know, but Puja has been the one working the most on our upcoming BiDi implementations in Java and I'm curious how (if) Java is handling this. We may not add support for it in Ruby to Chrome DevTools, but I do think we need to be able to support it for the WebDriver BiDi spec implementation.

titusfortner avatar Nov 16 '23 19:11 titusfortner

This is where the closeable part comes in from the Java end. Where we unsubscribe from the events of the module when close() gets called. Also, if the user has access to the BiDi object (which handles the underlying command sending etc), there is an unsubscribe method which internally calls the W3C BiDi unsubscribe method https://w3c.github.io/webdriver-bidi/#command-session-unsubscribe.

pujagani avatar Nov 17 '23 05:11 pujagani

I think it would be nice to unsubscribe from single events. The BiDi has a public <X> void clearListener(Event<X> event) { which is missing for the DevTools. So are there any reasons not to add this method to the DevTools too?

Another option would be to return a Closable when calling DevTools.addListener to detach the listener, when Closable.close is called. This would allow to remove the added listener instead of all for a specific event.

What option would you prefere?

joerg1985 avatar Dec 12 '23 09:12 joerg1985

I think for Java, we can do something similar to what BiDi already has. But it depends on what the user is looking for.

pujagani avatar Dec 19 '23 05:12 pujagani

@p0deje how hard would it be to implement this in Ruby? I'm more interested in our BiDi implementation than CDP, obviously.

titusfortner avatar Dec 31 '23 19:12 titusfortner

@titusfortner It should be fairly straightforward, we just need to clear @devtools.callbacks properly which is simply a hash of event => listeners. I'm not sure what the API should look like for this. Which looks more reasonable?

Option 1

listener = devtools.network.on(:web_socket_frame_sent) { |params| puts params }
listener.close

Option 2

callback = ->(params) { puts params }
devtools.network.on(:web_socket_frame_sent, &callback)
devtools.network.off(:web_socket_frame_sent, &callback)

Option 3

callback = ->(params) { puts params }
devtools.network.on(:web_socket_frame_sent, &callback)
devtools.network.events[:web_socket_frame_sent].delete(callback)
devtools.network.events[:web_socket_frame_sent].clear # remove all

p0deje avatar Jan 02 '24 00:01 p0deje

This issue is stale because it has been open 280 days with no activity. Remove stale label or comment or this will be closed in 14 days.

github-actions[bot] avatar Oct 08 '24 10:10 github-actions[bot]

This issue was closed because it has been stalled for 14 days with no activity.

github-actions[bot] avatar Oct 22 '24 10:10 github-actions[bot]

This issue has been automatically locked since there has not been any recent activity since it was closed. Please open a new issue for related bugs.

github-actions[bot] avatar Nov 21 '24 22:11 github-actions[bot]