Merge pull request #1409 from seleniumbase/context-manager-for-frame-switching

Add a Context Manager method for switching into iframes using a "with" statement
This commit is contained in:
Michael Mintz 2022-07-12 22:58:34 -04:00 committed by GitHub
commit 638dacba89
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 33 additions and 4 deletions

View File

@ -17,6 +17,19 @@ class FrameTests(BaseCase):
self.switch_to_frame("iframeResult") # Go back inside 1st iframe
self.highlight('iframe[title="Iframe Example"]')
def test_iframes_with_context_manager(self):
self.open("https://seleniumbase.io/w3schools/iframes.html")
with self.frame_switch("iframeResult"):
self.assert_text("HTML Iframes", "h2")
with self.frame_switch('[title*="Iframe"]'):
self.assert_text("This page is displayed in an iframe", "h1")
self.assert_text("Use CSS width & height to specify", "p")
with self.frame_switch('[title*="Iframe"]'):
self.assert_text("seleniumbase.io/w3schools/iframes", "a")
self.click("button#runbtn")
with self.frame_switch("iframeResult"):
self.highlight('iframe[title="Iframe Example"]')
def test_set_content_to_frame(self):
self.open("https://seleniumbase.io/w3schools/iframes.html")
self.set_content_to_frame("iframeResult")

View File

@ -209,6 +209,9 @@ self.switch_to_default_content()
self.switch_to_parent_frame()
with self.frame_switch(frame, timeout=None):
# Indented Code Block for Context Manager (Must use "with")
self.set_content_to_frame(frame, timeout=None)
self.set_content_to_default(nested=False)

View File

@ -14,7 +14,7 @@ keyring==23.6.0
pkginfo==1.8.3
Jinja2==3.1.2
click==8.1.3
zipp==3.8.0
zipp==3.8.1
ghp-import==2.1.0
readme-renderer==35.0
pymdown-extensions==9.5

View File

@ -128,7 +128,7 @@ pdfminer.six==20220524;python_version>="3.7"
coverage==5.5;python_version<"3.6"
coverage==6.2;python_version>="3.6" and python_version<"3.7"
coverage==6.4.1;python_version>="3.7"
coverage==6.4.2;python_version>="3.7"
pytest-cov==2.12.1;python_version<"3.6"
pytest-cov==3.0.0;python_version>="3.6"
flake8==3.7.9;python_version<"3.6"

View File

@ -1,2 +1,2 @@
# seleniumbase package
__version__ = "3.5.0"
__version__ = "3.5.1"

View File

@ -42,6 +42,7 @@ import textwrap
import time
import unittest
import urllib3
from contextlib import contextmanager
from selenium.common.exceptions import (
ElementClickInterceptedException as ECI_Exception,
ElementNotInteractableException as ENI_Exception,
@ -2775,6 +2776,18 @@ class BaseCase(unittest.TestCase):
return
self.driver.switch_to.parent_frame()
@contextmanager
def frame_switch(self, frame, timeout=None):
""" Context Manager for switching into iframes.
Usage example:
with self.frame_switch("iframe"):
# Perform actions here that should be done within the iframe.
# The iframe is automatically exited after the "with" block ends.
"""
self.switch_to_frame(frame, timeout=timeout)
yield
self.switch_to_parent_frame()
def set_content_to_frame(self, frame, timeout=None):
"""Replaces the page html with an iframe's html from that page.
If the iframe contains an "src" field that includes a valid URL,

View File

@ -254,7 +254,7 @@ setup(
"coverage": [
'coverage==5.5;python_version<"3.6"',
'coverage==6.2;python_version>="3.6" and python_version<"3.7"',
'coverage==6.4.1;python_version>="3.7"',
'coverage==6.4.2;python_version>="3.7"',
'pytest-cov==2.12.1;python_version<"3.6"',
'pytest-cov==3.0.0;python_version>="3.6"',
],