We want to understand the difference between Close, Quit & Dispose commands?
Laymen terms: –
We are going for sleep inside our gorgeous house and charming room . It is really hot today but we can feel some cool breeze, so we would prefer not to close the windows of the house, but we simply need to close our entryway. After some time we realized that cool breeze has taken a shape of massive storm, immediately we need to close all the windows to feel safe. But we are really sleepy and lethargic so we asked our accomplice to close all the windows and we slept peacefully.
|My Lovely House|
Above story went bit long 🙂 , let’s try to relate the things now :-
1. Closing only the entryway is similar to closing only the current instance of browser on which selenium is working, it is a Close command.
2. Closing all windows on storm was Quit command, while asking our partner to do it was Dispose command. Thus internally Quit is calling Dispose command.
Enough after that boring and sleepy story let’s dive straight into technical terms,
Close: – This method is used to close the current open window. It closes the current open window on which driver has focus on.
Quit: – This method is used to destroy the instance of WebDriver. It closes all Browser Windows associated with that driver and safely ends the session. Quit() calls Dispose method to achieve this.
Dispose: – This method closes all Browser windows and safely ends the session
Suppose if we are working with only one window, close () and quit () technically do exactly same thing that is closes the browser/window and releases the memory by quitting the driver. But if in our test, it opens another window while execution and we wish to close one of the windows, to continue execution; we can switch the focus to desired window and perform close action on it. On other hand if we are done with the execution and we want all the browser instances to be killed and release the memory, Quit command is recommended.
Close Method:- In the below code, we open the “About Us” page in new window and we used Close method at the end, still this browser window is not closed.Thus only the Home page is closed as selenium was having focus on that page.
Quit Method:- All the opened windows are closed,which was opened by selenium.
For more Selenium Tutorials