0
0
Selenium Javatesting~10 mins

iFrame switching (switchTo.frame) in Selenium Java - Test Execution Trace

Choose your learning style9 modes available
Test Overview

This test opens a webpage containing an iframe, switches the Selenium WebDriver context to the iframe, verifies a button inside the iframe is clickable, clicks it, and then switches back to the main page.

Test Code - JUnit with Selenium WebDriver
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 java.time.Duration;
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.assertTrue;

public class IFrameSwitchTest {
    WebDriver driver;
    WebDriverWait wait;

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

    @Test
    public void testSwitchToIFrameAndClickButton() {
        driver.get("https://example.com/page-with-iframe");

        // Wait for iframe to be present and switch to it
        wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.id("iframe1")));

        // Now inside iframe context
        WebElement button = wait.until(ExpectedConditions.elementToBeClickable(By.id("btnInsideIframe")));
        assertTrue(button.isDisplayed(), "Button inside iframe should be visible");

        button.click();

        // Switch back to main page
        driver.switchTo().defaultContent();

        // Verify we are back by checking an element outside iframe
        WebElement header = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("mainHeader")));
        assertTrue(header.isDisplayed(), "Main page header should be visible after switching back");
    }

    @AfterEach
    public void tearDown() {
        if (driver != null) {
            driver.quit();
        }
    }
}
Execution Trace - 8 Steps
StepActionSystem StateAssertionResult
1Test starts and Chrome browser opensBrowser window is blank, ready to navigate-PASS
2Navigate to https://example.com/page-with-iframePage loads with main content and an iframe with id 'iframe1'Page loaded successfullyPASS
3Wait until iframe with id 'iframe1' is available and switch to itDriver context is now inside the iframeIframe is present and switched toPASS
4Wait for button with id 'btnInsideIframe' to be clickable inside iframeButton is visible and enabled inside iframeButton is displayed (assertTrue passes)PASS
5Click the button inside iframeButton click triggers expected action inside iframe-PASS
6Switch back to main page content using driver.switchTo().defaultContent()Driver context is back to main pageContext switched back to main pagePASS
7Wait for element with id 'mainHeader' to be visible on main pageMain page header is visibleHeader is displayed (assertTrue passes)PASS
8Test ends and browser closesBrowser window closed-PASS
Failure Scenario
Failing Condition: Iframe with id 'iframe1' is not found or not available within wait time
Execution Trace Quiz - 3 Questions
Test your understanding
What does the test do immediately after switching to the iframe?
ASwitches back to the main page
BClicks a button outside the iframe
CWaits for a button inside the iframe to be clickable
DCloses the browser
Key Result
Always wait explicitly for the iframe to be available before switching context to avoid NoSuchFrameException.