LoginPageContext.php 4.5 KB

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