0
0
Selenium Javatesting~15 mins

Nested frames in Selenium Java - Build an Automation Script

Choose your learning style9 modes available
Verify text inside nested frames
Preconditions (2)
Step 1: Open the web page with nested frames
Step 2: Switch to the outer frame by its name or ID 'frame1'
Step 3: Within the outer frame, switch to the inner frame by its name or ID 'frame2'
Step 4: Locate the paragraph element with id 'nestedText' inside the inner frame
Step 5: Verify that the text of this paragraph is exactly 'Nested Frame Content'
Step 6: Switch back to the default content
✅ Expected Result: The paragraph inside the nested frame contains the text 'Nested Frame Content' and the test completes without errors.
Automation Requirements - Selenium WebDriver with Java
Assertions Needed:
Verify the paragraph text inside the nested frame matches 'Nested Frame Content'
Best Practices:
Use explicit waits to ensure frames and elements are available before switching or interacting
Use By.id or By.name locators for frames and elements for better reliability
Switch back to default content after frame interactions
Handle NoSuchFrameException and NoSuchElementException gracefully
Automated Solution
Selenium Java
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.openqa.selenium.NoSuchFrameException;
import org.openqa.selenium.NoSuchElementException;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.time.Duration;

public class NestedFramesTest {
    private WebDriver driver;
    private WebDriverWait wait;

    @BeforeEach
    public void setUp() {
        driver = new ChromeDriver();
        wait = new WebDriverWait(driver, Duration.ofSeconds(10));
    }

    @Test
    public void testNestedFrameText() {
        driver.get("https://example.com/nested_frames_page");

        try {
            // Wait for outer frame and switch
            wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.name("frame1")));

            // Wait for inner frame and switch
            wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.name("frame2")));

            // Wait for paragraph element inside inner frame
            WebElement nestedText = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("nestedText")));

            // Assert the text content
            assertEquals("Nested Frame Content", nestedText.getText(), "Text inside nested frame did not match.");

        } catch (NoSuchFrameException e) {
            throw new AssertionError("Frame not found: " + e.getMessage());
        } catch (NoSuchElementException e) {
            throw new AssertionError("Element not found inside frame: " + e.getMessage());
        } finally {
            // Switch back to default content
            driver.switchTo().defaultContent();
        }
    }

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

This test opens the web page with nested frames.

It waits explicitly for the outer frame named 'frame1' to be available and switches to it.

Then it waits for the inner frame named 'frame2' inside the outer frame and switches to it.

Inside the inner frame, it waits for the paragraph element with id 'nestedText' to be visible.

It asserts that the text of this paragraph exactly matches 'Nested Frame Content'.

Finally, it switches back to the main page content to avoid side effects for other tests.

Setup and teardown methods initialize and close the browser properly.

Explicit waits ensure the frames and elements are ready before interacting, preventing flaky tests.

Common Mistakes - 4 Pitfalls
Not switching back to the default content after working inside frames
Using Thread.sleep() instead of explicit waits
Using incorrect or brittle locators like absolute XPath for frames
Not handling exceptions like NoSuchFrameException
Bonus Challenge

Now add data-driven testing with 3 different nested frame URLs and expected texts

Show Hint