selenese_tester.php 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. <?php
  2. /**
  3. * @package SimpleTest
  4. * @subpackage Extensions
  5. * @version $Id: selenese_tester.php 1802 2008-09-08 10:43:58Z maetl_ $
  6. */
  7. /**#@+
  8. * include SimpleTest files
  9. */
  10. require_once dirname(__FILE__).'/../unit_tester.php';
  11. require_once dirname(__FILE__).'/selenium/remote-control.php';
  12. /**#@-*/
  13. /**
  14. * SeleneseTestCase
  15. *
  16. * Integrate selenese html test suite support (can be generated by selenium-IDE)
  17. *
  18. * @category Testing
  19. * @package SimpleTest
  20. * @subpackage Extensions
  21. * @author Guidet Alexandre <alwex@free.fr>
  22. * @param simpleSelenium $selenium
  23. * @param string $html
  24. * @param string $testFile
  25. * @param array $parsed_table
  26. * @param string $logMessages
  27. * @param array $_commandMap
  28. *
  29. */
  30. class SeleneseTestCase extends UnitTestCase {
  31. var $selenium;
  32. var $html;
  33. var $testFile;
  34. var $parsed_table;
  35. var $logMessages;
  36. var $_commandMap = array("verify",
  37. "verifyErrorOnNext",
  38. "verifyNotErrorOnNext",
  39. "verifyFailureOnNext",
  40. "verifyNotFailureOnNext",
  41. "verifySelected",
  42. "verifyNotSelected",
  43. "verifyAlert",
  44. "verifyNotAlert",
  45. "verifyAllButtons",
  46. "verifyNotAllButtons",
  47. "verifyAllFields",
  48. "verifyNotAllFields",
  49. "verifyAllLinks",
  50. "verifyNotAllLinks",
  51. "verifyAllWindowIds",
  52. "verifyNotAllWindowIds",
  53. "verifyAllWindowNames",
  54. "verifyNotAllWindowNames",
  55. "verifyAllWindowTitles",
  56. "verifyNotAllWindowTitles",
  57. "verifyAttribute",
  58. "verifyNotAttribute",
  59. "verifyAttributeFromAllWindows",
  60. "verifyNotAttributeFromAllWindows",
  61. "verifyBodyText",
  62. "verifyNotBodyText",
  63. "verifyConfirmation",
  64. "verifyNotConfirmation",
  65. "verifyCookie",
  66. "verifyNotCookie",
  67. "verifyCursorPosition",
  68. "verifyNotCursorPosition",
  69. "verifyElementHeight",
  70. "verifyNotElementHeight",
  71. "verifyElementIndex",
  72. "verifyNotElementIndex",
  73. "verifyElementPositionLeft",
  74. "verifyNotElementPositionLeft",
  75. "verifyElementPositionTop",
  76. "verifyNotElementPositionTop",
  77. "verifyElementWidth",
  78. "verifyNotElementWidth",
  79. "verifyEval",
  80. "verifyNotEval",
  81. "verifyExpression",
  82. "verifyNotExpression",
  83. "verifyHtmlSource",
  84. "verifyNotHtmlSource",
  85. "verifyLocation",
  86. "verifyNotLocation",
  87. "verifyLogMessages",
  88. "verifyNotLogMessages",
  89. "verifyMouseSpeed",
  90. "verifyNotMouseSpeed",
  91. "verifyPrompt",
  92. "verifyNotPrompt",
  93. "verifySelectedId",
  94. "verifyNotSelectedId",
  95. "verifySelectedIds",
  96. "verifyNotSelectedIds",
  97. "verifySelectedIndex",
  98. "verifyNotSelectedIndex",
  99. "verifySelectedIndexes",
  100. "verifyNotSelectedIndexes",
  101. "verifySelectedLabel",
  102. "verifyNotSelectedLabel",
  103. "verifySelectedLabels",
  104. "verifyNotSelectedLabels",
  105. "verifySelectedValue",
  106. "verifyNotSelectedValue",
  107. "verifySelectedValues",
  108. "verifyNotSelectedValues",
  109. "verifySelectOptions",
  110. "verifyNotSelectOptions",
  111. "verifyTable",
  112. "verifyNotTable",
  113. "verifyText",
  114. "verifyNotText",
  115. "verifyTitle",
  116. "verifyNotTitle",
  117. "verifyValue",
  118. "verifyNotValue",
  119. "verifyWhetherThisFrameMatchFrameExpression",
  120. "verifyNotWhetherThisFrameMatchFrameExpression",
  121. "verifyWhetherThisWindowMatchWindowExpression",
  122. "verifyNotWhetherThisWindowMatchWindowExpression",
  123. "verifyAlertPresent",
  124. "verifyAlertNotPresent",
  125. "verifyChecked",
  126. "verifyNotChecked",
  127. "verifyConfirmationPresent",
  128. "verifyConfirmationNotPresent",
  129. "verifyEditable",
  130. "verifyNotEditable",
  131. "verifyElementPresent",
  132. "verifyElementNotPresent",
  133. "verifyOrdered",
  134. "verifyNotOrdered",
  135. "verifyPromptPresent",
  136. "verifyPromptNotPresent",
  137. "verifySomethingSelected",
  138. "verifyNotSomethingSelected",
  139. "verifyTextPresent",
  140. "verifyTextNotPresent",
  141. "verifyVisible",
  142. "verifyNotVisible",
  143. "assert",
  144. "assertErrorOnNext",
  145. "assertNotErrorOnNext",
  146. "assertFailureOnNext",
  147. "assertNotFailureOnNext",
  148. "assertSelected",
  149. "assertNotSelected",
  150. "assertAlert",
  151. "assertNotAlert",
  152. "assertAllButtons",
  153. "assertNotAllButtons",
  154. "assertAllFields",
  155. "assertNotAllFields",
  156. "assertAllLinks",
  157. "assertNotAllLinks",
  158. "assertAllWindowIds",
  159. "assertNotAllWindowIds",
  160. "assertAllWindowNames",
  161. "assertNotAllWindowNames",
  162. "assertAllWindowTitles",
  163. "assertNotAllWindowTitles",
  164. "assertAttribute",
  165. "assertNotAttribute",
  166. "assertAttributeFromAllWindows",
  167. "assertNotAttributeFromAllWindows",
  168. "assertBodyText",
  169. "assertNotBodyText",
  170. "assertConfirmation",
  171. "assertNotConfirmation",
  172. "assertCookie",
  173. "assertNotCookie",
  174. "assertCursorPosition",
  175. "assertNotCursorPosition",
  176. "assertElementHeight",
  177. "assertNotElementHeight",
  178. "assertElementIndex",
  179. "assertNotElementIndex",
  180. "assertElementPositionLeft",
  181. "assertNotElementPositionLeft",
  182. "assertElementPositionTop",
  183. "assertNotElementPositionTop",
  184. "assertElementWidth",
  185. "assertNotElementWidth",
  186. "assertEval",
  187. "assertNotEval",
  188. "assertExpression",
  189. "assertNotExpression",
  190. "assertHtmlSource",
  191. "assertNotHtmlSource",
  192. "assertLocation",
  193. "assertNotLocation",
  194. "assertLogMessages",
  195. "assertNotLogMessages",
  196. "assertMouseSpeed",
  197. "assertNotMouseSpeed",
  198. "assertPrompt",
  199. "assertNotPrompt",
  200. "assertSelectedId",
  201. "assertNotSelectedId",
  202. "assertSelectedIds",
  203. "assertNotSelectedIds",
  204. "assertSelectedIndex",
  205. "assertNotSelectedIndex",
  206. "assertSelectedIndexes",
  207. "assertNotSelectedIndexes",
  208. "assertSelectedLabel",
  209. "assertNotSelectedLabel",
  210. "assertSelectedLabels",
  211. "assertNotSelectedLabels",
  212. "assertSelectedValue",
  213. "assertNotSelectedValue",
  214. "assertSelectedValues",
  215. "assertNotSelectedValues",
  216. "assertSelectOptions",
  217. "assertNotSelectOptions",
  218. "assertTable",
  219. "assertNotTable",
  220. "assertText",
  221. "assertNotText",
  222. "assertTitle",
  223. "assertNotTitle",
  224. "assertValue",
  225. "assertNotValue",
  226. "assertWhetherThisFrameMatchFrameExpression",
  227. "assertNotWhetherThisFrameMatchFrameExpression",
  228. "assertWhetherThisWindowMatchWindowExpression",
  229. "assertNotWhetherThisWindowMatchWindowExpression",
  230. "assertAlertPresent",
  231. "assertAlertNotPresent",
  232. "assertChecked",
  233. "assertNotChecked",
  234. "assertConfirmationPresent",
  235. "assertConfirmationNotPresent",
  236. "assertEditable",
  237. "assertNotEditable",
  238. "assertElementPresent",
  239. "assertElementNotPresent",
  240. "assertOrdered",
  241. "assertNotOrdered",
  242. "assertPromptPresent",
  243. "assertPromptNotPresent",
  244. "assertSomethingSelected",
  245. "assertNotSomethingSelected",
  246. "assertTextPresent",
  247. "assertTextNotPresent",
  248. "assertVisible",
  249. "assertNotVisible");
  250. /**
  251. * constructor
  252. *
  253. * Construct the object with the specified browser and url
  254. *
  255. * @param string $browser
  256. * @param string $url
  257. */
  258. function __construct($browser, $url) {
  259. $this->selenium = new SimpleSeleniumRemoteControl($browser, $url);
  260. $this->parsed_table = array();
  261. }
  262. /**
  263. * tidy
  264. *
  265. * Reformat the selenium-IDE html test suites
  266. */
  267. function tidy() {
  268. $tmp = $this->html;
  269. preg_match('/<meta.*>/', $tmp, $matche);
  270. $matche[0] = str_replace("/>", ">", $matche[0]);
  271. $matche[0] = str_replace(">", "/>", $matche[0]);
  272. $tmp = preg_replace('/<meta.*>/', $matche[0], $tmp);
  273. $this->html = $tmp;
  274. }
  275. /**
  276. * parse
  277. *
  278. * Extract the called selenium fonction from the html suite
  279. */
  280. function parse() {
  281. $parsedTab = array();
  282. $key1 = 0;
  283. $contenthtml = new DOMDocument;
  284. @$contenthtml->loadHtml($this->html);
  285. $content = simplexml_import_dom($contenthtml);
  286. foreach ($content->body->table->tbody->tr as $tr){
  287. $key2 = 0;
  288. foreach ($tr->td as $td){
  289. $parsedTab[$key1][$key2] = $td;
  290. $key2++;
  291. }
  292. $key1++;
  293. }
  294. $this->parsed_table = $parsedTab;
  295. }
  296. /**
  297. * assertFunction
  298. *
  299. * Integrate selenium fonctions in simpletest
  300. *
  301. * @param string $function
  302. * @param string $param1
  303. * @param string $param2
  304. *
  305. */
  306. function assertFunction($function, $param1, $param2) {
  307. $_verifyMap = array('verify', 'verifyTextPresent', 'verifyTextNotPresent', 'verifyValue');
  308. $reponse = $this->selenium->__call($function, array($param1, $param2));
  309. $message = $reponse;
  310. $message .= " using command '".$function ."' with target '".$param1."'";
  311. if (!empty($param2)) {
  312. $message .= " and value '".$param2."'";
  313. }
  314. $message .= " in file '".$this->testFile."'";
  315. if (!in_array($function, $_verifyMap)) {
  316. $reponse = substr($reponse, 0, 2) == 'OK' ? true : false;
  317. }
  318. $this->assertTrue($reponse, $message);
  319. }
  320. /**
  321. * launch
  322. *
  323. * Launch the html test suite from a PHP variable on the url declared wihle
  324. * constructing the object. The filename is used to localize the error.
  325. *
  326. * @param string $testFile
  327. * @param string $filename
  328. *
  329. */
  330. function launch($html="") {
  331. $this->html = $html;
  332. $this->tidy();
  333. $this->parse();
  334. $this->selenium->start();
  335. foreach ($this->parsed_table as $test) {
  336. if (in_array($test[0], $this->_commandMap)) {
  337. $this->assertFunction($test[0], $test[1], $test[2]);
  338. } else {
  339. $this->selenium->__call($test[0], array($test[1], $test[2]));
  340. }
  341. }
  342. $this->selenium->stop();
  343. }
  344. /**
  345. * launchPhpFile
  346. *
  347. * Parse the PHP file then launch the computed test suite
  348. *
  349. * @param string $file
  350. *
  351. */
  352. function launchPhpFile($file) {
  353. ob_start();
  354. require($file);
  355. $data = ob_get_contents();
  356. ob_end_clean();
  357. $this->testFile = $file;
  358. $this->html = $data;
  359. $this->launch($this->html);
  360. }
  361. /**
  362. * launchFile
  363. *
  364. * Launch the html test suite file on the url declared wihle constructing the object
  365. *
  366. * @param string $testFile
  367. *
  368. */
  369. function launchFile($testFile) {
  370. $this->testFile = $testFile;
  371. $this->html = file_get_contents($testFile);
  372. $this->launch($this->html);
  373. }
  374. }
  375. ?>