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

Connect to client if exists? #111

Closed
deven96 opened this issue Nov 10, 2020 · 5 comments
Closed

Connect to client if exists? #111

deven96 opened this issue Nov 10, 2020 · 5 comments

Comments

@deven96
Copy link

deven96 commented Nov 10, 2020

I'm curious as to whether it is possible to connect to an existing client if it has already been started somewhere rather than have Client::new() fail. Something like a Client::new_or_connect() that enables one to open a new window and do something else.

@jonhoo
Copy link
Owner

jonhoo commented Nov 14, 2020

I think in theory that should be possible if you know the session ID. What use-case are you thinking of?

@deven96
Copy link
Author

deven96 commented Nov 15, 2020

I think in theory that should be possible if you know the session ID. What use-case are you thinking of?

I was thinking of an async web application that made use of fantoccini. Since only Client::new can be used and it fails when called elsewhere if there is a client already running, then exporting the session ID as an environmental variable and using that in Client::new_or_connect() where it tries to create a new client but if that fails, connects to the existing client with that session ID.

If you guide me then perhaps I could even make this contribution myself

@jonhoo
Copy link
Owner

jonhoo commented Nov 15, 2020

Client::new should not fail just because another session is already open. I believe this is only the case for geckodriver, and I believe it should be considered a bug there. Works fine with chromedriver for example (this is why the firefox tests in fantoccini all have #[serial]).

The challenge with new_or_connect is how you get the session ID of an existing session. I don't know if WebDriver provides an API endpoint to ask for current sessions — if it does, maybe we could use that? Otherwise, I think it'd have to be up to the application to re-use sessions by adding a Client::from_session_id where the application manually inputs the session ID that it already got from another Client::new. In either case you'll have to be a little careful about when you close the session though, since it may be being used by multiple Clients.

Another concern is that if you have multiple Clients connected through the same session, they are no longer independent, which may make them kind of useless. Imagine you have two tests that share a Client, and one tries to go to foo.com and the other bar.com. Since they are controlling the same browser session, the requests will end up racing with one another, and the browser will end up on one site or the other, not both.

@deven96
Copy link
Author

deven96 commented Nov 16, 2020

Ah yes I see what you mean. The most appropriate function might be a Client::from_session_id(). About the racing requests, I think that would be up to the programmer to think about how to manage that, either by opening a new window or some other way

@jonhoo
Copy link
Owner

jonhoo commented Apr 30, 2022

I'm going to close this in favor of #57

@jonhoo jonhoo closed this as completed Apr 30, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants