Skip to content

Update switchToIFrame #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 13 additions & 11 deletions src/Codeception/Module/WebDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -2521,37 +2521,39 @@ public function switchToWindow($name = null)
*
* Example:
* ``` html
* <iframe name="another_frame" src="http://example.com">
* <iframe name="another_frame" id="fr1" src="http://example.com">
*
* ```
*
* ``` php
* <?php
* # switch to iframe
* # switch to iframe by name
* $I->switchToIFrame("another_frame");
* # switch to iframe by CSS or XPath
* $I->switchToIFrame("#fr1");
* # switch to parent page
* $I->switchToIFrame();
*
* ```
*
* @param string|null $name
* @param string|null $locator (name, CSS or XPath)
*/
public function switchToIFrame($name = null)
public function switchToIFrame($locator = null)
{
if (is_null($name)) {
if (is_null($locator)) {
$this->webDriver->switchTo()->defaultContent();
return;
}
try {
$this->webDriver->switchTo()->frame($name);
$els = $this->_findElements("iframe[name='$locator']");
} catch (\Exception $e) {
$this->debug('Iframe was not found by name, locating iframe by CSS or XPath');
$frames = $this->_findElements($name);
if (!count($frames)) {
throw $e;
}
$this->webDriver->switchTo()->frame($frames[0]);
$els = $this->_findElements($locator);
}
if (!count($els)) {
throw new ElementNotFound($selector, "Iframe was not found by CSS or XPath");
}
$this->webDriver->switchTo()->frame($els[0]);
}

/**
Expand Down