How to Handle Multiple Windows in Selenium
Handle Multiple Windows in Selenium
This is a way of directing all the selenium commands to one single window. So automatically by default, every command that the user types in the Selenium web driver will get directed to the Main window from all the child windows. So for us to divert the attention from the child window to the main window, the related syntax can be used.
It takes the user command and diverts the driver control to the main window. As soon as the control changes, the entire driver command will now start going to the new window. At this juncture, we should clearly understand that the window switch is essential and should definitely happen in order to enable a smooth transition.
This technique is used in selenium Web Driver in order to handle a single window and multiple windows. In this technique, we will be using three different methods. They are
- getwindowHandle(),
- getwindowHandles() and
- switchTo().
getwindowHandle() method is used to handle the single window and return type “String”.
The window handle is based on the unique string value we will identify each window. Selenium is not very supportive of Windows apps. Basically, Selenium is a quality assurance testing tool based on automation that only supports web application testing. Owing to this reason, we cannot handle any pop-ups, notifications or error messages that arise in Selenium whichever are windows related. But then there is always a possibility of handling selenium via other tools namely Auto IT, Robot class, etc.
Some web applications can be opened in more than one window. Web Driver assigns a special and distinct id to every window as soon as the web Driver object is instantiated. This distinct ID comprising of alphabets and numbers is called window handle in technical terms.
Prerequisites to run a test case:
The most primary and first thing to do is to first understand which programming language to use. Java is one of the most powerful coding languages because of its simple and easy understandable nature and powerful operations and functionalities it can perform. Next to that, we would need an IDE that will act as a base that can keep the test scripts running. Suitable examples are NetBeans, Eclipse and numerous other ones but the most preferred one for most people by popular opinion is Eclipse owing to its high efficiency in running programs.
Other selenium plugins like Selenium standalone server, JAR files, and IDE are also integral components that are essential to keep things going. Then the most obvious factor is the browser drivers which are Google Chrome, Opera, Internet Explorer, etc.
For example