SearchContext.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. /**
  3. *
  4. * @copyright Copyright (c) 2018, Daniel Calviño Sánchez (danxuliu@gmail.com)
  5. *
  6. * @license GNU AGPL version 3 or any later version
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Affero General Public License as
  10. * published by the Free Software Foundation, either version 3 of the
  11. * License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. use Behat\Behat\Context\Context;
  23. class SearchContext implements Context, ActorAwareInterface {
  24. use ActorAware;
  25. /**
  26. * @return Locator
  27. */
  28. public static function searchBoxInput() {
  29. return Locator::forThe()->css("#header .searchbox input")->
  30. describedAs("Search box input in the header");
  31. }
  32. /**
  33. * @return Locator
  34. */
  35. public static function searchResults() {
  36. return Locator::forThe()->css("#searchresults")->
  37. describedAs("Search results");
  38. }
  39. /**
  40. * @return Locator
  41. */
  42. public static function searchResult($number) {
  43. return Locator::forThe()->xpath("//*[contains(concat(' ', normalize-space(@class), ' '), ' result ')][$number]")->
  44. descendantOf(self::searchResults())->
  45. describedAs("Search result $number");
  46. }
  47. /**
  48. * @return Locator
  49. */
  50. public static function searchResultName($number) {
  51. return Locator::forThe()->css(".name")->
  52. descendantOf(self::searchResult($number))->
  53. describedAs("Name for search result $number");
  54. }
  55. /**
  56. * @return Locator
  57. */
  58. public static function searchResultPath($number) {
  59. // Currently search results for comments misuse the ".path" class to
  60. // dim the user name, so "div.path" needs to be used to find the proper
  61. // path element.
  62. return Locator::forThe()->css("div.path")->
  63. descendantOf(self::searchResult($number))->
  64. describedAs("Path for search result $number");
  65. }
  66. /**
  67. * @return Locator
  68. */
  69. public static function searchResultLink($number) {
  70. return Locator::forThe()->css(".link")->
  71. descendantOf(self::searchResult($number))->
  72. describedAs("Link for search result $number");
  73. }
  74. /**
  75. * @When I search for :query
  76. */
  77. public function iSearchFor($query) {
  78. $this->actor->find(self::searchBoxInput(), 10)->setValue($query . "\r");
  79. }
  80. /**
  81. * @When I open the search result :number
  82. */
  83. public function iOpenTheSearchResult($number) {
  84. $this->actor->find(self::searchResultLink($number), 10)->click();
  85. }
  86. /**
  87. * @Then I see that the search result :number is :name
  88. */
  89. public function iSeeThatTheSearchResultIs($number, $name) {
  90. PHPUnit_Framework_Assert::assertEquals(
  91. $name, $this->actor->find(self::searchResultName($number), 10)->getText());
  92. }
  93. /**
  94. * @Then I see that the search result :number was found in :path
  95. */
  96. public function iSeeThatTheSearchResultWasFoundIn($number, $path) {
  97. PHPUnit_Framework_Assert::assertEquals(
  98. $path, $this->actor->find(self::searchResultPath($number), 10)->getText());
  99. }
  100. }