Skip to content

Commit 98b380c

Browse files
committed
[docs] Add information for behat::execute() context definition
1 parent 68198bf commit 98b380c

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed

general/development/tools/behat/writing.md

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,70 @@ Sometimes, you will need to set up data that is specific to your plugin, or perf
264264

265265
As well as creating completely new steps, you can also extend some of the standard steps:
266266

267+
#### Calling other steps
268+
269+
When writing custom steps you will often want to perform actions, such as:
270+
271+
- clicking a link
272+
- pressing a button
273+
- typing into a field
274+
- calling another existing step
275+
276+
When doing this you **should** use the `\behat_session_trait::execute()` method to call the existing step to perform the action.
277+
You **should not** call the `->click()` method on a `NodeElement` manually as this will bypass some of the error detection states, and pausing to wait for JavaScript actions to take place.
278+
279+
The `\behat_session_trait::execute()` method accepts:
280+
281+
- the name of the method to call on a behat context class; and
282+
- any arguments.
283+
284+
For example:
285+
286+
```php title="behat_general.php"
287+
/**
288+
* Toggles the specified admin switch.
289+
*
290+
* @When /^I toggle the "(?P<element_string>(?:[^"]|\\")*)" admin switch "(?P<state_string>on|off)"$/
291+
* @param string $element Element we look for
292+
* @param string $state The state of the switch
293+
* @throws ElementNotFoundException Thrown by behat_base::find
294+
*/
295+
public function i_toggle_admin_switch($element, $state) {
296+
// First check we are running Javascript, otherwise explode.
297+
if (!$this->running_javascript()) {
298+
throw new \Behat\Mink\Exception\DriverException('Switches are only available with JavaScript enabled');
299+
}
300+
301+
// Next check that the node is available.
302+
$node = $this->get_selected_node('checkbox', $element);
303+
$this->ensure_node_is_visible($node);
304+
305+
// Update the state of the switch.
306+
$field = $node->getAttribute('id');
307+
if ($state == "on") {
308+
$this->execute('behat_forms::i_set_the_field_to', [$field, 1]);
309+
} else if ($state == "off") {
310+
$this->execute('behat_forms::i_set_the_field_to', [$field, 0]);
311+
} else {
312+
throw new \Behat\Mink\Exception\ExpectationException('Invalid state for switch: ' . $state, $this->getSession());
313+
}
314+
}
315+
```
316+
317+
<Since issueNumber="MDL-86231" versions={["4.1.21", "4.4.11", "4.5.7", "5.0.3"]} />
318+
319+
The `\behat_session_trait::execute()` method accepts the method on the behat context class in the callable array format:
320+
321+
```php
322+
$this->execute([\behat_forms::class, 'i_set_the_field_to'], [$field, 0]);
323+
```
324+
325+
:::note
326+
327+
Only the string format, adn the array callable are supported. You cannot pass a callback method.
328+
329+
:::
330+
267331
#### Custom selectors (<tt>... in the "..." "..."</tt>)
268332

269333
There are a load of different steps which can refer to specific items on-screen, for example

0 commit comments

Comments
 (0)