Skip to main content

Beha Steps & Steps Definition

A feature file has many Steps, and each Step has a Step Definition associated with it. When Behat executes a Step in a Scenario it look for a matching Step Definition to execute in FeatureContext. Step Definitions are also known as Glue Code in Cucumber terminology.

Here is the sample feature file consist of single scenario and 2 steps, one of the step is already exist in Mink Context and other does not exist even in FeatureContext.

Feature: Login
  Scenario: My First login
    Given I am on the homepage
    Then I follow "Sign in" link

Once you will execute your behat command you will find output like this

(base) Taruns-MBP:behatpractice tarungoswami$ bin/behat
Feature: Login

  Scenario: My First login       # features/Login.feature:2
    Given I am on the homepage   # Behat\MinkExtension\Context\MinkContext::iAmOnHomepage()
    Then I follow "Sign in" link

1 scenario (1 undefined)
2 steps (1 passed, 1 undefined)
0m0.08s (10.98Mb)

 >> default suite has undefined steps. Please choose the context to generate snippets:

  [0] None
  [1] Behat\MinkExtension\Context\MinkContext

You can see 1 scenario, 1 undefined and it has 2 Steps and one of them is undefined and once you will choose 1, you will get this

 > 1

--- Behat\MinkExtension\Context\MinkContext has missing steps. Define them with these snippets:

     * @Then I follow :arg1 link
    public function iFollowLink($arg1)
        throw new PendingException();

We have suggestion that MinkContext has missing steps. Define them with these snippets. Copy and paste this to your FeatureContext file and next step is to comment or delete line starting with throw keyword.

Step Definitions are written inside a FeatureContext class file. Behat matches FeatureContext methods to step definitions using pattern matching. When Behat runs, it compares lines of Gherkin steps from each scenario to the patterns bound to each method in your FeatureContext. If the line of Gherkin satisfies a bound pattern, its corresponding step definition is executed.

Let’s take a closer look at this code:

  • Notice the comment block starts with /**, and not the usual /*. This is important for Behat to be able to parse such comments as annotations!
  • @Then is a definition keyword. There are 3 supported keywords in annotations: @Given/@When/@Then. These three definition keywords are actually equivalent, but all three are available so that your step definition remains readable. Behat does not differentiate between step keywords when matching patterns to methods. So a step defined with @Then could also be matched to @Given ..., @When ..., @And ..., @But ..., etc.
  • The text after the keyword is the step text pattern (e.g. I follow :arg1 link).
  • All token values of the pattern (e.g. :arg1) will be captured and passed to the method argument with the same name ($arg1). A pattern can’t automatically determine the datatype of its matches, so all method arguments coming from step definitions are passed as strings.

Now execute your feature file again, you will not receive any error.  If you are still getting the error, make sure that FeatureContext is added in your behat.yml in context section:

    - FeatureContext
    - Behat\MinkExtension\Context\MinkContext

Submitted by tgoswami on October 5, 2020

Tarun has 11+ years of experience in Quality Assurance in different domains like Banking, E-commerce, Health and Education.


At ProgramsBuzz, you can learn, share and grow with millions of techie around the world from different domain like Data Science, Software Development, QA and Digital Marketing. You can ask doubt and get the answer for your queries from our experts.