LoginPageContext.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. /**
  3. *
  4. * @copyright Copyright (c) 2017, 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 Behat\Behat\Hook\Scope\BeforeScenarioScope;
  24. use PHPUnit\Framework\Assert;
  25. class LoginPageContext implements Context, ActorAwareInterface {
  26. use ActorAware;
  27. /**
  28. * @var FeatureContext
  29. */
  30. private $featureContext;
  31. /**
  32. * @var FilesAppContext
  33. */
  34. private $filesAppContext;
  35. public static function userNameField(): Locator {
  36. return Locator::forThe()->field("user")->
  37. describedAs("User name field in Login page");
  38. }
  39. public static function passwordField(): Locator {
  40. return Locator::forThe()->field("password")->
  41. describedAs("Password field in Login page");
  42. }
  43. public static function loginButton(): Locator {
  44. return Locator::forThe()->css(".button-vue[type='submit']")->
  45. describedAs("Login button in Login page");
  46. }
  47. public static function wrongPasswordMessage(): Locator {
  48. return Locator::forThe()->xpath("//*[@class = 'input-field__helper-text-message input-field__helper-text-message--error' and normalize-space() = 'Wrong username or password.']")->
  49. describedAs("Wrong password message in Login page");
  50. }
  51. /**
  52. * @return Locator
  53. */
  54. public static function userDisabledMessage() {
  55. return Locator::forThe()->xpath("//*[@class = 'input-field__helper-text-message input-field__helper-text-message--error' and normalize-space() = 'User disabled']")->
  56. describedAs('User disabled message on login page');
  57. }
  58. /**
  59. * @When I log in with user :user and password :password
  60. */
  61. public function iLogInWithUserAndPassword(string $user, string $password): void {
  62. $this->actor->find(self::userNameField(), 10)->setValue($user);
  63. $this->actor->find(self::passwordField())->setValue($password);
  64. $this->actor->find(self::loginButton())->click();
  65. }
  66. /**
  67. * @Then I see that the current page is the Login page
  68. */
  69. public function iSeeThatTheCurrentPageIsTheLoginPage() {
  70. Assert::assertStringStartsWith(
  71. $this->actor->locatePath("/login"),
  72. $this->actor->getSession()->getCurrentUrl());
  73. }
  74. /**
  75. * @Then I see that a wrong password message is shown
  76. */
  77. public function iSeeThatAWrongPasswordMessageIsShown() {
  78. Assert::assertTrue(
  79. $this->actor->find(self::wrongPasswordMessage(), 10)->isVisible());
  80. }
  81. /**
  82. * @Then I see that the disabled user message is shown
  83. */
  84. public function iSeeThatTheDisabledUserMessageIsShown() {
  85. Assert::assertTrue(
  86. $this->actor->find(self::userDisabledMessage(), 10)->isVisible());
  87. }
  88. /**
  89. * @BeforeScenario
  90. */
  91. public function getOtherRequiredSiblingContexts(BeforeScenarioScope $scope) {
  92. $environment = $scope->getEnvironment();
  93. $this->featureContext = $environment->getContext("FeatureContext");
  94. $this->filesAppContext = $environment->getContext("FilesAppContext");
  95. }
  96. /**
  97. * @Given I am logged in
  98. */
  99. public function iAmLoggedIn() {
  100. $this->featureContext->iVisitTheHomePage();
  101. $this->iLogInWithUserAndPassword("user0", "123456acb");
  102. $this->filesAppContext->iSeeThatTheCurrentPageIsTheFilesApp();
  103. }
  104. /**
  105. * @Given I am logged in as :userName
  106. */
  107. public function iAmLoggedInAs($userName) {
  108. $this->featureContext->iVisitTheHomePage();
  109. $this->iLogInWithUserAndPassword($userName, "123456acb");
  110. $this->filesAppContext->iSeeThatTheCurrentPageIsTheFilesApp();
  111. }
  112. /**
  113. * @Given I am logged in as the admin
  114. */
  115. public function iAmLoggedInAsTheAdmin() {
  116. $this->featureContext->iVisitTheHomePage();
  117. $this->iLogInWithUserAndPassword("admin", "admin");
  118. $this->filesAppContext->iSeeThatTheCurrentPageIsTheFilesApp();
  119. }
  120. /**
  121. * @Given I can not log in with user :user and password :password
  122. */
  123. public function iCanNotLogInWithUserAndPassword($user, $password) {
  124. $this->featureContext->iVisitTheHomePage();
  125. $this->iLogInWithUserAndPassword($user, $password);
  126. $this->iSeeThatTheCurrentPageIsTheLoginPage();
  127. $this->iSeeThatAWrongPasswordMessageIsShown();
  128. }
  129. }