Notes on how to intervene an existing selenium web driver session with impelementation and tests.
As a software engineer in automation test, sometimes it is the most reasonable method to capture the existing selenium web driver session and intervene with customized action steps. Especially with the commercial toolkit or homemade testing utility but source is not open at the meantime.
In my case, the toolkit is not able to fill up one of the authentication page. However, it offers a keyword to pause until a manual action to input user crediential. Current solution is to monitor the stdout with bash shell, when the typical log of “http://localhost:XXXX“ shows up, use the selenium commander URL to capture the existing selenium session. There is still a parameter of session ID missing. This can be solved by an JSON wired protocol end point, “/sessions”. Usually toolkit implements singleton on seleenium driver language binding. Query the end point of “/sessions” will return the session ID between web driver executable and the browser.
As mentioned in above, the communication between Selenium Development Language bindings and WebDriver executables is the JSON wired protocol.
Unfortunately this end point is not implemented with Selenium Java or Python binding. It could be soloved by a new created wrapper function to query existing sessions.
Generally automation test toolkit desktop version implements singleton on selenium binding instance and keep one session. So the query above will return one session in JSON. The solution is to add code parsing the return value and extract the session ID.
For example, if the selenium based toolkit generates stdout or logs as:
The session can be queried by URL in browser with the end point:
(Update Aug 08, 2017) The sample code to query session information. The codesnap only return the first session ID and capabilities in tuple. Production code shall take care of situation when there are multiple sessions as well.
(End of Aug 08, 2017 Update)
With an effective session ID and executing commander URL, the captured web driver class overwrites start_session method. Property “w3c” is assigned for Web Element Find method groups.
The unit test is implemented with doctest as a fast way for experiment codes.
The source code is shared at Github Folder now. Java binding solution is still under construction. After that, a separate Github project will be created to maintain this solution together with monitoring bash shell.
To tes the solution, open a selenium session and extract the session ID and commander URL. For a quick test, the existing github project cucumber-java-toy is used. The code is as below:
When the commander URL and session ID are available, the quick test could be performed with below Python snap.
Successful output of test code shall be:
Since browser is not running in headless mode, the page transition and backward movement action can also be visual checked that the captured web driver works fine and either driver can quit the session, which leads to a termination on web driver executable process, too.
Add query on “/sessions” end point for Selenium Java Binding.
Prototype passed test. (TODO: update github and maintain in separate repo).
Implement Captured Web Driver with Selenium Java Binding.
It works fine with GeckoDriver for Firefox on both Mac and Windows. However, IEDriverServer throws 404 error while performing “find*” API calls.
It finally reject the 2nd connection with Selenium Python binding. In the meantime, title/url property query through the JSON wired protocol works well.
Aug 06, 2017: Initial post as a quick note on captured session with selenium web driver.