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.
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.
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(); } } }
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test starts and Chrome browser opens | Browser window is blank, ready to navigate | - | PASS |
| 2 | Navigate to https://example.com/page-with-iframe | Page loads with main content and an iframe with id 'iframe1' | Page loaded successfully | PASS |
| 3 | Wait until iframe with id 'iframe1' is available and switch to it | Driver context is now inside the iframe | Iframe is present and switched to | PASS |
| 4 | Wait for button with id 'btnInsideIframe' to be clickable inside iframe | Button is visible and enabled inside iframe | Button is displayed (assertTrue passes) | PASS |
| 5 | Click the button inside iframe | Button click triggers expected action inside iframe | - | PASS |
| 6 | Switch back to main page content using driver.switchTo().defaultContent() | Driver context is back to main page | Context switched back to main page | PASS |
| 7 | Wait for element with id 'mainHeader' to be visible on main page | Main page header is visible | Header is displayed (assertTrue passes) | PASS |
| 8 | Test ends and browser closes | Browser window closed | - | PASS |