Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 310 and 401] [Versions 311 and 401] [Versions 39 and 401] [Versions 400 and 401] [Versions 401 and 403]

Behat hooks steps definitions. This methods are used by Behat CLI command.

Copyright: 2012 David MonllaĆ³
License: http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
File Size: 890 lines (34 kb)
Included or required:0 times
Referenced: 0 times
Includes or requires: 7 files
 lib/testing/classes/test_lock.php
 lib/behat/behat_base.php
 lib/behat/classes/behat_context_helper.php
 lib/behat/classes/behat_command.php
 lib/behat/classes/util.php
 lib/testing/classes/nasty_strings.php
 lib/behat/classes/behat_selectors.php

Defines 1 class


Class: behat_hooks  - X-Ref

Hooks to the behat process.

Behat accepts hooks after and before each
suite, feature, scenario and step.

They can not call other steps as part of their process
like regular steps definitions does.

Throws generic Exception because they are captured by Behat.

before_suite_hook(BeforeSuiteScope $scope)   X-Ref
Gives access to moodle codebase, ensures all is ready and sets up the test lock.

Includes config.php to use moodle codebase with $CFG->behat_* instead of $CFG->prefix and $CFG->dataroot, called
once per suite.

param: BeforeSuiteScope $scope scope passed by event fired before suite.

before_suite_final_checks(BeforeSuiteScope $scope)   X-Ref
Run final tests before running the suite.

param: BeforeSuiteScope $scope scope passed by event fired before suite.

before_feature(BeforeFeatureScope $scope)   X-Ref
Gives access to moodle codebase, to keep track of feature start time.

param: BeforeFeatureScope $scope scope passed by event fired before feature.

after_feature(AfterFeatureScope $scope)   X-Ref
Gives access to moodle codebase, to keep track of feature end time.

param: AfterFeatureScope $scope scope passed by event fired after feature.

after_suite(AfterSuiteScope $scope)   X-Ref
Gives access to moodle codebase, to keep track of suite timings.

param: AfterSuiteScope $scope scope passed by event fired after suite.

restart_session()   X-Ref
Helper function to restart the Mink session.


start_session()   X-Ref
Start the Session, applying any initial configuratino required.


before_goutte_scenarios(BeforeScenarioScope $scope)   X-Ref
Restart the session before each non-javascript scenario.

param: BeforeScenarioScope $scope scope passed by event fired before scenario.

before_first_scenario_start_session(BeforeScenarioScope $scope)   X-Ref
Start the session before the first javascript scenario.

This is treated slightly differently to try to capture when Selenium is not running at all.

param: BeforeScenarioScope $scope scope passed by event fired before scenario.

before_subsequent_scenario_start_session(BeforeScenarioScope $scope)   X-Ref
Start the session before each javascript scenario.

Note: Before the first scenario the @see before_first_scenario_start_session() function is used instead.

param: BeforeScenarioScope $scope scope passed by event fired before scenario.

before_scenario_hook(BeforeScenarioScope $scope)   X-Ref
Resets the test environment.

param: BeforeScenarioScope $scope scope passed by event fired before scenario.

mark_first_js_scenario_as_seen(BeforeScenarioScope $scope)   X-Ref
Mark the first Javascript Scenario as have been seen.

param: BeforeScenarioScope $scope scope passed by event fired before scenario.

before_step(BeforeStepScope $scope)   X-Ref
Hook to open the site root before the first step in the suite.
Yes, this is in a strange location and should be in the BeforeScenario hook, but failures in the test setUp lead
to the test being incorrectly marked as skipped with no way to force the test to be failed.

param: BeforeStepScope $scope

fetch_tags_for_scenario(\Behat\Behat\Hook\Scope\BeforeScenarioScope $scope)   X-Ref
Sets up the tags for the current scenario.

param: \Behat\Behat\Hook\Scope\BeforeScenarioScope $scope Scope

get_tags_for_scenario()   X-Ref
Gets the tags for the current scenario

return: array Array where key is tag name and value is an integer

before_step_javascript(BeforeStepScope $scope)   X-Ref
Wait for JS to complete before beginning interacting with the DOM.

Executed only when running against a real browser. We wrap it
all in a try & catch to forward the exception to i_look_for_exceptions
so the exception will be at scenario level, which causes a failure, by
default would be at framework level, which will stop the execution of
the run.

param: BeforeStepScope $scope scope passed by event fired before step.

after_step_javascript(AfterStepScope $scope)   X-Ref
Wait for JS to complete after finishing the step.

With this we ensure that there are not AJAX calls
still in progress.

Executed only when running against a real browser. We wrap it
all in a try & catch to forward the exception to i_look_for_exceptions
so the exception will be at scenario level, which causes a failure, by
default would be at framework level, which will stop the execution of
the run.

param: AfterStepScope $scope scope passed by event fired after step..

reset_webdriver_between_scenarios(AfterScenarioScope $scope)   X-Ref
Reset the session between each scenario.

param: AfterScenarioScope $scope scope passed by event fired after scenario.

get_run_faildump_dir()   X-Ref
Getter for self::$faildumpdirname

return: string

take_screenshot(AfterStepScope $scope)   X-Ref
Take screenshot when a step fails.

param: AfterStepScope $scope scope passed by event after step.

take_contentdump(AfterStepScope $scope)   X-Ref
Take a dump of the page content when a step fails.

param: AfterStepScope $scope scope passed by event after step.

get_faildump_filename(AfterStepScope $scope, $filetype)   X-Ref
Determine the full pathname to store a failure-related dump.

This is used for content such as the DOM, and screenshots.

param: AfterStepScope $scope scope passed by event after step.
param: String $filetype The file suffix to use. Limited to 4 chars.

i_look_for_exceptions()   X-Ref
Internal step definition to find exceptions, debugging() messages and PHP debug messages.

Part of behat_hooks class as is part of the testing framework, is auto-executed
after each step so no features will splicitly use it.


is_first_scenario()   X-Ref
Returns whether the first scenario of the suite is running

return: bool

is_first_javascript_scenario()   X-Ref
Returns whether the first scenario of the suite is running

return: bool

register_component_selectors_for_component(string $component)   X-Ref
Register a set of component selectors.

param: string $component

first_step_setup_complete(BeforeStepScope $scope)   X-Ref
Mark the first step as having been completed.

This must be the last BeforeStep hook in the setup.

param: BeforeStepScope $scope

log_and_stop(string $message)   X-Ref
Log a notification, and then exit.

param: string $message The content to dispaly