Here, we'll be creating an automation script that manages frames and iframes. 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_04.html file in the Chrome browser.
- Create a new Java file for the automation script. Make sure you include the required libraries for the script to work.
- For the frames, create a method and name it checkFrames:
checkFrames();
- Using the driver.switchTo().frame() method, change the focus to the frame with the ID info (the focus can also be changed to a certain frame with its name, if it has one):
private static void checkFrames(){
WebDriver driver = new ChromeDriver();
try {
driver.get("https://trainingbypackt.github.io/
Beginning-Selenium/lesson_2/exercise02_concept_04.html");
driver.switchTo().frame("info");
If a frame does not have an ID or a name, it can be selected by its (zerobased) index.
- With the focus on the info frame, use the driver.getPageSource() method to obtain the frame's HTML contents, and verify that it contains the string "Frame Info":
if (driver.getPageSource().contains("Frame Info"))
- Using the System.out.println method, display a message that indicates whether the focus is on the info frame.
{
System.out.println("The script worked, the
focus was changed to Frame Info");
} else {
System.out.println("Something went wrong
with the script, the focus was not changed to Frame Info");
}
- Using the driver.switchTo().defaultContent() method, return the focus to the main page:
driver.switchTo().defaultContent();
- Using the driver.switchTo().frame() method, change the focus to the frame with the ID "title".
driver.switchTo().frame("title");
- With the focus on the title frame, use the driver.getPageSource() method to verify that the HTML content of the frame contains Frame Title:
if (driver.getPageSource().contains("Frame Title"))
- Using the System.out.println method, display a message that indicates whether the focus is on the title frame.
{
System.out.println("The script worked, the
focus was changed to Frame Title");
}
else {
System.out.println("Something went wrong
with the script, the focus was not changed to Frame Title");
}
-
Using the driver.switchTo().defaultContent() method, return the focus to the main page.
- For the iframes, create a method and name it checkIFrames:
checkIFrames();
- Using the driver.switchTo().frame() method, change the focus to the frame with the ID twitter (the focus can also be changed to a frame with its name, if it has one):
driver.switchTo().frame("twitter");
When a frame does not have an ID or a name, it can be selected by its (zerobased) index.
- With the focus on the twitter frame, use the driver.getPageSource() method to verify that the content of the page includes "Frame Twitter":
if (driver.getPageSource().contains("Frame Twitter"))
- Using the System.out.println method, display a message that indicates whether the focus is on the twitter frame.
{
System.out.println("The script worked, the
focus was changed to iFrame Twitter");
} else {
System.out.println("Something went wrong
with the script, the focus was not changed to m");
}
- To change the focus from the twitter frame to the <iframe> embedded within it, we will have to locate the web element with the findElement method (we will explain this subject in an upcoming chapter):
WebElement twitterFrame = driver.findElement(By.
tagName("iframe"));
driver.switchTo().frame(twitterFrame);
- Once we have located the <iframe> and have set the focus on it, we can manipulate its elements (we will explain this subject in an upcoming chapter):
WebElement button = driver.findElement(By.id("followbutton"));
button.click();
- Compile and execute the script.