Reputation: 117
I have to verify text/messages inside an iframe wihout a name/id. After exploring, I found out that solution to add id/name and use switchToIFrame(id/name). How can I set id/name to a node element in mink? SetAttribute() is not a supported method in nodelement and setValue() is not supported for a frame.
<html>
<body>
<div class="div1">
<iframe class="xyz">
<!DOCTYPE html>
<html>
<body>
<div class="frame"></div>
<p>The paragraph1</p>
</body>
</html>
</body>
</html>
My context file is
public function iShouldSeeDescription($arg1)
{
$expected = "The paragraph1";
$iframe=$this->getSession ()->getPage ()->find ( 'xpath', '//div[contains(@class,"div1")]/iframe[contains(@class,"xyz")]');
$iframe->setAttribute('id', 'iframe_id');
$iframeId = $iframe->getAttribute('id');
$this->getSession()->switchToIframe("$iframeId");
$actual = $this->getSession()->getPage()->find('xpath', '//div[@class="frame"]/p[1]');
if ($actual === null) {
throw new \InvalidArgumentException ( sprintf ( 'null:%s', $arg1 ) );
}
if (!($actual === $expected)) {
throw new \InvalidArgumentException ( sprintf ( 'The Acutal description:%s and expected description:%s did not match ', $actual, $expected ) );
}
}
Upvotes: 2
Views: 2542
Reputation: 36
put the number or a for loop to iterate which one is required then set the number works with the cross origin
$this->getSession()->getDriver()->switchToIFrame(0);
Upvotes: 0
Reputation: 4173
What you could do to set id/name to an iframe is to use javascript.
Create a method like switchToIFrameBySelector($iframeSelector) that waits for the given element to be available then you can execute a javascript code to set a name/id for your iframe and after use switchToIFrame method.
For executing the js script you can use executeScript method in a try-catch and throw a custom Exception if needed.
public function switchToIFrame($iframeSelector){
$function = <<<JS
(function(){
var iframe = document.querySelector("$iframeSelector");
iframe.name = "iframeToSwitchTo";
})()
JS;
try{
$this->getSession()->executeScript($function);
}catch (Exception $e){
print_r($e->getMessage());
throw new \Exception("Element $iframeSelector was NOT found.".PHP_EOL . $e->getMessage());
}
$this->getSession()->getDriver()->switchToIFrame("iframeToSwitchTo");
}
Another way to set the id/name of the element in js is to use:
setAttribute('name', 'iframeToSwitchTo')
Upvotes: 5