Skip to content

Commit 638dacb

Browse files
authored
Merge pull request #1409 from seleniumbase/context-manager-for-frame-switching
Add a Context Manager method for switching into iframes using a "with" statement
2 parents 1cba7db + 2188407 commit 638dacb

File tree

7 files changed

+33
-4
lines changed

7 files changed

+33
-4
lines changed

examples/test_iframes.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,19 @@ def test_iframe_basics(self):
1717
self.switch_to_frame("iframeResult") # Go back inside 1st iframe
1818
self.highlight('iframe[title="Iframe Example"]')
1919

20+
def test_iframes_with_context_manager(self):
21+
self.open("https://seleniumbase.io/w3schools/iframes.html")
22+
with self.frame_switch("iframeResult"):
23+
self.assert_text("HTML Iframes", "h2")
24+
with self.frame_switch('[title*="Iframe"]'):
25+
self.assert_text("This page is displayed in an iframe", "h1")
26+
self.assert_text("Use CSS width & height to specify", "p")
27+
with self.frame_switch('[title*="Iframe"]'):
28+
self.assert_text("seleniumbase.io/w3schools/iframes", "a")
29+
self.click("button#runbtn")
30+
with self.frame_switch("iframeResult"):
31+
self.highlight('iframe[title="Iframe Example"]')
32+
2033
def test_set_content_to_frame(self):
2134
self.open("https://seleniumbase.io/w3schools/iframes.html")
2235
self.set_content_to_frame("iframeResult")

help_docs/method_summary.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,9 @@ self.switch_to_default_content()
209209

210210
self.switch_to_parent_frame()
211211

212+
with self.frame_switch(frame, timeout=None):
213+
# Indented Code Block for Context Manager (Must use "with")
214+
212215
self.set_content_to_frame(frame, timeout=None)
213216

214217
self.set_content_to_default(nested=False)

mkdocs_build/requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ keyring==23.6.0
1414
pkginfo==1.8.3
1515
Jinja2==3.1.2
1616
click==8.1.3
17-
zipp==3.8.0
17+
zipp==3.8.1
1818
ghp-import==2.1.0
1919
readme-renderer==35.0
2020
pymdown-extensions==9.5

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ pdfminer.six==20220524;python_version>="3.7"
128128

129129
coverage==5.5;python_version<"3.6"
130130
coverage==6.2;python_version>="3.6" and python_version<"3.7"
131-
coverage==6.4.1;python_version>="3.7"
131+
coverage==6.4.2;python_version>="3.7"
132132
pytest-cov==2.12.1;python_version<"3.6"
133133
pytest-cov==3.0.0;python_version>="3.6"
134134
flake8==3.7.9;python_version<"3.6"

seleniumbase/__version__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
# seleniumbase package
2-
__version__ = "3.5.0"
2+
__version__ = "3.5.1"

seleniumbase/fixtures/base_case.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ def test_anything(self):
4242
import time
4343
import unittest
4444
import urllib3
45+
from contextlib import contextmanager
4546
from selenium.common.exceptions import (
4647
ElementClickInterceptedException as ECI_Exception,
4748
ElementNotInteractableException as ENI_Exception,
@@ -2775,6 +2776,18 @@ def switch_to_parent_frame(self):
27752776
return
27762777
self.driver.switch_to.parent_frame()
27772778

2779+
@contextmanager
2780+
def frame_switch(self, frame, timeout=None):
2781+
""" Context Manager for switching into iframes.
2782+
Usage example:
2783+
with self.frame_switch("iframe"):
2784+
# Perform actions here that should be done within the iframe.
2785+
# The iframe is automatically exited after the "with" block ends.
2786+
"""
2787+
self.switch_to_frame(frame, timeout=timeout)
2788+
yield
2789+
self.switch_to_parent_frame()
2790+
27782791
def set_content_to_frame(self, frame, timeout=None):
27792792
"""Replaces the page html with an iframe's html from that page.
27802793
If the iframe contains an "src" field that includes a valid URL,

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@
254254
"coverage": [
255255
'coverage==5.5;python_version<"3.6"',
256256
'coverage==6.2;python_version>="3.6" and python_version<"3.7"',
257-
'coverage==6.4.1;python_version>="3.7"',
257+
'coverage==6.4.2;python_version>="3.7"',
258258
'pytest-cov==2.12.1;python_version<"3.6"',
259259
'pytest-cov==3.0.0;python_version>="3.6"',
260260
],

0 commit comments

Comments
 (0)