AppNavigationContext.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. /**
  3. *
  4. * @copyright Copyright (c) 2017, Daniel Calviño Sánchez (danxuliu@gmail.com)
  5. * @copyright Copyright (c) 2018, John Molakvoæ (skjnldsv) <skjnldsv@protonmail.com>
  6. *
  7. * @license GNU AGPL version 3 or any later version
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as
  11. * published by the Free Software Foundation, either version 3 of the
  12. * License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. */
  23. use Behat\Behat\Context\Context;
  24. use PHPUnit\Framework\Assert;
  25. class AppNavigationContext implements Context, ActorAwareInterface {
  26. use ActorAware;
  27. /**
  28. * @return Locator
  29. */
  30. public static function appNavigation() {
  31. return Locator::forThe()->xpath("//*[@id=\"app-navigation\" or contains(@class, 'app-navigation')]")->
  32. describedAs("App navigation");
  33. }
  34. /**
  35. * @return Locator
  36. */
  37. public static function appNavigationSectionItemFor($sectionText) {
  38. return Locator::forThe()->xpath("//li/*[contains(normalize-space(), '$sectionText')]/..")->
  39. descendantOf(self::appNavigation())->
  40. describedAs($sectionText . " section item in App Navigation");
  41. }
  42. /**
  43. * @return Locator
  44. */
  45. public static function appNavigationSectionItemInFor($caption, $sectionText) {
  46. return Locator::forThe()->xpath("//li[normalize-space() = '$caption']/following-sibling::li/a[normalize-space() = '$sectionText']/..")->
  47. descendantOf(self::appNavigation())->
  48. describedAs($sectionText . " section item of the $caption group in App Navigation");
  49. }
  50. /**
  51. * @return Locator
  52. */
  53. public static function appNavigationCurrentSectionItem() {
  54. return Locator::forThe()->css(".active")->
  55. descendantOf(self::appNavigation())->
  56. describedAs("Current section item in App Navigation");
  57. }
  58. /**
  59. * @return Locator
  60. */
  61. public static function buttonForTheSection($class, $section) {
  62. return Locator::forThe()->css("." . $class)->
  63. descendantOf(self::appNavigationSectionItemFor($section))->
  64. describedAs("The $class button on the $section section in App Navigation");
  65. }
  66. /**
  67. * @return Locator
  68. */
  69. public static function counterForTheSection($section) {
  70. return Locator::forThe()->css(".app-navigation-entry-utils-counter")->
  71. descendantOf(self::appNavigationSectionItemFor($section))->
  72. describedAs("The counter for the $section section in App Navigation");
  73. }
  74. /**
  75. * @Given I open the :section section
  76. */
  77. public function iOpenTheSection($section) {
  78. $this->actor->find(self::appNavigationSectionItemFor($section), 10)->click();
  79. }
  80. /**
  81. * @Given I open the :section section of the :caption group
  82. */
  83. public function iOpenTheSectionOf($caption, $section) {
  84. $this->actor->find(self::appNavigationSectionItemInFor($caption, $section), 10)->click();
  85. }
  86. /**
  87. * @Given I click the :class button on the :section section
  88. */
  89. public function iClickTheButtonInTheSection($class, $section) {
  90. $this->actor->find(self::buttonForTheSection($class, $section), 10)->click();
  91. }
  92. /**
  93. * @Then I see that the current section is :section
  94. */
  95. public function iSeeThatTheCurrentSectionIs($section) {
  96. Assert::assertEquals($this->actor->find(self::appNavigationCurrentSectionItem(), 10)->getText(), $section);
  97. }
  98. /**
  99. * @Then I see that the section :section is shown
  100. */
  101. public function iSeeThatTheSectionIsShown($section) {
  102. if (!WaitFor::elementToBeEventuallyShown(
  103. $this->actor,
  104. self::appNavigationSectionItemFor($section),
  105. $timeout = 10 * $this->actor->getFindTimeoutMultiplier())) {
  106. Assert::fail("The section $section in the app navigation is not shown yet after $timeout seconds");
  107. }
  108. }
  109. /**
  110. * @Then I see that the section :section is not shown
  111. */
  112. public function iSeeThatTheSectionIsNotShown($section) {
  113. if (!WaitFor::elementToBeEventuallyNotShown(
  114. $this->actor,
  115. self::appNavigationSectionItemFor($section),
  116. $timeout = 10 * $this->actor->getFindTimeoutMultiplier())) {
  117. Assert::fail("The section $section in the app navigation is still shown after $timeout seconds");
  118. }
  119. }
  120. /**
  121. * @Then I see that the section :section has a count of :count
  122. */
  123. public function iSeeThatTheSectionHasACountOf($section, $count) {
  124. Assert::assertEquals($this->actor->find(self::counterForTheSection($section), 10)->getText(), $count);
  125. }
  126. /**
  127. * @Then I see that the section :section does not have a count
  128. */
  129. public function iSeeThatTheSectionDoesNotHaveACount($section) {
  130. if (!WaitFor::elementToBeEventuallyNotShown(
  131. $this->actor,
  132. self::counterForTheSection($section),
  133. $timeout = 10 * $this->actor->getFindTimeoutMultiplier())) {
  134. Assert::fail("The counter for section $section is still shown after $timeout seconds");
  135. }
  136. }
  137. }