@@ -2515,7 +2515,7 @@ public function switchToWindow($name = null)
2515
2515
}
2516
2516
2517
2517
/**
2518
- * Switch to another frame on the page.
2518
+ * Switch to another iframe on the page.
2519
2519
*
2520
2520
* Example:
2521
2521
* ``` html
@@ -2538,23 +2538,62 @@ public function switchToWindow($name = null)
2538
2538
*/
2539
2539
public function switchToIFrame ($ locator = null )
2540
2540
{
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 ) {
2542
2578
$ this ->webDriver ->switchTo ()->defaultContent ();
2543
2579
return ;
2544
2580
}
2545
2581
$ els = null ;
2546
2582
try {
2547
- $ els = $ this ->_findElements ("iframe [name='$ locator'] " );
2583
+ $ els = $ this ->_findElements ("{ $ tag } [name=' $ locator'] " );
2548
2584
} catch (\Exception $ e ) {
2549
2585
$ this ->debug ('Failed to find locator by name: ' . $ e ->getMessage ());
2550
2586
}
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 ' );
2553
2590
$ els = $ this ->_findElements ($ locator );
2554
2591
}
2592
+
2555
2593
if (!count ($ els )) {
2556
- throw new ElementNotFound ($ locator , ' Iframe ' );
2594
+ throw new ElementNotFound ($ locator , ucfirst ( $ tag ) );
2557
2595
}
2596
+
2558
2597
$ this ->webDriver ->switchTo ()->frame ($ els [0 ]);
2559
2598
}
2560
2599
0 commit comments