Frames
Two iframes on one page. You cannot touch anything inside them until you switch into the frame – and you must switch back afterwards.
Frame 1
Frame 2 (smaller)
Inside each frame there is a heading with the id frame-heading and a button with the id frame-button.
This text is outside the frames.
Practice tasks
- Switch into frame 1 and assert the heading text is This is frame 1.
- Click the button inside frame 1 and assert the message inside that frame appears.
- Switch back to the main page with
switchTo().defaultContent()and read the outside text. - Switch into frame 2 and assert its heading is different from frame 1.
- Count how many iframes are on the page.
Sample automation code
driver.switchTo().frame("frame1");
Assert.assertEquals(driver.findElement(By.id("frame-heading")).getText(), "This is frame 1");
driver.findElement(By.id("frame-button")).click();
driver.switchTo().defaultContent();
Assert.assertTrue(driver.findElement(By.id("outside-message")).isDisplayed());
Assert.assertEquals(driver.findElements(By.tagName("iframe")).size(), 2);
const frame1 = page.frameLocator('#frame1');
await expect(frame1.locator('#frame-heading')).toHaveText('This is frame 1');
await frame1.locator('#frame-button').click();
await expect(frame1.locator('#frame-message')).toBeVisible();
await expect(page.getByTestId('outside-message')).toBeVisible(); // no switching back needed