Can you call scenarios as steps in Behat 3?
Asked Answered
B

1

5

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?

Bortman answered 27/12, 2014 at 10:22 Comment(2)
The guide to migrate from Behat 2 to Behat 3 has not yet been released. You therefore need to educate yourself with the help from the Changelog and pick out those things which are of your interest to further look them up. The build of the chained steps extension for behat 3 is currently broken.Digressive
Hi, did you find a way to do this? I need this functionality. IMO stripping this out was shortsighted as I use Behat for testing external APIs which have processes that need to be polled through a series of steps.Rajasthan
I
8

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

Irrawaddy answered 12/1, 2015 at 22:10 Comment(2)
Thanks for your answer. I indeed found this information myself and an extension by Stof re-introducing the chained steps. However, the one problem I have with your solution is the fact that you cannot have multiple context all extending the MinkContext. Behat will complain about "steps already being defined". Do you know how to solve this ?Bortman
This is a proper behavior. Here you've got it explained. If you want use Mink as a base for your contexts you have to extend over the RawMinkContext. It is all about how you're organizing your contexts.Bayle

© 2022 - 2024 — McMap. All rights reserved.