In Testing, it is essential to understand how to interact with the browser and locate the DOM elements. It is easy to handle windows with Robot Framework. In this blog, we will learn about how to handle windows using the selenium library.
To handle windows there are a few commands provided by the selenium library in this blog we will discuss-
- Project setup
- Getting titles, names, and identifiers of window
- Set and get window positions
- Switching windows
Project setup for textbox:
Firstly, we must install all prerequisite libraries in our project (for more info about prerequisites you can visit- https://www.neovasolutions.com/2022/07/21/robot-framework-automation-using-selenium-and-python/ )
After this, we can check our libraries by pip list command, or we can check manually also
File > settings > project: robot-automation > python interpreter

After verifying the prerequisite part create a file workingwithwindow.robot in the TestCases directory.
— go to the new file > give a name of with .robot >Enter


Keywords:
- Get Window Handles – Returns all child windows from the browser as a list.
- Get Window Identifiers – Return and log id attribute of windows from browser
- Get Window Names – Return and log the name of windows from the browser
- Get Window Titles – Return and log titles of windows from browser
- Set Window Position – Sets the windows position on x, y coordinate.
- Get Window Position – Returns windows positions
- Set Window Size –Sets window size
- Get Window Size – Returns window size
- Switch Window –Switch to window browser matching locator
- Close Window– Close the window from browser
Now let us write a test case for a better understanding of windows Keywords.
TestCase:
Step1: Go to https://demo.automationtesting.in/Windows.html
Step2: Click on the button which will open a new window.
Step3: Get window titles, names, and identifiers.
Step4: Set window position-100, 300
Step5: Set window size- 800, 600
Step6: Switch the window and close it.
*** Settings ***
Library SeleniumLibrary
*** Variables ***
*** Test Cases ***
WindowsTab
open browser http://demo.automationtesting.in/Windows.html chrome
maximize browser window
wait until element is visible xpath:(//button[@class='btn btn-info'])[1]
click element xpath:(//button[@class='btn btn-info'])[1]
@{WindowHandles}= get window handles
sleep 4s
@{WindowIdentifier}= get window identifiers
sleep 4s
@{WindowsNames}= get window names
sleep 4s
@{WindowsTitle}= get window titles
sleep 4s
set window position 100 300
${x} ${y}= get window position
log ${x}
log ${y}
sleep 4
set window size 800 600
${width} ${height}= get window size
log ${width}
log ${height}
sleep 4
switch window title=Frames & windows
sleep 4s
switch window title=Selenium
close window
*** Keywords ***
***Settings*** – To work with robot framework keywords we need to import the selenium library in the settings section because all these keywords are built in the selenium library-
Library SeleniumLibrary
Similarly, we can import other libraries, resource files, and variable files as per our project requirement in this section.
***Variables***– This section contains the defined variables that are used commonly, here we declared variables of browser and URL so we can reuse these variables.
${url} ${browser}
In the same way, we can create more variables like this according to the requirement of the project.
***Test Cases***-

Code Explanation:
The first step in this test is to visit the URL https://demo.automationtesting.in/Register.html in Chrome. We have already set up variables for this. Then we gave a name to our test HandlingAlerts. Open Browser * This keyword opens a specific browser for the given URL and maximize browser window for maximizing the current browser window. Now we have one other keyword to wait for, wait until element is visible by this keyword test will not run until the element of the locator is not visible.
Now by the keywords of get window handles, get window identifiers, get window names, get window titles we can get names, titles, identifiers, and here we are storing these in a variable ${windowshandle}, ${windowsidentifiers}, ${windowsnames}, ${windowstitles}.
After this, our next keyword is to set the window position at 100x and 300y and after setting this we can get the window position too after storing in a variable by get window position keyword.
Similarly, we can set window size by set window size keyword 800 width and 600 height we can also get these by get window size keyword.
Now for closing the window we must identify windows with their titles. Switch window keyword works for switching the window and it identifies by the title and then close the specific window.
Execution:
To execute the testcase we need to use the command on terminal:

After execution, we should get the result as a pass.

Report:


Conclusion:
In this Blog, we learned how to handle windows using the Selenium library in the Robot Framework. With the keywords provided by the robot framework and the library imported, we can set or get window position, name, title, and identifiers.