@@ -2515,7 +2515,7 @@ public function switchToWindow($name = null)
25152515 }
25162516
25172517 /**
2518- * Switch to another frame on the page.
2518+ * Switch to another iframe on the page.
25192519 *
25202520 * Example:
25212521 * ``` html
@@ -2538,23 +2538,62 @@ public function switchToWindow($name = null)
25382538 */
25392539 public function switchToIFrame ($ locator = null )
25402540 {
2541- if (is_null ($ locator )) {
2541+ $ this ->findAndSwitchToFrame ($ locator , 'iframe ' );
2542+ }
2543+
2544+ /**
2545+ * Switch to another frame on the page.
2546+ *
2547+ * Example:
2548+ * ``` html
2549+ * <frame name="another_frame" id="fr1" src="http://example.com">
2550+ *
2551+ * ```
2552+ *
2553+ * ``` php
2554+ * <?php
2555+ * # switch to frame by name
2556+ * $I->switchToFrame("another_frame");
2557+ * # switch to frame by CSS or XPath
2558+ * $I->switchToFrame("#fr1");
2559+ * # switch to parent page
2560+ * $I->switchToFrame();
2561+ *
2562+ * ```
2563+ *
2564+ * @param string|null $locator (name, CSS or XPath)
2565+ */
2566+ public function switchToFrame ($ locator = null )
2567+ {
2568+ $ this ->findAndSwitchToFrame ($ locator , 'frame ' );
2569+ }
2570+
2571+ /**
2572+ * @param string|null $locator
2573+ * @param string $tag
2574+ */
2575+ private function findAndSwitchToFrame ($ locator = null , $ tag = 'frame ' )
2576+ {
2577+ if ($ locator === null ) {
25422578 $ this ->webDriver ->switchTo ()->defaultContent ();
25432579 return ;
25442580 }
25452581 $ els = null ;
25462582 try {
2547- $ els = $ this ->_findElements ("iframe [name='$ locator'] " );
2583+ $ els = $ this ->_findElements ("{ $ tag } [name=' $ locator'] " );
25482584 } catch (\Exception $ e ) {
25492585 $ this ->debug ('Failed to find locator by name: ' . $ e ->getMessage ());
25502586 }
2551- if (!is_array ($ els ) || !count ($ els )) {
2552- $ this ->debug ('Iframe was not found by name, locating iframe by CSS or XPath ' );
2587+
2588+ if (!isset ($ els ) || !is_array ($ els ) || !count ($ els )) {
2589+ $ this ->debug (ucfirst ($ tag ) . ' was not found by name, locating ' . $ tag . ' by CSS or XPath ' );
25532590 $ els = $ this ->_findElements ($ locator );
25542591 }
2592+
25552593 if (!count ($ els )) {
2556- throw new ElementNotFound ($ locator , ' Iframe ' );
2594+ throw new ElementNotFound ($ locator , ucfirst ( $ tag ) );
25572595 }
2596+
25582597 $ this ->webDriver ->switchTo ()->frame ($ els [0 ]);
25592598 }
25602599
0 commit comments