1
0

SettingsMenuContext.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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 SettingsMenuContext implements Context, ActorAwareInterface {
  26. use ActorAware;
  27. /**
  28. * @return Locator
  29. */
  30. public static function settingsSectionInHeader() {
  31. return Locator::forThe()->xpath("//*[@id = 'header']//*[@id = 'user-menu']")->
  32. describedAs("Settings menu section in the header");
  33. }
  34. /**
  35. * @return Locator
  36. */
  37. public static function settingsMenuButton() {
  38. return Locator::forThe()->css(".header-menu__trigger")->
  39. descendantOf(self::settingsSectionInHeader())->
  40. describedAs("Settings menu button");
  41. }
  42. /**
  43. * @return Locator
  44. */
  45. public static function settingsMenu() {
  46. return Locator::forThe()->css(".user-menu__nav")->
  47. descendantOf(self::settingsSectionInHeader())->
  48. describedAs("Settings menu");
  49. }
  50. /**
  51. * @return Locator
  52. */
  53. public static function usersMenuItem() {
  54. return self::menuItemFor("Users");
  55. }
  56. /**
  57. * @return Locator
  58. */
  59. public static function usersAppsItem() {
  60. return self::menuItemFor("Apps");
  61. }
  62. /**
  63. * @return Locator
  64. */
  65. public static function logOutMenuItem() {
  66. return self::menuItemFor("Log out");
  67. }
  68. /**
  69. * @return Locator
  70. */
  71. private static function menuItemFor($itemText) {
  72. return Locator::forThe()->xpath("//a[normalize-space() = '$itemText']")->
  73. descendantOf(self::settingsMenu())->
  74. describedAs($itemText . " item in Settings menu");
  75. }
  76. /**
  77. * @param string $itemText
  78. * @return Locator
  79. */
  80. private static function settingsPanelFor($itemText) {
  81. return Locator::forThe()->xpath("//div[@id = 'app-navigation' or contains(@class, 'app-navigation')]//ul//li[@class = 'app-navigation-caption' and normalize-space() = '$itemText']")->
  82. describedAs($itemText . " item in Settings panel");
  83. }
  84. /**
  85. * @param string $itemText
  86. * @return Locator
  87. */
  88. private static function settingsPanelEntryFor($itemText) {
  89. return Locator::forThe()->xpath("//div[@id = 'app-navigation' or contains(@class, 'app-navigation')]//ul//li[normalize-space() = '$itemText']")->
  90. describedAs($itemText . " entry in Settings panel");
  91. }
  92. /**
  93. * @return array
  94. */
  95. public function menuItems() {
  96. return $this->actor->find(self::settingsMenu(), 10)
  97. ->getWrappedElement()->findAll('xpath', '//a');
  98. }
  99. /**
  100. * @When I open the Settings menu
  101. */
  102. public function iOpenTheSettingsMenu() {
  103. $this->actor->find(self::settingsMenuButton(), 10)->click();
  104. }
  105. /**
  106. * @When I open the User settings
  107. */
  108. public function iOpenTheUserSettings() {
  109. $this->iOpenTheSettingsMenu();
  110. $this->actor->find(self::usersMenuItem(), 2)->click();
  111. }
  112. /**
  113. * @When I open the Apps management
  114. */
  115. public function iOpenTheAppsManagement() {
  116. $this->iOpenTheSettingsMenu();
  117. $this->actor->find(self::usersAppsItem(), 2)->click();
  118. }
  119. /**
  120. * @When I visit the settings page
  121. */
  122. public function iVisitTheSettingsPage() {
  123. $this->iOpenTheSettingsMenu();
  124. $this->actor->find(self::menuItemFor('Settings'), 2)->click();
  125. }
  126. /**
  127. * @When I visit the admin settings page
  128. */
  129. public function iVisitTheAdminSettingsPage() {
  130. $this->iOpenTheSettingsMenu();
  131. $this->actor->find(self::menuItemFor('Administration settings'), 2)->click();
  132. }
  133. /**
  134. * @When I log out
  135. */
  136. public function iLogOut() {
  137. $this->iOpenTheSettingsMenu();
  138. $this->actor->find(self::logOutMenuItem(), 2)->click();
  139. }
  140. /**
  141. * @Then I see that the Settings menu is shown
  142. */
  143. public function iSeeThatTheSettingsMenuIsShown() {
  144. Assert::assertTrue(
  145. $this->actor->find(self::settingsMenu(), 10)->isVisible());
  146. }
  147. /**
  148. * @Then I see that the Settings menu has only :items items
  149. */
  150. public function iSeeThatTheSettingsMenuHasOnlyXItems($items) {
  151. Assert::assertCount(intval($items), self::menuItems());
  152. }
  153. /**
  154. * @Then I see that the :itemText item in the Settings menu is shown
  155. */
  156. public function iSeeThatTheItemInTheSettingsMenuIsShown($itemText) {
  157. Assert::assertTrue(
  158. $this->actor->find(self::menuItemFor($itemText), 10)->isVisible());
  159. }
  160. /**
  161. * @Then I see that the :itemText item in the Settings menu is not shown
  162. */
  163. public function iSeeThatTheItemInTheSettingsMenuIsNotShown($itemText) {
  164. $this->iSeeThatTheSettingsMenuIsShown();
  165. try {
  166. Assert::assertFalse(
  167. $this->actor->find(self::menuItemFor($itemText))->isVisible());
  168. } catch (NoSuchElementException $exception) {
  169. }
  170. }
  171. /**
  172. * @Then I see that the :itemText settings panel is shown
  173. */
  174. public function iSeeThatTheItemSettingsPanelIsShown($itemText) {
  175. Assert::assertTrue(
  176. $this->actor->find(self::settingsPanelFor($itemText), 10)->isVisible()
  177. );
  178. }
  179. /**
  180. * @Then I see that the :itemText entry in the settings panel is shown
  181. */
  182. public function iSeeThatTheItemEntryInTheSettingsPanelIsShown($itemText) {
  183. Assert::assertTrue(
  184. $this->actor->find(self::settingsPanelEntryFor($itemText), 10)->isVisible()
  185. );
  186. }
  187. /**
  188. * @Then I see that the :itemText settings panel is not shown
  189. */
  190. public function iSeeThatTheItemSettingsPanelIsNotShown($itemText) {
  191. try {
  192. Assert::assertFalse(
  193. $this->actor->find(self::settingsPanelFor($itemText), 10)->isVisible()
  194. );
  195. } catch (NoSuchElementException $exception) {
  196. }
  197. }
  198. }