Test Overview
This test opens a webpage with nested frames. It switches to the inner frame inside the outer frame and verifies the text inside the inner frame.
This test opens a webpage with nested frames. It switches to the inner frame inside the outer frame and verifies the text inside the inner frame.
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.WebDriverWait; import org.openqa.selenium.support.ui.ExpectedConditions; 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; public class NestedFramesTest { WebDriver driver; WebDriverWait wait; @BeforeEach public void setUp() { driver = new ChromeDriver(); wait = new WebDriverWait(driver, 10); } @Test public void testNestedFramesText() { driver.get("https://the-internet.herokuapp.com/nested_frames"); // Switch to the outer frame named 'frame-top' wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt("frame-top")); // Switch to the inner frame named 'frame-middle' wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt("frame-middle")); // Find the content element inside the inner frame WebElement content = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("content"))); // Verify the text inside the inner frame assertEquals("MIDDLE", content.getText()); } @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 open and ready | — | PASS |
| 2 | Navigate to URL 'https://the-internet.herokuapp.com/nested_frames' | Page with nested frames is loaded | Page loaded successfully | PASS |
| 3 | Wait and switch to outer frame named 'frame-top' | Driver context is inside 'frame-top' frame | Frame 'frame-top' is available and switched | PASS |
| 4 | Wait and switch to inner frame named 'frame-middle' inside 'frame-top' | Driver context is inside 'frame-middle' frame nested in 'frame-top' | Frame 'frame-middle' is available and switched | PASS |
| 5 | Find element with id 'content' inside 'frame-middle' | Element with text 'MIDDLE' is visible inside inner frame | Element with id 'content' is found and visible | PASS |
| 6 | Assert that the text of element 'content' equals 'MIDDLE' | Text inside inner frame is 'MIDDLE' | Text equals 'MIDDLE' | PASS |
| 7 | Test ends and browser closes | Browser window is closed | — | PASS |
switchTo().frame().defaultContent() returns to the main page, parentFrame() goes up one level, and window() switches browser windows, not frames.driver.switchTo().frame() step-by-step for each frame level. -> Option Aframe() method accepts a String for frame name/id, so it must be in quotes.driver.switchTo().frame("frame1"); uses correct Java syntax with double quotes and parentheses.driver.switchTo().frame("outerFrame");
driver.switchTo().frame("innerFrame");
String text = driver.findElement(By.id("message")).getText();
System.out.println(text);getText() returns "Hello Nested Frames".driver.switchTo().frame("innerFrame");
String text = driver.findElement(By.id("msg")).getText();frameA contains frameB, which contains frameC. You want to click a button inside frameC. Which sequence of Selenium Java commands correctly switches to frameC and clicks the button with id "btnSubmit"?frameC is inside frameB, which is inside frameA, you must switch step-by-step: first to frameA, then frameB, then frameC.frameC, find the button by id "btnSubmit" and click it.Pass the test and this pattern moves unknown