SearchContext.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. use PHPUnit\Framework\Assert;
  24. class SearchContext implements Context, ActorAwareInterface {
  25. use ActorAware;
  26. /**
  27. * @return Locator
  28. */
  29. public static function searchBoxInput() {
  30. return Locator::forThe()->css("#header .searchbox input")->
  31. describedAs("Search box input in the header");
  32. }
  33. /**
  34. * @return Locator
  35. */
  36. public static function searchResults() {
  37. return Locator::forThe()->css("#searchresults")->
  38. describedAs("Search results");
  39. }
  40. /**
  41. * @return Locator
  42. */
  43. public static function searchResult($number) {
  44. return Locator::forThe()->xpath("//*[contains(concat(' ', normalize-space(@class), ' '), ' result ')][$number]")->
  45. descendantOf(self::searchResults())->
  46. describedAs("Search result $number");
  47. }
  48. /**
  49. * @return Locator
  50. */
  51. public static function searchResultName($number) {
  52. return Locator::forThe()->css(".name")->
  53. descendantOf(self::searchResult($number))->
  54. describedAs("Name for search result $number");
  55. }
  56. /**
  57. * @return Locator
  58. */
  59. public static function searchResultPath($number) {
  60. // Currently search results for comments misuse the ".path" class to
  61. // dim the user name, so "div.path" needs to be used to find the proper
  62. // path element.
  63. return Locator::forThe()->css("div.path")->
  64. descendantOf(self::searchResult($number))->
  65. describedAs("Path for search result $number");
  66. }
  67. /**
  68. * @return Locator
  69. */
  70. public static function searchResultLink($number) {
  71. return Locator::forThe()->css(".link")->
  72. descendantOf(self::searchResult($number))->
  73. describedAs("Link for search result $number");
  74. }
  75. /**
  76. * @When I search for :query
  77. */
  78. public function iSearchFor($query) {
  79. $this->actor->find(self::searchBoxInput(), 10)->setValue($query);
  80. }
  81. /**
  82. * @When I open the search result :number
  83. */
  84. public function iOpenTheSearchResult($number) {
  85. $this->actor->find(self::searchResultLink($number), 10)->click();
  86. }
  87. /**
  88. * @Then I see that the search result :number is :name
  89. */
  90. public function iSeeThatTheSearchResultIs($number, $name) {
  91. Assert::assertEquals(
  92. $name, $this->actor->find(self::searchResultName($number), 10)->getText());
  93. }
  94. /**
  95. * @Then I see that the search result :number was found in :path
  96. */
  97. public function iSeeThatTheSearchResultWasFoundIn($number, $path) {
  98. Assert::assertEquals(
  99. $path, $this->actor->find(self::searchResultPath($number), 10)->getText());
  100. }
  101. }