Here, we'll be creating an automation script that manages windows. The steps for completion of this process are as follows:
- Review and analyze the structure of the https://trainingbypackt.github.io/Beginning-Selenium/lesson_2/exercise02_concept_05.html file.
- Create a new Java file for the automation script using IntelliJ IDEA. Make sure that you include the required libraries for the script to work.
- Using the driver.getWindowHandle() method, save the handle of the parent window in a String variable:
String parentWindowHandle = driver.getWindowHandle();
- Using the driver.switchTo().window(""); method, switch the focus to the TwitterWindow:
driver.switchTo().window("TwiterWindow");
- With the focus on "TwitterWindow", use the driver.getTitle() method to verify that the title of the page is Frame Twitter:
if (driver.getTitle().equalsIgnoreCase("Frame Twitter"))
- Using the System.out.println method, display a message that indicates whether the focus is on the TwitterWindow:
{
System.out.println("The script worked, the
window title is Frame Twitter");
} else {
System.out.println("Something went wrong,
the window title is NOT Frame Twitter");
}
- Using the driver.close() method, close "TwitterWindow":
driver.close();
- Return the focus to the parent window by using the windowHandle that you received on step 1.
- With the focus on the parent window, use the driver.getTitle() method to verify that the title of the page is Lesson 2.
- Compile and run the script.
You should now have a better understanding of how to navigate, resize, and switch between windows, and also how to handle different interactions with alerts.