w3c / webdriver

Remote control interface that enables introspection and control of user agents.
https://w3c.github.io/webdriver/
Other
676 stars 190 forks source link

Consider introducing some kind of interface for resetting a session. #1777

Closed ioquatix closed 1 month ago

ioquatix commented 7 months ago

New Session

Creating a new session can be slow. In my tests, it's between 300-500ms on Chrome, and about 2x that on Firefox.

Reusing sessions can significantly improve test throughput and latency by amortising the startup cost.

I'm currently investigating whether a session reset is good enough:

class Session
  # ...

  # Reset the session to a clean state.
  def reset!
    # Go to a blank page (in theory this should also invalidate any Element instances):
    self.navigate_to("about:blank")

    # Clear cookies and local storage:
    self.delete_all_cookies

    # This does not work consistently:
    # self.execute("localStorage.clear();")

    # Detach the session instance from the underlying HTTP client:
    @delegate = nil
  end

I think it would be pretty awesome if we had a session reset mechanism. Ideally it would return a new session ID (to invalidate any existing references) but reuse as much state as possible to reduce the overhead of creating a new session.

whimboo commented 7 months ago

Thanks for the suggestion. An API like that we considered useful and already implemented it for WebDriver BiDi. It's called session.end. Given that our focus is on the BiDi protocol which will completely replace WebDriver classic eventually, I don't think that we will get such an API added for it.

ioquatix commented 7 months ago

I don't think we should let "eventually completely replace" get in the way of useful functionality today. That being said, I'm probably not going to be the one implementing it.

whimboo commented 7 months ago

Note that there is a number of APIs that we are going to implement for BiDi only. And this is most likely one of these.

ioquatix commented 7 months ago

Does session.end actually implement a session reset? I feel like session.end means the session may not be used again. But what I'm asking for is a state reset of the given session, so we don't need to create the session for subsequent tests.

ioquatix commented 7 months ago

Sorry, but perhaps you can help with further clarification:

6.1.3.2. The session.new Command The session.new command allows creating a new BiDi session. NOTE: A session created this way will not be accessible via HTTP.

Regarding the NOTE, does that also apply that sessions created via HTTP are not accessible via the BiDi interface?

whimboo commented 2 months ago

Note that creating a new session with WebDriver BiDi is cheap. Note that it will not restart the browser as for WebDriver HTTP. So I don't see an issue here.

Regarding your last question, it is always possible to access a session as created via WebDriver HTTP from BiDi. But it's not possible the other way around.