Skip to content

Instantly share code, notes, and snippets.

@oksana-c
Created September 4, 2019 20:46
Show Gist options
  • Save oksana-c/3842b2896610663ae1f9597765cf710a to your computer and use it in GitHub Desktop.
Save oksana-c/3842b2896610663ae1f9597765cf710a to your computer and use it in GitHub Desktop.
<?php
/**
* Sets an id for the first iframe situated in the element specified by id.
* Needed when wanting to fill in WYSIWYG editor situated in an iframe without identifier.
*
* @Given /^the iframe in element "(?P<element>[^"]*)" has id "(?P<id>[^"]*)"$/
*/
public function theIframeInElementHasId($element_id, $iframe_id) {
$function = <<<JS
(function(){
var elem = document.getElementById("$element_id");
var iframes = elem.getElementsByTagName('iframe');
var f = iframes[0];
f.id = "$iframe_id";
})()
JS;
try {
$this->getSession()->executeScript($function);
}
catch(Exception $e) {
throw new \Exception(sprintf('No iframe found in the element "%s" on the page "%s".', $element_id, $this->getSession()->getCurrentUrl()));
}
}
/**
* Fills in WYSIWYG editor with specified id.
*
* @Given /^(?:|I )fill in "(?P<text>[^"]*)" in WYSIWYG editor "(?P<iframe>[^"]*)"$/
*/
public function iFillInInWYSIWYGEditor($text, $iframe) {
try {
$this->getSession()->switchToIFrame($iframe);
}
catch (Exception $e) {
throw new \Exception(sprintf("No iframe with id '%s' found on the page '%s'.", $iframe, $this->getSession()->getCurrentUrl()));
}
$this->getSession()->executeScript("document.body.innerHTML = '<p>".$text."</p>'");
$this->getSession()->switchToIFrame();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment