Not sure if I have posted about this before, but I thought it would not hurt to post again. In Moodle 3.8.1, 3.7.4 and 3.6.?, we have three new ways that plugins can hook into Behat, to make it easier and more efficient to write Behat tests for your plugin:
- Plugins can now hook into Moodle’s And the following “blah” exist: step.
Probably easiest to work out how to do this by looking at an example. Here is one from the quiz:
To make this
Given the following "mod_quiz > group overrides" exist:
| quiz | group | attempts |
| Test quiz name | G1 | 2 |
work, well, first you need code in class mod_quiz_generator extends testing_module_generator to create that sort of thing. Then you need a new class like class behat_mod_quiz_generator extends behat_generator_base which says how to map from the data in the Behat step to a call to the generator.
General documentation for how to do this is in the class comment on behat_generator_base: https://github.com/moodle/moodle/blob/265296a26e0400faa733b3f849029191c0d45ed0/lib/behat/classes/behat_generator_base.php#L32
Of course, you could always make completely custom steps to generate set-up for your tests, but hopefully being able to hook into the standard Moodle method lets you do with less code and more standardisation.
- Similarly, plugins can how hook into ‘Named selectors’, which are what makes the steps like And I click on "Add" "button" in the "Competency picker" "dialogue" work. In that step, ‘button’ and ‘dialogue’ are selectors, and now you can define ones specific to your plugin. E.g. we could define a selector like “mod_forumng > Post”.
To do this, you basically have to say how to convert from the selector info in the Behat step into an XPath. Here is an example from Moodle core. This time the extra code you would need to write goes in the class like behat_mod_forumng that probably already exists. You need to override an extra method get_partial_named_selectors.
Again, the best docs are in the PHP docs.
- Finally, there are two new steps:
When I am on the "Quiz 1" "mod_quiz > View" page
When I am on the "Quiz 1" "mod_quiz > View" page logged in as "student"
The point of the first step is to let you start on the page you want to test, without having to navigate through a bunch of irrelevant pages (which takes time, and which might break if they every change parts of Moodle outside your plugin). The point of the second step is that if you use the old When I log in as "student" step, that takes you to the Moodle ‘Dashboard’ page which is really slow to load, and so logging in and going straight to the page you want to test is faster.
To make both steps work, you just have add an override for one method to the existing behat_mod_forumng class. Example here: https://github.com/moodle/moodle/blob/87252eae2cb8edacc2e31f518ff4491c122be141/mod/quiz/tests/behat/behat_mod_quiz.php#L60 and docs here: https://github.com/moodle/moodle/blob/MOODLE_37_STABLE/lib/behat/behat_base.php#L1083.
I expect that most people won’t have time to update most old Behat tests to use these tings, but we should probably start using them for new tests.