Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call a feature inside another feature in cucumber-jvm?

I have a feature file

Feature: Create Profile

Scenario: Create Profile
Given I want to create a profile
When I create a profile
Then I should be navigated to Home Page
Then sign out link should exist

So the above runs all ok and asserts that it's indeed back in the Home Page and the sign out link exists.

And now I have another feature file.

Feature: Go to my account page

Scenario: Go to my account page
Given I want to go to my account page    
When I go to my account page
Then I should be navigated to the my account page

Before running "When I go to my account page" step, the user should "Create Profile".

So what I did was I appended the

When I create a profile
Then I should be navigated to Home Page
Then sign out link should exist

before When I go to my account page.

But I see that I have duplicated the same code that is in the "Create Profile" feature/scenario.

How do I run the entire "Create Profile" feature/scenario inside the "Go to my account page" scenario?

I am using cucumber-jvm with Selenium and JUnit.

like image 460
Ranhiru Jude Cooray Avatar asked Nov 05 '22 01:11

Ranhiru Jude Cooray


1 Answers

Did you see the Background DSL-feature? It would work for your case but is probably not what you're literally asking about. In this case you can require user to create profile by:

Feature: Create Profile
  Background:
    Given I create a profile
    And I should be navigated to Home Page
    And sign out link should exist

  Scenario: Create Profile
    # do nothing because all actions are in background

  Scenario: Go to my account page
    When I go to my account page
    Then I should be navigated to the my account page

But you have to merge the two your feature files into a single feature file.

Also take a look at @Before and @After cucumber annotations - so that you can run some code to initialize (or create) an account for your test if previous solution wouldn't work for you.

like image 63
Artem Oboturov Avatar answered Nov 09 '22 10:11

Artem Oboturov