Search code examples
node.jsseleniumsessionselenium-webdrivergeckodriver

Closing windows in selenium multi-window test


I am currently working on a project that requires using multiple windows each requiring different sessions and cookies.

After each window is created and does its work it needs to be closed. Then the process is repeated.

The windows are created in an array as shown.

var fOptions = new firefox.Options();
var profile = new firefox.Profile('./fProfile');
fOptions.setProfile(profile);    
driver[0] = new Builder().withCapabilities({'browserName': 'firefox'}).setFirefoxOptions(fOptions).build();
driver[1] .....

What would be the best way to close the windows: using driver[index].close() or driver[index].quit()

I sometimes run into this error This driver instance does not have a valid session ID (did you call WebDriver.quit()?) and may no longer be used. when using driver[index].quit()

And also get a lot of tmp-[abcd] folders in my TEMP directory am guessing from undeleted sessions.

I am open to any solutions that achieve new sessions on multi-window tests

I am using nodeJS implementation of Selenium with gecko driver.


Solution

  • As you are creating the windows in an array so the the best way to close the windows will be using :

    driver[index].close()
    

    Analysis

    When you invoke quit() the webdriver::server sends DELETE to the geckodriver::marionette for the entire session as follows :

    webdriver::server   DEBUG   -> DELETE /session/f84dbafc-4166-4a08-afd3-79b98bad1470 
    

    geckodriver inturn sends "quit" signal along with "eForceQuit" flag to marionette as follows :

    geckodriver::marionette TRACE   -> 37:[0,3,"quit",{"flags":["eForceQuit"]}]
    

    marionette finally generates the log that Marionette won't be accepting any further connections the cause being "shutdown" as follows :

    1518532363988   Marionette  DEBUG   New connections will no longer be accepted
    1518532364053   Marionette  TRACE   0 <- [1,3,null,{"cause":"shutdown"}]
    1518532364088   geckodriver::marionette TRACE   <- [1,3,null,{"cause":"shutdown"}]
    1518532364089   webdriver::server   DEBUG   Deleting session
    1518532364089   geckodriver::marionette DEBUG   Stopping browser process
    1518532364519   webdriver::server   DEBUG   <- 200 OK {"value": {}}
    

    Though your driver instances are indexed but as Marionette stops accepting connections hence you observe the following error :

    This driver instance does not have a valid session ID (did you call WebDriver.quit()?) and may no longer be used.
    

    Instead if you invoke close(), the webdriver::server sends DELETE to the geckodriver::marionette for the particular session windown only as follows :

    1518532935982   webdriver::server   DEBUG   -> DELETE /session/3694b8b7-89b1-4249-a710-0915ad2e867e/window 
    1518532935983   geckodriver::marionette TRACE   -> 16:[0,4,"close",{}]
    1518532935985   Marionette  TRACE   0 -> [0,4,"close",{}]   
    

    Hence the other Windows / TABs will remain unaffected.


    Update

    If you observe the GeckoDriver logs closely it is evident that Marionette scoops out a new moz:profile while creating a new session as follows :

    1518532230009   mozrunner::runner   INFO    Running command: "C:\\Program Files\\Mozilla Firefox\\firefox.exe" "-marionette" "-profile" "C:\\Users\\user_name\\AppData\\Local\\Temp\\rust_mozprofile.TxhOyDz3ozxL"
    

    This activity is managed and handled by the WebDriver instance i.e. the GeckoDriver . This workflow is in practice since we migrated from the Legacy Firefox to Marionette based Firefox and these stacks up in the temporary directory. As of now I don't see GeckoDriver cleaning up the chores at the end of the Test Execution. So a solution to clean up the rust_moz folders will be to run CCleaner tool regularly at-least before and after the execution of your Test Suite.

    You can find a detailed discussion on _“rust_mozprofile”_ directory in _Is it Firefox or Geckodriver, which creates “rust_mozprofile” directory_