When writing functionnal tests, some parts are repeated quite frequently. For example users logging in :
I go to "/login"
I fill in "login" with "username"
I fill in "password" with "password"
I press "Login"
I would like to define those steps as :
Given I am logged in as "userA"
Now on Behat 2.x, I would define a step in php :
return array(
new Step\Given('I go to "/login"'),
new Step\Then('I fill in "login" with "username"'),
new Step\Then('I fill in "password" with "password"'),
new Step\Then('I press "Login"'),
);
Is this behaviour still encouraged for Behat 3? Is there a better way to do this?
This is called step execution chaining and it was removed in Behat 3. Here is the original answer from the Behat's creator.
If you want to use the MinkContext just extend it in your context or if your code is more complex use patterns like composition. Then you'll be able to directly call methods responsible for those steps like:
class FeatureContext extends MinkContext
{
/**
* @Given I am logged in as :user
*/
public function iAmLoggedInAsUser($user)
{
$this->visit('/login');
$this->fillField('login', 'username');
$this->fillField('password', 'password');
$this->pressButton('Login');
// make assertion to be sure user is logged in
}
}
Another great conversation about Behat's contexts, steps and its language is here
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With