PageRenderTime 51ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 1ms

/tests/codecept/src/Codeception/Util/Mink.php

http://github.com/imagecms/ImageCMS
PHP | 500 lines | 392 code | 81 blank | 27 comment | 40 complexity | c0c6272191a075582bfa295ae7cfc044 MD5 | raw file
Possible License(s): LGPL-2.1, AGPL-1.0
  1. <?php
  2. namespace Codeception\Util;
  3. use Codeception\Exception\ElementNotFound;
  4. use Symfony\Component\CssSelector\CssSelector;
  5. use Symfony\Component\CssSelector\Exception\ParseException;
  6. use Symfony\Component\CssSelector\XPathExpr;
  7. abstract class Mink extends \Codeception\Module implements RemoteInterface, WebInterface
  8. {
  9. /**
  10. * @var \Behat\Mink\Session
  11. */
  12. public $session = null;
  13. public function _initialize() {
  14. if (!$this->session) throw new \Codeception\Exception\Module(__CLASS__, "Module is not initialized. Mink session is not started in _initialize method of module.");;
  15. try {
  16. $this->session->start();
  17. $this->session->visit($this->config['url'].'/');
  18. $this->session->stop();
  19. } catch (\Exception $e) {
  20. throw new \Codeception\Exception\ModuleConfig(__CLASS__, "Provided URL can't be accessed by this driver." . $e->getMessage());
  21. }
  22. }
  23. public function _before(\Codeception\TestCase $test)
  24. {
  25. $this->session->start();
  26. }
  27. public function _after(\Codeception\TestCase $test) {
  28. $this->session->stop();
  29. }
  30. public function _getUrl()
  31. {
  32. if (!isset($this->config['url']))
  33. throw new \Codeception\Exception\ModuleConfig(__CLASS__, "Module connection failure. The URL for client can't bre retrieved");
  34. return $this->config['url'];
  35. }
  36. public function _setHeader($header, $value)
  37. {
  38. $this->session->setRequestHeader($header, $value);
  39. }
  40. public function _setCookie($cookie, $value)
  41. {
  42. $this->session->setCookie($cookie, $value);
  43. }
  44. public function _getCookie($cookie)
  45. {
  46. return $this->session->getCookie($cookie);
  47. }
  48. public function _getResponseHeader($header)
  49. {
  50. $headers = $this->session->getResponseHeaders();
  51. if (!isset($headers[$header])) return false;
  52. return $headers[$header];
  53. }
  54. public function _getResponseCode()
  55. {
  56. return $this->session->getStatusCode();
  57. }
  58. public function _sendRequest($url)
  59. {
  60. $this->session->visit($url);
  61. return $this->session->getDriver()->getContent();
  62. }
  63. /**
  64. * Opens the page.
  65. *
  66. * @param $page
  67. */
  68. public function amOnPage($page)
  69. {
  70. $this->session->visit($this->config['url'].$page);
  71. }
  72. public function amOnSubdomain($subdomain)
  73. {
  74. $url = $this->config['url'];
  75. $url = preg_replace('~(https?:\/\/)(.*\.)(.*\.)~', "$1$3", $url); // removing current subdomain
  76. $url = preg_replace('~(https?:\/\/)(.*)~', "$1$subdomain.$2", $url); // inserting new
  77. $this->_reconfigure(array('url' => $url));
  78. }
  79. public function dontSee($text, $selector = null) {
  80. $res = $this->proceedSee($text, $selector);
  81. $this->assertNot($res);
  82. }
  83. public function see($text, $selector = null) {
  84. $res = $this->proceedSee($text, $selector);
  85. $this->assert($res);
  86. }
  87. protected function proceedSee($text, $selector = null) {
  88. if ($selector) {
  89. $nodes = null;
  90. if (Locator::isCSS($selector)) {
  91. $nodes = $this->session->getPage()->findAll('css', $selector);
  92. }
  93. if (!$nodes and Locator::isXPath($selector)) {
  94. $nodes = $this->session->getPage()->findAll('xpath', $selector);
  95. }
  96. if (empty($nodes)) throw new ElementNotFound($selector, 'CSS or XPath');
  97. $values = '';
  98. foreach ($nodes as $node) {
  99. $values .= '<!-- Merged Output -->'.$node->getText();
  100. }
  101. return array('pageContains', $this->escape($text), $values, "'$selector' selector.");
  102. }
  103. $response = $this->session->getPage()->getText();
  104. $output = Framework::formatResponse($response);
  105. return array('pageContains', $this->escape($text), $response, "'$text' in ".$output.'.');
  106. }
  107. public function seeLink($text, $url = null)
  108. {
  109. $text = $this->escape($text);
  110. $nodes = $this->session->getPage()->findAll(
  111. 'named', array(
  112. 'link', $this->session->getSelectorsHandler()->xpathLiteral($text)
  113. )
  114. );
  115. if (!$url) {
  116. return \PHPUnit_Framework_Assert::assertNotEmpty($nodes);
  117. }
  118. foreach ($nodes as $node) {
  119. if (false !== strpos($node->getAttribute('href'), $url)) {
  120. return \PHPUnit_Framework_Assert::assertContains(
  121. $text, $node->getHtml(), "with url '{$url}'"
  122. );
  123. }
  124. }
  125. return \PHPUnit_Framework_Assert::fail("with url '{$url}'");
  126. }
  127. public function dontSeeLink($text, $url = null)
  128. {
  129. if (!$url) {
  130. return $this->dontSee($text, 'a');
  131. }
  132. $text = $this->escape($text);
  133. $nodes = $this->session->getPage()->findAll(
  134. 'named', array(
  135. 'link', $this->session->getSelectorsHandler()->xpathLiteral($text)
  136. )
  137. );
  138. foreach ($nodes as $node) {
  139. if (false !== strpos($node->getAttribute('href'), $url)) {
  140. return \PHPUnit_Framework_Assert::fail("with url '$url'");
  141. }
  142. }
  143. return \PHPUnit_Framework_Assert::assertTrue(true, "with url '$url'");
  144. }
  145. public function click($link, $context = null) {
  146. $url = $this->session->getCurrentUrl();
  147. $el = $this->findClickable($link, $context);
  148. $el->click();
  149. if ($this->session->getCurrentUrl() != $url) {
  150. $this->debug('moved to page '. $this->session->getCurrentUrl());
  151. }
  152. }
  153. public function seeElement($selector)
  154. {
  155. $el = $this->findEl($selector);
  156. $this->assertNotEmpty($el);
  157. }
  158. public function dontSeeElement($selector)
  159. {
  160. $el = array();
  161. try{
  162. $el = $this->findEl($selector);
  163. } catch (\PHPUnit_Framework_AssertionFailedError $e) {
  164. }
  165. $this->assertEmpty($el);
  166. }
  167. /**
  168. * @param $selector
  169. * @return \Behat\Mink\Element\NodeElement|null
  170. * @throws \Codeception\Exception\ElementNotFound
  171. */
  172. protected function findEl($selector)
  173. {
  174. $page = $this->session->getPage();
  175. $el = null;
  176. if (Locator::isCSS($selector)) {
  177. $el = $page->find('css', $selector);
  178. }
  179. if (!$el and Locator::isXPath($selector)) {
  180. $el = @$page->find('xpath',$selector);
  181. }
  182. if (!$el) throw new ElementNotFound($selector, 'CSS or XPath');
  183. return $el;
  184. }
  185. protected function findLinkByContent($link)
  186. {
  187. $literal = $this->session->getSelectorsHandler()->xpathLiteral($link);
  188. return $this->session->getPage()->find('xpath','.//a[normalize-space(.)=normalize-space('.$literal.')]');
  189. }
  190. protected function findClickable($link, $context = null)
  191. {
  192. $page = $context
  193. ? $this->findEl($context)
  194. : $this->session->getPage();
  195. if (!$page) {
  196. $this->fail("Context element $context not found");
  197. }
  198. $el = $this->findLinkByContent($link);
  199. if (!$el) $el = $page->findLink($link);
  200. if (!$el) $el = $page->findButton($link);
  201. if (!$el) $el = $this->findEl($link);
  202. return $el;
  203. }
  204. /**
  205. * Reloads current page
  206. */
  207. public function reloadPage() {
  208. $this->session->reload();
  209. }
  210. /**
  211. * Moves back in history
  212. */
  213. public function moveBack() {
  214. $this->session->back();
  215. $this->debug($this->session->getCurrentUrl());
  216. }
  217. /**
  218. * Moves forward in history
  219. */
  220. public function moveForward() {
  221. $this->session->forward();
  222. $this->debug($this->session->getCurrentUrl());
  223. }
  224. public function fillField($field, $value)
  225. {
  226. $field = $this->findField($field);
  227. $field->setValue($value);
  228. }
  229. public function selectOption($select, $option)
  230. {
  231. $field = $this->findField($select);
  232. if (is_array($option)) {
  233. foreach ($option as $opt) {
  234. $field->selectOption($opt, true);
  235. }
  236. return;
  237. }
  238. $field->selectOption($option);
  239. }
  240. public function checkOption($option)
  241. {
  242. $field = $this->findField($option);
  243. $field->check();
  244. }
  245. public function uncheckOption($option)
  246. {
  247. $field = $this->findField($option);
  248. $field->uncheck();
  249. }
  250. /**
  251. * @param $selector
  252. * @return \Behat\Mink\Element\NodeElement|null
  253. * @throws \Codeception\Exception\ElementNotFound
  254. */
  255. protected function findField($selector)
  256. {
  257. $page = $this->session->getPage();
  258. $field = $page->find('named', array(
  259. 'field', $this->session->getSelectorsHandler()->xpathLiteral($selector)
  260. ));
  261. if (!$field and Locator::isCSS($selector)) $field = $page->find('css', $selector);
  262. if (!$field and Locator::isXPath($selector)) $field = @$page->find('xpath', $selector);
  263. if (!$field) throw new ElementNotFound($selector, "Field by name, label, CSS or XPath");
  264. return $field;
  265. }
  266. public function _getCurrentUri()
  267. {
  268. $url = $this->session->getCurrentUrl();
  269. $parts = parse_url($url);
  270. if (!$parts) $this->fail("URL couldn't be parsed");
  271. $uri = "";
  272. if (isset($parts['path'])) $uri .= $parts['path'];
  273. if (isset($parts['query'])) $uri .= "?".$parts['query'];
  274. if (isset($parts['fragment'])) $uri .= "#".$parts['fragment'];
  275. return $uri;
  276. }
  277. public function seeInCurrentUrl($uri) {
  278. \PHPUnit_Framework_Assert::assertContains($uri, $this->_getCurrentUri());
  279. }
  280. public function dontSeeInCurrentUrl($uri)
  281. {
  282. \PHPUnit_Framework_Assert::assertNotContains($uri, $this->_getCurrentUri());
  283. }
  284. public function seeCurrentUrlEquals($uri)
  285. {
  286. \PHPUnit_Framework_Assert::assertEquals($uri, $this->_getCurrentUri());
  287. }
  288. public function dontSeeCurrentUrlEquals($uri)
  289. {
  290. \PHPUnit_Framework_Assert::assertNotEquals($uri, $this->_getCurrentUri());
  291. }
  292. public function seeCurrentUrlMatches($uri)
  293. {
  294. \PHPUnit_Framework_Assert::assertRegExp($uri, $this->_getCurrentUri());
  295. }
  296. public function dontSeeCurrentUrlMatches($uri)
  297. {
  298. \PHPUnit_Framework_Assert::assertNotRegExp($uri, $this->_getCurrentUri());
  299. }
  300. public function seeCookie($cookie)
  301. {
  302. \PHPUnit_Framework_Assert::assertNotNull($this->_getCookie($cookie));
  303. }
  304. public function dontSeeCookie($cookie)
  305. {
  306. \PHPUnit_Framework_Assert::assertNull($this->_getCookie($cookie));
  307. }
  308. public function setCookie($cookie, $value)
  309. {
  310. $this->_setCookie($cookie, $value);
  311. }
  312. public function resetCookie($cookie)
  313. {
  314. $this->_setCookie($cookie, null);
  315. }
  316. public function grabCookie($cookie)
  317. {
  318. return $this->_getCookie($cookie);
  319. }
  320. public function grabFromCurrentUrl($uri = null)
  321. {
  322. if (!$uri) return $this->session->getCurrentUrl();
  323. $matches = array();
  324. $res = preg_match($uri, $this->session->getCurrentUrl(), $matches);
  325. if (!$res) $this->fail("Couldn't match $uri in ".$this->session->getCurrentUrl());
  326. if (!isset($matches[1])) $this->fail("Nothing to grab. A regex parameter required. Ex: '/user/(\\d+)'");
  327. return $matches[1];
  328. }
  329. public function attachFile($field, $filename) {
  330. $field = $this->findField($field);
  331. $path = \Codeception\Configuration::dataDir().$filename;
  332. if (!file_exists($path)) \PHPUnit_Framework_Assert::fail("file $filename not found in Codeception data path. Only files stored in data path accepted");
  333. $field->attachFile($path);
  334. }
  335. public function seeOptionIsSelected($select, $text)
  336. {
  337. $option = $this->findSelectedOption($select);
  338. if (!$option) $this->fail("No option is selected in $select");
  339. $this->assertEquals($text, $option->getText());
  340. }
  341. public function dontSeeOptionIsSelected($select, $text)
  342. {
  343. $option = $this->findSelectedOption($select);
  344. if (!$option) {
  345. \PHPUnit_Framework_Assert::assertNull($option);
  346. return;
  347. }
  348. $this->assertNotEquals($text, $option->getText());
  349. }
  350. protected function findSelectedOption($select)
  351. {
  352. $selectbox = $this->findEl($select);
  353. $option = $selectbox->find('css','option[selected]');
  354. return $option;
  355. }
  356. public function seeCheckboxIsChecked($checkbox) {
  357. $node = $this->findField($checkbox);
  358. if (!$node) return \PHPUnit_Framework_Assert::fail(", checkbox not found");
  359. \PHPUnit_Framework_Assert::assertTrue($node->isChecked());
  360. }
  361. public function dontSeeCheckboxIsChecked($checkbox) {
  362. $node = $this->findField($checkbox);
  363. if (!$node) return \PHPUnit_Framework_Assert::fail(", checkbox not found");
  364. \PHPUnit_Framework_Assert::assertFalse($node->isChecked());
  365. }
  366. public function seeInField($field, $value) {
  367. $node = $this->findField($field);
  368. if (!$node) return \PHPUnit_Framework_Assert::fail(", field not found");
  369. $this->assertEquals($value, $node->getTagName() == 'textarea' ? $node->getText() : $node->getAttribute('value'));
  370. }
  371. public function dontSeeInField($field, $value) {
  372. $node = $this->findField($field);
  373. if (!$node) return \PHPUnit_Framework_Assert::fail(", field not found");
  374. $this->assertNotEquals($value, $node->getTagName() == 'textarea' ? $node->getText() : $node->getAttribute('value'));
  375. }
  376. public function grabTextFrom($cssOrXPathOrRegex) {
  377. $el = null;
  378. if (Locator::isCSS($cssOrXPathOrRegex)) {
  379. $el = $this->session->getPage()->find('css', $cssOrXPathOrRegex);
  380. if ($el) return $el->getText();
  381. }
  382. if (!$el and Locator::isXPath($cssOrXPathOrRegex)) {
  383. $el = @$this->session->getPage()->find('xpath', $cssOrXPathOrRegex);
  384. if ($el) return $el->getText();
  385. }
  386. if (@preg_match($cssOrXPathOrRegex, $this->session->getPage()->getContent(), $matches)) {
  387. return $matches[1];
  388. }
  389. throw new ElementNotFound($cssOrXPathOrRegex, 'CSS or XPath or Regex');
  390. }
  391. public function grabValueFrom($field) {
  392. $el = $this->findField($field);
  393. if ($el) {
  394. return $el->getValue();
  395. }
  396. $this->fail("Element '$field' not found");
  397. }
  398. public function grabAttribute() {
  399. }
  400. protected function escape($string)
  401. {
  402. return (string)$string;
  403. }
  404. }