Test Overview
This test verifies that after switching to an iframe and interacting with elements inside it, the test correctly switches back to the main page (default content) to interact with elements outside the iframe.
This test verifies that after switching to an iframe and interacting with elements inside it, the test correctly switches back to the main page (default content) to interact with elements outside the iframe.
from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC import unittest class TestDefaultContentSwitching(unittest.TestCase): def setUp(self): self.driver = webdriver.Chrome() self.driver.get('https://example.com/page_with_iframe') self.wait = WebDriverWait(self.driver, 10) def test_switch_to_iframe_and_back(self): driver = self.driver wait = self.wait # Wait for iframe and switch to it iframe = wait.until(EC.presence_of_element_located((By.ID, 'iframe1'))) driver.switch_to.frame(iframe) # Inside iframe: find button and click button_in_iframe = wait.until(EC.element_to_be_clickable((By.ID, 'btnInsideIframe'))) button_in_iframe.click() # Switch back to default content driver.switch_to.default_content() # On main page: find input and enter text input_main = wait.until(EC.presence_of_element_located((By.ID, 'mainInput'))) input_main.send_keys('Test input') # Assert input value self.assertEqual(input_main.get_attribute('value'), 'Test input') def tearDown(self): self.driver.quit() if __name__ == '__main__': unittest.main()
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test starts and browser opens the URL 'https://example.com/page_with_iframe' | Browser displays the main page with an iframe element with id 'iframe1' | - | PASS |
| 2 | Waits for iframe with id 'iframe1' to be present and switches to it | Driver context is inside the iframe content | Iframe element is located and switched to | PASS |
| 3 | Waits for button with id 'btnInsideIframe' inside iframe and clicks it | Button inside iframe is clicked | Button is clickable and click action performed | PASS |
| 4 | Switches back to default content (main page) | Driver context is back to main page content | Driver is no longer inside iframe | PASS |
| 5 | Waits for input with id 'mainInput' on main page and enters text 'Test input' | Input field on main page contains text 'Test input' | Input field value equals 'Test input' | PASS |
| 6 | Test ends and browser closes | Browser closed | - | PASS |