-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
WysiwygTrait.php
90 lines (75 loc) · 2.69 KB
/
WysiwygTrait.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
<?php
declare(strict_types=1);
namespace DrevOps\BehatSteps;
use Behat\Mink\Exception\ElementHtmlException;
use Behat\Mink\Exception\ElementNotFoundException;
use Behat\Mink\Exception\UnsupportedDriverActionException;
/**
* Trait WysiwygTrait.
*
* Trait to handle WYSIWYG fields.
*/
trait WysiwygTrait {
use KeyboardTrait;
/**
* Set value for WYSIWYG field.
*
* If used with Selenium driver, it will try to find associated WYSIWYG and
* fill it in. If used with webdriver - it will fill in the field as normal.
*
* @When /^(?:|I )fill in WYSIWYG "(?P<field>(?:[^"]|\\")*)" with "(?P<value>(?:[^"]|\\")*)"$/
*/
public function wysiwygFillField(string $field, string $value): void {
$field = $this->wysiwygFixStepArgument($field);
$value = $this->wysiwygFixStepArgument($value);
$page = $this->getSession()->getPage();
$element = $page->findField($field);
if ($element === NULL) {
throw new ElementNotFoundException($this->getSession()->getDriver(), 'form field', 'id|name|label|value|placeholder', $field);
}
$driver = $this->getSession()->getDriver();
try {
$driver->evaluateScript('true');
}
catch (UnsupportedDriverActionException) {
// For non-JS drivers process field in a standard way.
$element->setValue($value);
return;
}
$element_id = $element->getAttribute('id');
if (empty($element_id)) {
throw new ElementHtmlException('ID is empty', $driver, $element);
}
$parent_element = $element->getParent();
// Support Ckeditor 4.
$is_ckeditor_4 = !empty($driver->find($parent_element->getXpath() . "/div[contains(@class,'cke')]"));
if ($is_ckeditor_4) {
$this->getSession()
->executeScript(sprintf('CKEDITOR.instances["%s"].setData("%s");', $element_id, $value));
return;
}
// Support Ckeditor 5.
$ckeditor_5_element_selector = sprintf('.%s .ck-editor__editable', $parent_element->getAttribute('class'));
$this->getSession()
->executeScript(
"
const domEditableElement = document.querySelector(\"{$ckeditor_5_element_selector}\");
if (domEditableElement.ckeditorInstance) {
const editorInstance = domEditableElement.ckeditorInstance;
if (editorInstance) {
editorInstance.setData(\"{$value}\");
} else {
throw new Exception('Could not get the editor instance!');
}
} else {
throw new Exception('Could not find the element!');
}
");
}
/**
* Returns fixed step argument (with \\" replaced back to ").
*/
protected function wysiwygFixStepArgument(string $argument): string {
return str_replace('\\"', '"', $argument);
}
}