It seems that this function is not officially supported by selenium. But Tarun Lalwani has created working Java code to provide this function. See http://tarunlalwani.com/post/reusing-existing-browser-session-selenium-java/.
Here is a working code example copied from the link above:
public static RemoteWebDriver createDriverFromSession(final SessionId sessionId, URL command_executor){ CommandExecutor executor = new HttpCommandExecutor(command_executor) { @Override public Response execute(Command command) throws IOException { Response response = null; if (command.getName() == "newSession") { response = new Response(); response.setSessionId(sessionId.toString()); response.setStatus(0); response.setValue(Collections.<String, String>emptyMap()); try { Field commandCodec = null; commandCodec = this.getClass().getSuperclass().getDeclaredField("commandCodec"); commandCodec.setAccessible(true); commandCodec.set(this, new W3CHttpCommandCodec()); Field responseCodec = null; responseCodec = this.getClass().getSuperclass().getDeclaredField("responseCodec"); responseCodec.setAccessible(true); responseCodec.set(this, new W3CHttpResponseCodec()); } catch (NoSuchFieldException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } } else { response = super.execute(command); } return response; } }; return new RemoteWebDriver(executor, new DesiredCapabilities()); } public static void main(String [] args) { ChromeDriver driver = new ChromeDriver(); HttpCommandExecutor executor = (HttpCommandExecutor) driver.getCommandExecutor(); URL url = executor.getAddressOfRemoteServer(); SessionId session_id = driver.getSessionId(); RemoteWebDriver driver2 = createDriverFromSession(session_id, url); driver2.get("http://tarunlalwani.com"); }
For your test, RemoteWebDriver must be created from an existing browser session. To create this driver, you only need to know "session information", that is, the address of the server (local in our case) on which the browser is running, and the browser session ID. To get this information, we can create one browser session with selenium, open the desired page and, finally, run a real test script.
I do not know if there is a way to get information about a session that was not created by selenium.
Here is an example session information:
Remote Server Address: http: // localhost: 24266 . The port number is different for each session. Session ID: 534c7b561aacdd6dc319f60fed27d9d6.
testerjoe2
source share