Skip to content

Instantly share code, notes, and snippets.

@MaksymKrut
Last active August 1, 2016 17:25
Show Gist options
  • Save MaksymKrut/cda321c5b4af1d241603c7ebb2096347 to your computer and use it in GitHub Desktop.
Save MaksymKrut/cda321c5b4af1d241603c7ebb2096347 to your computer and use it in GitHub Desktop.
Make Behat recognise unnamed iframes on the page. We do not care how to name them, just how to operate with them.
/**
* @Given I switch to iframe with locator :locator
* @param String $locator
*/
public function iSwitchToIFrameWithLocator($locator)
{
$value = $this->takeValueFromIniFile('locators', $locator);
$iframe = $this->getSession()->getPage()->find("css", $value);
$iframeName = $iframe->getAttribute("name");
if ($iframeName == "") {
echo "\n\niFrame has no name. Let's name it.\n\n";
$javascript = "(function(){
var iframes = document.getElementsByTagName('iframe');
for (var i = 0; i < iframes.length; i++) {
iframes[i].name = 'iframe_number_' + (i + 1) ;
}
})()";
$this->getSession()->executeScript($javascript);
$iframe = $this->getSession()->getPage()->find("css", $value);
$iframeName = $iframe->getAttribute("name");
echo "\n\niFrame has new name: " . $iframeName . "\n\n";
} else {
echo "\n\niFrame already has a name: " . $iframeName . "\n\n";
}
$this->getSession()->getDriver()->switchToIFrame($iframeName);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment