Torikul Alam
Torikul Alam

Reputation: 549

How to use nested Scenario Outline in Cucumber java

2suppose i have a Scenario Outline like

@Scenario1
Scenario Outline:Scenario one
    Given fill up login fields "<email>" and "<password>"
    And click the login button
 Examples:
      | email            | password |
      | someEmailAddress | SomePassword | 
      | someEmailAddress2| SomePassword2 | 

and another Scenario like

@Scenario2
Scenario Outline:Scenario two
    Given fill up  fields "<value1>" and "<value2>"
 Examples:
      | value1  | value2  |
      | value11 | value21 | 
      | value12 | value22 | 

How could i run scenario like login with 'someEmailAddress' and fill up with all scenario2 value and then login with 'someEmailAddress2' and do the same.

Upvotes: 3

Views: 4950

Answers (2)

Murthi
Murthi

Reputation: 5347

There is no support for nested scenario outline in Cucumber, but you can use the following way to overcome it.

Scenario Outline: Scenario one and two
  Given fill up login fields "<email>" and "<password>"
  And click the login button
  And fill up  fields "<value1>" and "<value2>"

  Examples:
    | email             | password      | value1  | value2  |
    | someEmailAddress  | SomePassword  | value11 | value21 |
    | someEmailAddress  | SomePassword  | value12 | value22 |
    | someEmailAddress2 | SomePassword2 | value11 | value21 |
    | someEmailAddress2 | SomePassword2 | value12 | value22 |

Upvotes: 2

diabolist
diabolist

Reputation: 4099

Cucumber scenarios are tools we use to describe behaviour i.e. what is happening and why its important. They are not tools to program tests. The way to use Cucumber effectively is to keep your scenarios simple, and let code called by step definitions do your programming for you.

Step definitions and the methods they call are written in a programming language. This gives you all the power you need to deal with the details of how you interact with your system.

The art of writing Cucumber scenarios is for each one to talk about

  1. The state we need setup so we can do something (Givens)
  2. Our interaction (When)
  3. What we expect to see after our interaction. (Then)

So for your scenario we have

Scenario: Login
  Given I am registered
  When I login
  Then I should be logged in

When we make this scenario work our program has the behaviour that we can login. So then we can use that behaviour in other scenarios e.g.

Scenario: See my profile
  Given I am logged in
  When I view my profile
  Then I should see my profile

Now to make this work we might need a bit more work because this scenario doesn't have a registered user yet. We can deal with this in a number of ways

1) Add another Given, perhaps in a background

Background:
    Given I am registered

  Scenario ...
    Given I am logged in

2) We can register in the login step e.g.

   Given "I am logged in" do
      @i = register_user
      login_as user: @i
    end 

Notice how in this step we are calling helper methods register_user and login_as to do the work for us.

This is the way to start using Cucumber. Notice how my scenarios have no mention of how we login, no email, no password, no filling in anything. To use Cucumber effectively you have to push these details down into the step definitions and the helper methods they call.

Summary

Keep you scenarios simple and use them to describe WHAT and explain WHY. Use the step definitions and helper methods to deal with HOW. There is no need to use Scenario Outlines when using Cucumber and you should never be nesting them.

Upvotes: 5

Related Questions