Skip to content

Commit

Permalink
MDL-46891 behat: Optimised login step and context helper
Browse files Browse the repository at this point in the history
  • Loading branch information
Rajesh Taneja committed Mar 11, 2016
1 parent 42ad096 commit 86055d1
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 67 deletions.
28 changes: 28 additions & 0 deletions auth/manual/tests/behat/auth_manual.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
@auth @auth_manual
Feature: Test manual authentication works.
In order to check manual authentication
As a teacher
I need to go on login page and enter username and password.

Background:
Given the following "users" exist:
| username |
| teacher1 |

@javascript
Scenario: Check login works with javascript.
Given I am on homepage
And I expand navigation bar
And I click on "Log in" "link" in the ".logininfo" "css_element"
When I set the field "Username" to "teacher1"
And I set the field "Password" to "teacher1"
When I press "Log in"
Then I should see "You are logged in as"

Scenario: Check login works without javascript.
Given I am on homepage
And I click on "Log in" "link" in the ".logininfo" "css_element"
When I set the field "Username" to "teacher1"
And I set the field "Password" to "teacher1"
When I press "Log in"
Then I should see "You are logged in as"
65 changes: 8 additions & 57 deletions auth/tests/behat/behat_auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,34 +47,16 @@ class behat_auth extends behat_base {
* @Given /^I log in as "(?P<username_string>(?:[^"]|\\")*)"$/
*/
public function i_log_in_as($username) {
// Visit login page.
$this->getSession()->visit($this->locate_path('login/index.php'));

// Running this step using the API rather than a chained step because
// we need to see if the 'Log in' link is available or we need to click
// the dropdown to expand the navigation bar before.
$this->getSession()->visit($this->locate_path('/'));
// Enter username and password.
$behatforms = behat_context_helper::get('behat_forms');
$behatforms->i_set_the_field_to('Username', $this->escape($username));
$behatforms->i_set_the_field_to('Password', $this->escape($username));

// Generic steps (we will prefix them later expanding the navigation dropdown if necessary).
$steps = array(
new Given('I click on "' . get_string('login') . '" "link" in the ".logininfo" "css_element"'),
new Given('I set the field "' . get_string('username') . '" to "' . $this->escape($username) . '"'),
new Given('I set the field "' . get_string('password') . '" to "'. $this->escape($username) . '"'),
new Given('I press "' . get_string('login') . '"')
);

// If Javascript is disabled we have enough with these steps.
if (!$this->running_javascript()) {
return $steps;
}

// Wait for the homepage to be ready.
$this->getSession()->wait(self::TIMEOUT * 1000, self::PAGE_READY_JS);

// If it is needed, it expands the navigation bar with the 'Log in' link.
if ($clicknavbar = $this->get_expand_navbar_step()) {
array_unshift($steps, $clicknavbar);
}

return $steps;
// Press log in button.
$behatforms->press_button(get_string('login'));
}

/**
Expand All @@ -101,35 +83,4 @@ public function i_log_out() {

return $steps;
}

/**
* Returns a step to open the navigation bar if it is needed.
*
* The top log in and log out links are hidden when middle or small
* size windows (or devices) are used. This step returns a step definition
* clicking to expand the navbar if it is hidden.
*
* @return Given|bool A step definition or false if there is no need to show the navbar.
*/
protected function get_expand_navbar_step() {

// Checking if we need to click the navbar button to show the navigation menu, it
// is hidden by default when using clean theme and a medium or small screen size.

// The DOM and the JS should be all ready and loaded. Running without spinning
// as this is a widely used step and we can not spend time here trying to see
// a DOM node that is not always there (at the moment clean is not even the
// default theme...).
$navbuttonjs = "return (
Y.one('.btn-navbar') &&
Y.one('.btn-navbar').getComputedStyle('display') !== 'none'
)";

// Adding an extra click we need to show the 'Log in' link.
if (!$this->getSession()->getDriver()->evaluateScript($navbuttonjs)) {
return false;
}

return new Given('I click on ".btn-navbar" "css_element"');
}
}
2 changes: 1 addition & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 10 additions & 8 deletions lib/behat/classes/behat_context_helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

// NOTE: no MOODLE_INTERNAL test here, this file may be required by behat before including /config.php.

use \Behat\Behat\Context\BehatContext;
use Behat\Testwork\Environment\Environment;

/**
* Helper to get behat contexts.
Expand All @@ -38,18 +38,20 @@
class behat_context_helper {

/**
* @var BehatContext main behat context.
* Behat environment.
*
* @var Environment
*/
protected static $maincontext = false;
protected static $environment = null;

/**
* Save main behat context reference to be used for finding sub-contexts.
* Sets the browser session.
*
* @param BehatContext $maincontext
* @param Environment $environment
* @return void
*/
public static function set_main_context(BehatContext $maincontext) {
self::$maincontext = $maincontext;
public static function set_session(Environment $environment) {
self::$environment = $environment;
}

/**
Expand All @@ -65,7 +67,7 @@ public static function set_main_context(BehatContext $maincontext) {
*/
public static function get($classname) {

if (!$subcontext = self::$maincontext->getSubcontextByClassName($classname)) {
if (!$subcontext = self::$environment->getContext($classname)) {
throw coding_exception('The required "' . $classname . '" class does not exist');
}

Expand Down
2 changes: 1 addition & 1 deletion lib/tests/behat/behat_hooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ public function before_scenario(BeforeScenarioScope $scope) {
// We need the Mink session to do it and we do it only before the first scenario.
if (self::is_first_scenario()) {
behat_selectors::register_moodle_selectors($session);
behat_context_helper::set_main_context($event->getContext()->getMainContext());
behat_context_helper::set_session($scope->getEnvironment());
}

// Reset mink session between the scenarios.
Expand Down
31 changes: 31 additions & 0 deletions lib/tests/behat/behat_navigation.php
Original file line number Diff line number Diff line change
Expand Up @@ -384,4 +384,35 @@ protected function get_navigation_node($nodetext, $parentnode = null) {
}
return $node;
}

/**
* Step to open the navigation bar if it is needed.
*
* The top log in and log out links are hidden when middle or small
* size windows (or devices) are used. This step returns a step definition
* clicking to expand the navbar if it is hidden.
*
* @Given /^I expand navigation bar$/
*/
public function get_expand_navbar_step() {

// Checking if we need to click the navbar button to show the navigation menu, it
// is hidden by default when using clean theme and a medium or small screen size.

// The DOM and the JS should be all ready and loaded. Running without spinning
// as this is a widely used step and we can not spend time here trying to see
// a DOM node that is not always there (at the moment clean is not even the
// default theme...).
$navbuttonjs = "return (
Y.one('.btn-navbar') &&
Y.one('.btn-navbar').getComputedStyle('display') !== 'none'
)";

// Adding an extra click we need to show the 'Log in' link.
if (!$this->getSession()->getDriver()->evaluateScript($navbuttonjs)) {
return false;
}

return new Given('I click on ".btn-navbar" "css_element"');
}
}

0 comments on commit 86055d1

Please sign in to comment.