Managing Frames in an Automation Script

Here, we'll be creating an automation script that manages frames and iframes. The steps for completion of this process are as follows:

  1. Review and analyze the structure of the https://trainingbypackt.github.io/Beginning-Selenium/lesson_2/exercise02_concept_04.html file in the Chrome browser.
  2. Create a new Java file for the automation script. Make sure you include the required libraries for the script to work.
  3. For the frames, create a method and name it checkFrames:
checkFrames();
  1. 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.

  1. 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"))
  1. 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");
}
  1. Using the driver.switchTo().defaultContent() method, return the focus to the main page:
driver.switchTo().defaultContent();
  1. Using the driver.switchTo().frame() method, change the focus to the frame with the ID "title".
driver.switchTo().frame("title");
  1. 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"))
  1. 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");
}
  1. Using the driver.switchTo().defaultContent() method, return the focus to the main page.

  2. For the iframes, create a method and name it checkIFrames:
checkIFrames();
  1. 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.

  1. 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"))
  1. 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");
}
  1. 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);
  1. 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();
  1. Compile and execute the script.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.133.131.168