How do I test a CKEditor text format field with Behat/Mink?
-
28-02-2021 - |
Question
I am using an @javascript test in Behat on CircleCI to check my Drupal 8 site.
When I use the standard I fill in "my field" with "value"
step, it works for fields with the plain text format, but it fails for fields that use CKEditor.
How can I fill in fields on CKEditor?
La solution
This doesn't yet have official support in the Drupal Extension. The linked thread has some examples that use traits.
However, I found this gist by johnennewdeeson to be a faster, easier solution for me, just cut and paste into FeatureContext.php
:
/**
* @Then I fill in wysiwyg on field :locator with :value
*/
public function iFillInWysiwygOnFieldWith($locator, $value) {
$el = $this->getSession()->getPage()->findField($locator);
if (empty($el)) {
throw new ExpectationException('Could not find WYSIWYG with locator: ' . $locator, $this->getSession());
}
$fieldId = $el->getAttribute('id');
if (empty($fieldId)) {
throw new Exception('Could not find an id for field with locator: ' . $locator);
}
$this->getSession()
->executeScript("CKEDITOR.instances[\"$fieldId\"].setData(\"$value\");");
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à drupal.stackexchange