KindEditorDriver.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. require_once dirname(__FILE__) . '/php-webdriver/__init__.php';
  3. class KindEditorDriver {
  4. public $webDriver;
  5. public $session;
  6. public $element;
  7. public $timeout = 30;
  8. public $baseUrl = 'http://localhost/github-kindsoft/kindeditor/';
  9. public function __construct($url = '', $browser = 'internet explorer', $serverUrl = 'http://localhost:4444/wd/hub') {
  10. $options = getopt('', array(
  11. "browser:",
  12. ));
  13. $browser = isset($options['browser']) ? $options['browser'] : $browser;
  14. $this->webDriver = new WebDriver($serverUrl);
  15. $this->session = $this->webDriver->session($browser);
  16. if ($url !== '') {
  17. $this->open($url);
  18. }
  19. }
  20. public function open($url) {
  21. if (strpos($url, 'http://') !== 0) {
  22. $url = $this->baseUrl . $url;
  23. }
  24. $this->session->open($url);
  25. return $this;
  26. }
  27. public function selector($selector, $index = 0) {
  28. $endTime = time() + $this->timeout;
  29. while (true) {
  30. try {
  31. if ($index > 0) {
  32. $elements = $this->session->elements('css selector', $selector);
  33. $this->element = $elements[$index];
  34. } else {
  35. $this->element = $this->session->element('css selector', $selector);
  36. }
  37. return $this;
  38. } catch (NoSuchElementWebDriverError $e) {
  39. }
  40. sleep(1);
  41. if (time() > $endTime) {
  42. break;
  43. }
  44. }
  45. throw new TimeOutWebDriverError('The element could not be found', '');
  46. }
  47. public function value($val) {
  48. $this->element->value(array('value' => strSplitUnicode($val)));
  49. return $this;
  50. }
  51. public function keys($val) {
  52. $this->session->keys(array('value' => strSplitUnicode($val)));
  53. return $this;
  54. }
  55. public function click() {
  56. $this->element->click('');
  57. return $this;
  58. }
  59. public function mouseover() {
  60. $this->session->moveto(array('element' => $this->element->getID()));
  61. return $this;
  62. }
  63. public function script($script) {
  64. return $this->session->execute(array(
  65. 'script' => $script,
  66. 'args' => array(),
  67. ));
  68. }
  69. public function clickToolbar($name) {
  70. $this->session->frame(array('id' => null));
  71. return $this->selector('.ke-icon-' . $name)->click();
  72. }
  73. // get or set editor content
  74. public function html($val = null) {
  75. $this->session->frame(array('id' => null));
  76. if ($val === null) {
  77. return preg_replace('/[\r\n\t]/', '', $this->script("return editor.html();"));
  78. }
  79. $this->script("editor.html('$val');");
  80. return $this;
  81. }
  82. // input editor content
  83. public function input($val) {
  84. $id = 'ke-edit-iframe';
  85. $this->script("KindEditor('.ke-edit-iframe').eq(0).attr('id', '$id');");
  86. $this->selector("#$id");
  87. $this->session->frame(array('id' => $id));
  88. $this->keys($val);
  89. return $this;
  90. }
  91. // drag element
  92. public function drag($x, $y) {
  93. //$id = 'document-body';
  94. //$this->script("KindEditor('body').attr('id', '$id');");
  95. $this->mouseover();
  96. $this->session->buttondown("");
  97. $this->session->moveto(array(
  98. //'element' => $id,
  99. 'xoffset' => $x,
  100. 'yoffset' => $y,
  101. ));
  102. $this->session->buttonup("");
  103. return $this;
  104. }
  105. public function close() {
  106. $this->session->close();
  107. return $this;
  108. }
  109. }
  110. function strSplitUnicode($str, $l = 1) {
  111. if ($l > 0) {
  112. $ret = array();
  113. $len = mb_strlen($str, "UTF-8");
  114. for ($i = 0; $i < $len; $i += $l) {
  115. $ret[] = mb_substr($str, $i, $l, "UTF-8");
  116. }
  117. return $ret;
  118. }
  119. return preg_split("//u", $str, -1, PREG_SPLIT_NO_EMPTY);
  120. }
  121. function equals($a, $b) {
  122. if ($a === $b) {
  123. echo "[OK] \"$a\"\n";
  124. } else {
  125. echo "[FAILED]\n";
  126. echo "Expected: \"$b\"\n";
  127. echo "Result: \"$a\"\n";
  128. }
  129. }