Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you call scenarios as steps in Behat 3?

Tags:

php

gherkin

behat

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?

like image 912
i.am.michiel Avatar asked Dec 27 '14 10:12

i.am.michiel


1 Answers

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

like image 95
Sławomir Chrobak Avatar answered Sep 30 '22 05:09

Sławomir Chrobak