From 7e87dbb80222130093e80421bc7df5e975544379 Mon Sep 17 00:00:00 2001 From: Anton Fedurtsya Date: Thu, 19 Mar 2020 14:05:11 +0200 Subject: [PATCH 1/3] Make switch to frame possible --- src/Codeception/Module/WebDriver.php | 50 +++++++++++++++++++++++++--- 1 file changed, 45 insertions(+), 5 deletions(-) diff --git a/src/Codeception/Module/WebDriver.php b/src/Codeception/Module/WebDriver.php index d72a0a27..c6f576a0 100644 --- a/src/Codeception/Module/WebDriver.php +++ b/src/Codeception/Module/WebDriver.php @@ -2540,22 +2540,62 @@ public function switchToWindow($name = null) */ public function switchToIFrame($locator = null) { - if (is_null($locator)) { + $this->findAndSwitchToFrame($locator, 'iframe'); + } + + /** + * Switch to another frame on the page. + * + * Example: + * ``` html + * + * + * ``` + * + * ``` php + * switchToFrame("another_frame"); + * # switch to frame by CSS or XPath + * $I->switchToFrame("#fr1"); + * # switch to parent page + * $I->switchToFrame(); + * + * ``` + * + * @param string|null $locator (name, CSS or XPath) + */ + public function switchToFrame($locator = null) + { + $this->findAndSwitchToFrame($locator, 'frame'); + } + + /** + * @param string|null $locator + * @param string $tag + */ + private function findAndSwitchToFrame($locator = null, $tag = 'frame') + { + if ($locator === null) { $this->webDriver->switchTo()->defaultContent(); return; } + try { - $els = $this->_findElements("iframe[name='$locator']"); + $els = $this->_findElements("{$tag}[name='$locator']"); } catch (\Exception $e) { $this->debug('Failed to find locator by name: ' . $e->getMessage()); } - if (!is_array($els) || !count($els)) { - $this->debug('Iframe was not found by name, locating iframe by CSS or XPath'); + + if (!isset($els) || !is_array($els) || !count($els)) { + $this->debug(ucfirst($tag) . ' was not found by name, locating iframe by CSS or XPath'); $els = $this->_findElements($locator); } + if (!count($els)) { - throw new ElementNotFound($locator, 'Iframe'); + throw new ElementNotFound($locator, ucfirst($tag)); } + $this->webDriver->switchTo()->frame($els[0]); } From 443881e9185051f39551f5234d2ebedc5dab3652 Mon Sep 17 00:00:00 2001 From: Anton Fedurtsya Date: Thu, 19 Mar 2020 14:18:00 +0200 Subject: [PATCH 2/3] Fix tag name in debug information --- src/Codeception/Module/WebDriver.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Codeception/Module/WebDriver.php b/src/Codeception/Module/WebDriver.php index c6f576a0..4428f089 100644 --- a/src/Codeception/Module/WebDriver.php +++ b/src/Codeception/Module/WebDriver.php @@ -2588,7 +2588,7 @@ private function findAndSwitchToFrame($locator = null, $tag = 'frame') } if (!isset($els) || !is_array($els) || !count($els)) { - $this->debug(ucfirst($tag) . ' was not found by name, locating iframe by CSS or XPath'); + $this->debug(ucfirst($tag) . ' was not found by name, locating ' . $tag . ' by CSS or XPath'); $els = $this->_findElements($locator); } From 9da999682ebdcd8cbff1d11deaee646942d74f61 Mon Sep 17 00:00:00 2001 From: Gintautas Miselis Date: Sun, 31 May 2020 11:32:44 +0300 Subject: [PATCH 3/3] Correct description of switchToIframe --- src/Codeception/Module/WebDriver.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Codeception/Module/WebDriver.php b/src/Codeception/Module/WebDriver.php index 70e7e4d2..09d82165 100644 --- a/src/Codeception/Module/WebDriver.php +++ b/src/Codeception/Module/WebDriver.php @@ -2515,7 +2515,7 @@ public function switchToWindow($name = null) } /** - * Switch to another frame on the page. + * Switch to another iframe on the page. * * Example: * ``` html