Skip to main content

_java

TL;DR - Show Me The Code

3-work-with-frames/code/java/src/test/java/com/elemental/selenium/FramesTest.java
package com.elemental.selenium;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Assertions;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.By;
import org.openqa.selenium.firefox.FirefoxDriver;

public class FramesTest {
WebDriver driver;

@BeforeEach
public void beforeEach() {
driver = new FirefoxDriver();
}

@AfterEach
public void afterEach() {
if (driver != null) {
driver.quit();
}
}

@Test
public void exampleOne() throws Exception {
driver.get("https://the-internet.herokuapp.com/nested_frames");
driver.switchTo().frame("frame-top");
driver.switchTo().frame("frame-middle");
Assertions.assertEquals("MIDDLE", driver.findElement(By.id("content")).getText());
}

@Test
public void exampleTwo() throws Exception {
driver.get("https://the-internet.herokuapp.com/tinymce");
driver.switchTo().frame("mce_0_ifr");
WebElement editor = driver.findElement(By.id("tinymce"));
String beforeText = editor.getText();
editor.clear();
editor.sendKeys("Hello World!");
String afterText = editor.getText();
Assertions.assertNotEquals(beforeText, afterText);
driver.switchTo().defaultContent();
Assertions.assertEquals("An iFrame containing the TinyMCE WYSIWYG Editor",
driver.findElement(By.cssSelector("h3")).getText());
}

}

Code Walkthrough

Importing Libraries, Setup and Teardown

Lines 3 to 10 are importing our requisite classes (for annotations (e.g., org.junit.After, etc.), driving the browser with Selenium (e.g., org.openqa.selenium.WebDriver, etc.), and matchers for our assertions (e.g., org.junit.jupiter.api.Assertions, etc.)) and start our class with some setup and teardown methods.

Next, we'll start our class off with some setup and teardown methods, covered in lines 13 to 25.

Example 1

Line 28 shows our first test. In it, we'll step through an example of nested frames from the-internet.

With Selenium's .switchTo() method we can easily switch to the frame we want. When using it for frames (e.g., driver.switchTo().frame();) it accepts either an ID or name attribute. But in order to get the text of the middle frame (e.g., a frame nested within another frame), we need to switch to the parent frame (e.g., the top frame) first and then switch to the child frame (e.g., the middle frame).

Once we've done that we're able to find the element we need, grab its text, and assert that it's what we expect.

While this example helps illustrate the point of frame switching, it's not very practical.

Example 2

Here is a more likely example you'll run into -- working with a WYSIWYG Editor like TinyMCE. You can see the page we're testing here.

Once the page loads we switch into the frame that contains TinyMCE and...

  • grab the original text and store it
  • clear and input new text
  • grab the new text value
  • assert that the original and new texts are not the same

Keep in mind that if we need to access a part of the page outside the frame we're currently in we'll need to switch to it. Thankfully Selenium has a method that enables us to quickly jump back to the top level of the page -- driver.switchTo().defaultContent();.

Executing the Test

Before executing the test, we need to make sure the required dependencies are declared on the pom.xml file.

Toggle to see the pom.xml file.
3-work-with-frames/code/java/pom.xml
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.elemental.selenium</groupId>
<artifactId>tips</artifactId>
<version>1.0.0</version>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>

<dependencies>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.24.0</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.11.0</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>

Finally, we can run the test by executing mvn test from the command-line.