View source
<?php
namespace Drupal\Tests\codemirror_editor\FunctionalJavascript;
use Drupal\FunctionalJavascriptTests\WebDriverTestBase;
abstract class TestBase extends WebDriverTestBase {
protected $defaultTheme = 'stark';
public static $modules = [
'codemirror_editor',
'codemirror_editor_test',
];
protected $activeEditor;
protected function editorSetValue($value) {
$script = 'document.querySelector("%s .CodeMirror").CodeMirror.setValue("%s");';
$script = sprintf($script, $this
->getWrapperSelector(), $value);
$this
->getSession()
->getDriver()
->executeScript($script);
}
protected function editorSetSelection($anchor, $head) {
$script = 'document.querySelector("%s .CodeMirror").CodeMirror.setSelection({line: %d, ch: %d}, {line: %d, ch: %d});';
$script = sprintf($script, $this
->getWrapperSelector(), $anchor[0], $anchor[1], $head[0], $head[1]);
$this
->getSession()
->getDriver()
->executeScript($script);
}
protected function editorClickButton($button) {
$this
->getSession()
->getPage()
->find('css', sprintf('%s [data-cme-button="' . $button . '"]', $this
->getWrapperSelector()))
->click();
}
protected function changeMode($mode) {
$this
->getSession()
->getPage()
->find('css', '.cme-mode')
->selectOption($mode);
}
protected function click($xpath) {
$this
->getSession()
->getDriver()
->click($xpath);
}
protected function assertToolbarExists() {
$this
->assertSession()
->elementExists('css', $this
->getWrapperSelector() . ' .cme-toolbar');
}
protected function assertToolbarNotExists() {
$this
->assertSession()
->elementNotExists('css', $this
->getWrapperSelector() . ' .cme-toolbar');
}
protected function assertEditorOption($option, $expected_value) {
$script = 'document.querySelector("%s .CodeMirror").CodeMirror.getOption("%s");';
$script = sprintf($script, $this
->getWrapperSelector(), $option);
$value = $this
->getSession()
->getDriver()
->evaluateScript($script);
self::assertSame($expected_value, $value);
}
protected function assertEditorHeight($expected_height) {
$script = 'document.querySelector("%s .CodeMirror").style.height;';
$script = sprintf($script, $this
->getWrapperSelector());
$height = $this
->getSession()
->getDriver()
->evaluateScript($script);
self::assertEquals($expected_height, $height);
}
protected function assertScrollerMinHeight($expected_height) {
$script = 'document.querySelector("%s .CodeMirror .CodeMirror-scroll").style.minHeight;';
$script = sprintf($script, $this
->getWrapperSelector());
$height = (int) $this
->getSession()
->getDriver()
->evaluateScript($script);
self::assertLessThan(5, abs($expected_height - $height));
}
protected function assertEditorValue($expected_value) {
$script = 'document.querySelector("%s .CodeMirror").CodeMirror.getValue();';
$script = sprintf($script, $this
->getWrapperSelector());
$value = $this
->getSession()
->getDriver()
->evaluateScript($script);
self::assertSame($expected_value, $value);
}
protected function assertElementExist($xpath) {
$this
->assertSession()
->elementExists('xpath', $xpath);
}
protected function assertElementNotExist($xpath) {
$this
->assertSession()
->elementNotExists('xpath', $xpath);
}
protected function assertVisible($xpath) {
$is_visible = $this
->getSession()
->getDriver()
->isVisible($xpath);
$this
->assertTrue($is_visible);
}
protected function assertNotVisible($xpath) {
$is_visible = $this
->getSession()
->getDriver()
->isVisible($xpath);
$this
->assertFalse($is_visible);
}
protected function getWrapperSelector() {
return '.js-form-item-editor-' . $this->activeEditor;
}
protected function scrollToBottom() {
$this
->getSession()
->getDriver()
->evaluateScript('window.scrollTo(0, document.body.scrollHeight)');
}
}