WebAssert.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849
  1. <?php
  2. /*
  3. * This file is part of the Mink package.
  4. * (c) Konstantin Kudryashov <ever.zet@gmail.com>
  5. *
  6. * For the full copyright and license information, please view the LICENSE
  7. * file that was distributed with this source code.
  8. */
  9. namespace Behat\Mink;
  10. use Behat\Mink\Element\Element;
  11. use Behat\Mink\Element\ElementInterface;
  12. use Behat\Mink\Element\NodeElement;
  13. use Behat\Mink\Element\TraversableElement;
  14. use Behat\Mink\Exception\ElementNotFoundException;
  15. use Behat\Mink\Exception\ExpectationException;
  16. use Behat\Mink\Exception\ResponseTextException;
  17. use Behat\Mink\Exception\ElementHtmlException;
  18. use Behat\Mink\Exception\ElementTextException;
  19. /**
  20. * Mink web assertions tool.
  21. *
  22. * @author Konstantin Kudryashov <ever.zet@gmail.com>
  23. */
  24. class WebAssert
  25. {
  26. protected $session;
  27. /**
  28. * Initializes assertion engine.
  29. *
  30. * @param Session $session
  31. */
  32. public function __construct(Session $session)
  33. {
  34. $this->session = $session;
  35. }
  36. /**
  37. * Checks that current session address is equals to provided one.
  38. *
  39. * @param string $page
  40. *
  41. * @throws ExpectationException
  42. */
  43. public function addressEquals($page)
  44. {
  45. $expected = $this->cleanUrl($page);
  46. $actual = $this->getCurrentUrlPath();
  47. $this->assert($actual === $expected, sprintf('Current page is "%s", but "%s" expected.', $actual, $expected));
  48. }
  49. /**
  50. * Checks that current session address is not equals to provided one.
  51. *
  52. * @param string $page
  53. *
  54. * @throws ExpectationException
  55. */
  56. public function addressNotEquals($page)
  57. {
  58. $expected = $this->cleanUrl($page);
  59. $actual = $this->getCurrentUrlPath();
  60. $this->assert($actual !== $expected, sprintf('Current page is "%s", but should not be.', $actual));
  61. }
  62. /**
  63. * Checks that current session address matches regex.
  64. *
  65. * @param string $regex
  66. *
  67. * @throws ExpectationException
  68. */
  69. public function addressMatches($regex)
  70. {
  71. $actual = $this->getCurrentUrlPath();
  72. $message = sprintf('Current page "%s" does not match the regex "%s".', $actual, $regex);
  73. $this->assert((bool) preg_match($regex, $actual), $message);
  74. }
  75. /**
  76. * Checks that specified cookie exists and its value equals to a given one.
  77. *
  78. * @param string $name cookie name
  79. * @param string $value cookie value
  80. *
  81. * @throws ExpectationException
  82. */
  83. public function cookieEquals($name, $value)
  84. {
  85. $this->cookieExists($name);
  86. $actualValue = $this->session->getCookie($name);
  87. $message = sprintf('Cookie "%s" value is "%s", but should be "%s".', $name, $actualValue, $value);
  88. $this->assert($actualValue == $value, $message);
  89. }
  90. /**
  91. * Checks that specified cookie exists.
  92. *
  93. * @param string $name cookie name
  94. *
  95. * @throws ExpectationException
  96. */
  97. public function cookieExists($name)
  98. {
  99. $message = sprintf('Cookie "%s" is not set, but should be.', $name);
  100. $this->assert($this->session->getCookie($name) !== null, $message);
  101. }
  102. /**
  103. * Checks that current response code equals to provided one.
  104. *
  105. * @param int $code
  106. *
  107. * @throws ExpectationException
  108. */
  109. public function statusCodeEquals($code)
  110. {
  111. $actual = $this->session->getStatusCode();
  112. $message = sprintf('Current response status code is %d, but %d expected.', $actual, $code);
  113. $this->assert(intval($code) === intval($actual), $message);
  114. }
  115. /**
  116. * Checks that current response code not equals to provided one.
  117. *
  118. * @param int $code
  119. *
  120. * @throws ExpectationException
  121. */
  122. public function statusCodeNotEquals($code)
  123. {
  124. $actual = $this->session->getStatusCode();
  125. $message = sprintf('Current response status code is %d, but should not be.', $actual);
  126. $this->assert(intval($code) !== intval($actual), $message);
  127. }
  128. /**
  129. * Checks that current response header equals value.
  130. *
  131. * @param string $name
  132. * @param string $value
  133. *
  134. * @throws ExpectationException
  135. */
  136. public function responseHeaderEquals($name, $value)
  137. {
  138. $actual = $this->session->getResponseHeader($name);
  139. $message = sprintf('Current response header "%s" is "%s", but "%s" expected.', $name, $actual, $value);
  140. $this->assert($value === $actual, $message);
  141. }
  142. /**
  143. * Checks that current response header does not equal value.
  144. *
  145. * @param string $name
  146. * @param string $value
  147. *
  148. * @throws ExpectationException
  149. */
  150. public function responseHeaderNotEquals($name, $value)
  151. {
  152. $actual = $this->session->getResponseHeader($name);
  153. $message = sprintf('Current response header "%s" is "%s", but should not be.', $name, $actual, $value);
  154. $this->assert($value !== $actual, $message);
  155. }
  156. /**
  157. * Checks that current response header contains value.
  158. *
  159. * @param string $name
  160. * @param string $value
  161. *
  162. * @throws ExpectationException
  163. */
  164. public function responseHeaderContains($name, $value)
  165. {
  166. $actual = $this->session->getResponseHeader($name);
  167. $message = sprintf('The text "%s" was not found anywhere in the "%s" response header.', $value, $name);
  168. $this->assert(false !== stripos($actual, $value), $message);
  169. }
  170. /**
  171. * Checks that current response header does not contain value.
  172. *
  173. * @param string $name
  174. * @param string $value
  175. *
  176. * @throws ExpectationException
  177. */
  178. public function responseHeaderNotContains($name, $value)
  179. {
  180. $actual = $this->session->getResponseHeader($name);
  181. $message = sprintf('The text "%s" was found in the "%s" response header, but it should not.', $value, $name);
  182. $this->assert(false === stripos($actual, $value), $message);
  183. }
  184. /**
  185. * Checks that current response header matches regex.
  186. *
  187. * @param string $name
  188. * @param string $regex
  189. *
  190. * @throws ExpectationException
  191. */
  192. public function responseHeaderMatches($name, $regex)
  193. {
  194. $actual = $this->session->getResponseHeader($name);
  195. $message = sprintf('The pattern "%s" was not found anywhere in the "%s" response header.', $regex, $name);
  196. $this->assert((bool) preg_match($regex, $actual), $message);
  197. }
  198. /**
  199. * Checks that current response header does not match regex.
  200. *
  201. * @param string $name
  202. * @param string $regex
  203. *
  204. * @throws ExpectationException
  205. */
  206. public function responseHeaderNotMatches($name, $regex)
  207. {
  208. $actual = $this->session->getResponseHeader($name);
  209. $message = sprintf(
  210. 'The pattern "%s" was found in the text of the "%s" response header, but it should not.',
  211. $regex,
  212. $name
  213. );
  214. $this->assert(!preg_match($regex, $actual), $message);
  215. }
  216. /**
  217. * Checks that current page contains text.
  218. *
  219. * @param string $text
  220. *
  221. * @throws ResponseTextException
  222. */
  223. public function pageTextContains($text)
  224. {
  225. $actual = $this->session->getPage()->getText();
  226. $actual = preg_replace('/\s+/u', ' ', $actual);
  227. $regex = '/'.preg_quote($text, '/').'/ui';
  228. $message = sprintf('The text "%s" was not found anywhere in the text of the current page.', $text);
  229. $this->assertResponseText((bool) preg_match($regex, $actual), $message);
  230. }
  231. /**
  232. * Checks that current page does not contains text.
  233. *
  234. * @param string $text
  235. *
  236. * @throws ResponseTextException
  237. */
  238. public function pageTextNotContains($text)
  239. {
  240. $actual = $this->session->getPage()->getText();
  241. $actual = preg_replace('/\s+/u', ' ', $actual);
  242. $regex = '/'.preg_quote($text, '/').'/ui';
  243. $message = sprintf('The text "%s" appears in the text of this page, but it should not.', $text);
  244. $this->assertResponseText(!preg_match($regex, $actual), $message);
  245. }
  246. /**
  247. * Checks that current page text matches regex.
  248. *
  249. * @param string $regex
  250. *
  251. * @throws ResponseTextException
  252. */
  253. public function pageTextMatches($regex)
  254. {
  255. $actual = $this->session->getPage()->getText();
  256. $message = sprintf('The pattern %s was not found anywhere in the text of the current page.', $regex);
  257. $this->assertResponseText((bool) preg_match($regex, $actual), $message);
  258. }
  259. /**
  260. * Checks that current page text does not matches regex.
  261. *
  262. * @param string $regex
  263. *
  264. * @throws ResponseTextException
  265. */
  266. public function pageTextNotMatches($regex)
  267. {
  268. $actual = $this->session->getPage()->getText();
  269. $message = sprintf('The pattern %s was found in the text of the current page, but it should not.', $regex);
  270. $this->assertResponseText(!preg_match($regex, $actual), $message);
  271. }
  272. /**
  273. * Checks that page HTML (response content) contains text.
  274. *
  275. * @param string $text
  276. *
  277. * @throws ExpectationException
  278. */
  279. public function responseContains($text)
  280. {
  281. $actual = $this->session->getPage()->getContent();
  282. $regex = '/'.preg_quote($text, '/').'/ui';
  283. $message = sprintf('The string "%s" was not found anywhere in the HTML response of the current page.', $text);
  284. $this->assert((bool) preg_match($regex, $actual), $message);
  285. }
  286. /**
  287. * Checks that page HTML (response content) does not contains text.
  288. *
  289. * @param string $text
  290. *
  291. * @throws ExpectationException
  292. */
  293. public function responseNotContains($text)
  294. {
  295. $actual = $this->session->getPage()->getContent();
  296. $regex = '/'.preg_quote($text, '/').'/ui';
  297. $message = sprintf('The string "%s" appears in the HTML response of this page, but it should not.', $text);
  298. $this->assert(!preg_match($regex, $actual), $message);
  299. }
  300. /**
  301. * Checks that page HTML (response content) matches regex.
  302. *
  303. * @param string $regex
  304. *
  305. * @throws ExpectationException
  306. */
  307. public function responseMatches($regex)
  308. {
  309. $actual = $this->session->getPage()->getContent();
  310. $message = sprintf('The pattern %s was not found anywhere in the HTML response of the page.', $regex);
  311. $this->assert((bool) preg_match($regex, $actual), $message);
  312. }
  313. /**
  314. * Checks that page HTML (response content) does not matches regex.
  315. *
  316. * @param $regex
  317. *
  318. * @throws ExpectationException
  319. */
  320. public function responseNotMatches($regex)
  321. {
  322. $actual = $this->session->getPage()->getContent();
  323. $message = sprintf('The pattern %s was found in the HTML response of the page, but it should not.', $regex);
  324. $this->assert(!preg_match($regex, $actual), $message);
  325. }
  326. /**
  327. * Checks that there is specified number of specific elements on the page.
  328. *
  329. * @param string $selectorType element selector type (css, xpath)
  330. * @param string|array $selector element selector
  331. * @param int $count expected count
  332. * @param ElementInterface $container document to check against
  333. *
  334. * @throws ExpectationException
  335. */
  336. public function elementsCount($selectorType, $selector, $count, ElementInterface $container = null)
  337. {
  338. $container = $container ?: $this->session->getPage();
  339. $nodes = $container->findAll($selectorType, $selector);
  340. $message = sprintf(
  341. '%d %s found on the page, but should be %d.',
  342. count($nodes),
  343. $this->getMatchingElementRepresentation($selectorType, $selector, count($nodes) !== 1),
  344. $count
  345. );
  346. $this->assert(intval($count) === count($nodes), $message);
  347. }
  348. /**
  349. * Checks that specific element exists on the current page.
  350. *
  351. * @param string $selectorType element selector type (css, xpath)
  352. * @param string|array $selector element selector
  353. * @param ElementInterface $container document to check against
  354. *
  355. * @return NodeElement
  356. *
  357. * @throws ElementNotFoundException
  358. */
  359. public function elementExists($selectorType, $selector, ElementInterface $container = null)
  360. {
  361. $container = $container ?: $this->session->getPage();
  362. $node = $container->find($selectorType, $selector);
  363. if (null === $node) {
  364. if (is_array($selector)) {
  365. $selector = implode(' ', $selector);
  366. }
  367. throw new ElementNotFoundException($this->session->getDriver(), 'element', $selectorType, $selector);
  368. }
  369. return $node;
  370. }
  371. /**
  372. * Checks that specific element does not exists on the current page.
  373. *
  374. * @param string $selectorType element selector type (css, xpath)
  375. * @param string|array $selector element selector
  376. * @param ElementInterface $container document to check against
  377. *
  378. * @throws ExpectationException
  379. */
  380. public function elementNotExists($selectorType, $selector, ElementInterface $container = null)
  381. {
  382. $container = $container ?: $this->session->getPage();
  383. $node = $container->find($selectorType, $selector);
  384. $message = sprintf(
  385. 'An %s appears on this page, but it should not.',
  386. $this->getMatchingElementRepresentation($selectorType, $selector)
  387. );
  388. $this->assert(null === $node, $message);
  389. }
  390. /**
  391. * Checks that specific element contains text.
  392. *
  393. * @param string $selectorType element selector type (css, xpath)
  394. * @param string|array $selector element selector
  395. * @param string $text expected text
  396. *
  397. * @throws ElementTextException
  398. */
  399. public function elementTextContains($selectorType, $selector, $text)
  400. {
  401. $element = $this->elementExists($selectorType, $selector);
  402. $actual = $element->getText();
  403. $regex = '/'.preg_quote($text, '/').'/ui';
  404. $message = sprintf(
  405. 'The text "%s" was not found in the text of the %s.',
  406. $text,
  407. $this->getMatchingElementRepresentation($selectorType, $selector)
  408. );
  409. $this->assertElementText((bool) preg_match($regex, $actual), $message, $element);
  410. }
  411. /**
  412. * Checks that specific element does not contains text.
  413. *
  414. * @param string $selectorType element selector type (css, xpath)
  415. * @param string|array $selector element selector
  416. * @param string $text expected text
  417. *
  418. * @throws ElementTextException
  419. */
  420. public function elementTextNotContains($selectorType, $selector, $text)
  421. {
  422. $element = $this->elementExists($selectorType, $selector);
  423. $actual = $element->getText();
  424. $regex = '/'.preg_quote($text, '/').'/ui';
  425. $message = sprintf(
  426. 'The text "%s" appears in the text of the %s, but it should not.',
  427. $text,
  428. $this->getMatchingElementRepresentation($selectorType, $selector)
  429. );
  430. $this->assertElementText(!preg_match($regex, $actual), $message, $element);
  431. }
  432. /**
  433. * Checks that specific element contains HTML.
  434. *
  435. * @param string $selectorType element selector type (css, xpath)
  436. * @param string|array $selector element selector
  437. * @param string $html expected text
  438. *
  439. * @throws ElementHtmlException
  440. */
  441. public function elementContains($selectorType, $selector, $html)
  442. {
  443. $element = $this->elementExists($selectorType, $selector);
  444. $actual = $element->getHtml();
  445. $regex = '/'.preg_quote($html, '/').'/ui';
  446. $message = sprintf(
  447. 'The string "%s" was not found in the HTML of the %s.',
  448. $html,
  449. $this->getMatchingElementRepresentation($selectorType, $selector)
  450. );
  451. $this->assertElement((bool) preg_match($regex, $actual), $message, $element);
  452. }
  453. /**
  454. * Checks that specific element does not contains HTML.
  455. *
  456. * @param string $selectorType element selector type (css, xpath)
  457. * @param string|array $selector element selector
  458. * @param string $html expected text
  459. *
  460. * @throws ElementHtmlException
  461. */
  462. public function elementNotContains($selectorType, $selector, $html)
  463. {
  464. $element = $this->elementExists($selectorType, $selector);
  465. $actual = $element->getHtml();
  466. $regex = '/'.preg_quote($html, '/').'/ui';
  467. $message = sprintf(
  468. 'The string "%s" appears in the HTML of the %s, but it should not.',
  469. $html,
  470. $this->getMatchingElementRepresentation($selectorType, $selector)
  471. );
  472. $this->assertElement(!preg_match($regex, $actual), $message, $element);
  473. }
  474. /**
  475. * Checks that an attribute exists in an element.
  476. *
  477. * @param string $selectorType
  478. * @param string|array $selector
  479. * @param string $attribute
  480. *
  481. * @return NodeElement
  482. *
  483. * @throws ElementHtmlException
  484. */
  485. public function elementAttributeExists($selectorType, $selector, $attribute)
  486. {
  487. $element = $this->elementExists($selectorType, $selector);
  488. $message = sprintf(
  489. 'The attribute "%s" was not found in the %s.',
  490. $attribute,
  491. $this->getMatchingElementRepresentation($selectorType, $selector)
  492. );
  493. $this->assertElement($element->hasAttribute($attribute), $message, $element);
  494. return $element;
  495. }
  496. /**
  497. * Checks that an attribute of a specific elements contains text.
  498. *
  499. * @param string $selectorType
  500. * @param string|array $selector
  501. * @param string $attribute
  502. * @param string $text
  503. *
  504. * @throws ElementHtmlException
  505. */
  506. public function elementAttributeContains($selectorType, $selector, $attribute, $text)
  507. {
  508. $element = $this->elementAttributeExists($selectorType, $selector, $attribute);
  509. $actual = $element->getAttribute($attribute);
  510. $regex = '/'.preg_quote($text, '/').'/ui';
  511. $message = sprintf(
  512. 'The text "%s" was not found in the attribute "%s" of the %s.',
  513. $text,
  514. $attribute,
  515. $this->getMatchingElementRepresentation($selectorType, $selector)
  516. );
  517. $this->assertElement((bool) preg_match($regex, $actual), $message, $element);
  518. }
  519. /**
  520. * Checks that an attribute of a specific elements does not contain text.
  521. *
  522. * @param string $selectorType
  523. * @param string|array $selector
  524. * @param string $attribute
  525. * @param string $text
  526. *
  527. * @throws ElementHtmlException
  528. */
  529. public function elementAttributeNotContains($selectorType, $selector, $attribute, $text)
  530. {
  531. $element = $this->elementAttributeExists($selectorType, $selector, $attribute);
  532. $actual = $element->getAttribute($attribute);
  533. $regex = '/'.preg_quote($text, '/').'/ui';
  534. $message = sprintf(
  535. 'The text "%s" was found in the attribute "%s" of the %s.',
  536. $text,
  537. $attribute,
  538. $this->getMatchingElementRepresentation($selectorType, $selector)
  539. );
  540. $this->assertElement(!preg_match($regex, $actual), $message, $element);
  541. }
  542. /**
  543. * Checks that specific field exists on the current page.
  544. *
  545. * @param string $field field id|name|label|value
  546. * @param TraversableElement $container document to check against
  547. *
  548. * @return NodeElement
  549. *
  550. * @throws ElementNotFoundException
  551. */
  552. public function fieldExists($field, TraversableElement $container = null)
  553. {
  554. $container = $container ?: $this->session->getPage();
  555. $node = $container->findField($field);
  556. if (null === $node) {
  557. throw new ElementNotFoundException($this->session->getDriver(), 'form field', 'id|name|label|value', $field);
  558. }
  559. return $node;
  560. }
  561. /**
  562. * Checks that specific field does not exists on the current page.
  563. *
  564. * @param string $field field id|name|label|value
  565. * @param TraversableElement $container document to check against
  566. *
  567. * @throws ExpectationException
  568. */
  569. public function fieldNotExists($field, TraversableElement $container = null)
  570. {
  571. $container = $container ?: $this->session->getPage();
  572. $node = $container->findField($field);
  573. $this->assert(null === $node, sprintf('A field "%s" appears on this page, but it should not.', $field));
  574. }
  575. /**
  576. * Checks that specific field have provided value.
  577. *
  578. * @param string $field field id|name|label|value
  579. * @param string $value field value
  580. * @param TraversableElement $container document to check against
  581. *
  582. * @throws ExpectationException
  583. */
  584. public function fieldValueEquals($field, $value, TraversableElement $container = null)
  585. {
  586. $node = $this->fieldExists($field, $container);
  587. $actual = $node->getValue();
  588. $regex = '/^'.preg_quote($value, '/').'$/ui';
  589. $message = sprintf('The field "%s" value is "%s", but "%s" expected.', $field, $actual, $value);
  590. $this->assert((bool) preg_match($regex, $actual), $message);
  591. }
  592. /**
  593. * Checks that specific field have provided value.
  594. *
  595. * @param string $field field id|name|label|value
  596. * @param string $value field value
  597. * @param TraversableElement $container document to check against
  598. *
  599. * @throws ExpectationException
  600. */
  601. public function fieldValueNotEquals($field, $value, TraversableElement $container = null)
  602. {
  603. $node = $this->fieldExists($field, $container);
  604. $actual = $node->getValue();
  605. $regex = '/^'.preg_quote($value, '/').'$/ui';
  606. $message = sprintf('The field "%s" value is "%s", but it should not be.', $field, $actual);
  607. $this->assert(!preg_match($regex, $actual), $message);
  608. }
  609. /**
  610. * Checks that specific checkbox is checked.
  611. *
  612. * @param string $field field id|name|label|value
  613. * @param TraversableElement $container document to check against
  614. *
  615. * @throws ExpectationException
  616. */
  617. public function checkboxChecked($field, TraversableElement $container = null)
  618. {
  619. $node = $this->fieldExists($field, $container);
  620. $this->assert($node->isChecked(), sprintf('Checkbox "%s" is not checked, but it should be.', $field));
  621. }
  622. /**
  623. * Checks that specific checkbox is unchecked.
  624. *
  625. * @param string $field field id|name|label|value
  626. * @param TraversableElement $container document to check against
  627. *
  628. * @throws ExpectationException
  629. */
  630. public function checkboxNotChecked($field, TraversableElement $container = null)
  631. {
  632. $node = $this->fieldExists($field, $container);
  633. $this->assert(!$node->isChecked(), sprintf('Checkbox "%s" is checked, but it should not be.', $field));
  634. }
  635. /**
  636. * Gets current url of the page.
  637. *
  638. * @return string
  639. */
  640. protected function getCurrentUrlPath()
  641. {
  642. return $this->cleanUrl($this->session->getCurrentUrl());
  643. }
  644. /**
  645. * Trims scriptname from the URL.
  646. *
  647. * @param string $url
  648. *
  649. * @return string
  650. */
  651. protected function cleanUrl($url)
  652. {
  653. $parts = parse_url($url);
  654. $fragment = empty($parts['fragment']) ? '' : '#'.$parts['fragment'];
  655. $path = empty($parts['path']) ? '/' : $parts['path'];
  656. return preg_replace('/^\/[^\.\/]+\.php\//', '/', $path).$fragment;
  657. }
  658. /**
  659. * Asserts a condition.
  660. *
  661. * @param bool $condition
  662. * @param string $message Failure message
  663. *
  664. * @throws ExpectationException when the condition is not fulfilled
  665. */
  666. private function assert($condition, $message)
  667. {
  668. if ($condition) {
  669. return;
  670. }
  671. throw new ExpectationException($message, $this->session->getDriver());
  672. }
  673. /**
  674. * Asserts a condition involving the response text.
  675. *
  676. * @param bool $condition
  677. * @param string $message Failure message
  678. *
  679. * @throws ResponseTextException when the condition is not fulfilled
  680. */
  681. private function assertResponseText($condition, $message)
  682. {
  683. if ($condition) {
  684. return;
  685. }
  686. throw new ResponseTextException($message, $this->session->getDriver());
  687. }
  688. /**
  689. * Asserts a condition on an element.
  690. *
  691. * @param bool $condition
  692. * @param string $message Failure message
  693. * @param Element $element
  694. *
  695. * @throws ElementHtmlException when the condition is not fulfilled
  696. */
  697. private function assertElement($condition, $message, Element $element)
  698. {
  699. if ($condition) {
  700. return;
  701. }
  702. throw new ElementHtmlException($message, $this->session->getDriver(), $element);
  703. }
  704. /**
  705. * Asserts a condition involving the text of an element.
  706. *
  707. * @param bool $condition
  708. * @param string $message Failure message
  709. * @param Element $element
  710. *
  711. * @throws ElementTextException when the condition is not fulfilled
  712. */
  713. private function assertElementText($condition, $message, Element $element)
  714. {
  715. if ($condition) {
  716. return;
  717. }
  718. throw new ElementTextException($message, $this->session->getDriver(), $element);
  719. }
  720. /**
  721. * @param string $selectorType
  722. * @param string|array $selector
  723. * @param bool $plural
  724. *
  725. * @return string
  726. */
  727. private function getMatchingElementRepresentation($selectorType, $selector, $plural = false)
  728. {
  729. $pluralization = $plural ? 's' : '';
  730. if (in_array($selectorType, array('named', 'named_exact', 'named_partial'))
  731. && is_array($selector) && 2 === count($selector)
  732. ) {
  733. return sprintf('%s%s matching locator "%s"', $selector[0], $pluralization, $selector[1]);
  734. }
  735. if (is_array($selector)) {
  736. $selector = implode(' ', $selector);
  737. }
  738. return sprintf('element%s matching %s "%s"', $pluralization, $selectorType, $selector);
  739. }
  740. }