Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling one feature file from another feature in cucumber

Consider I have below feature files:

Login.feature

Feature: Login on website

Scenario: Login verification on site

  • Given Navigate to site login page
  • When User enters username 'admin1'
  • And User enters password 'admin1'
  • And User clicks on login button
  • Then User should not be able to log in successfully

Home.feature

Feature: Welcome Page Verification

Scenario: Verify the page that comes after login

  • Given Login is successfully done
  • When The page after login successfully appears
  • Then The test is done

In Home.feature file, I need to execute Login.feature first and then call home.feature. So when i execute home from my runner test it will in turn execute login and then home.

RunnerTest.java

import org.junit.runner.RunWith;

import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;

@RunWith(Cucumber.class)


@CucumberOptions(strict = false, features = {
        "src/test/resources/Features/Home.feature",
            }, glue = { "tests" }, plugin = "html:target/cucumber-reports", format = { "pretty",
        "json:target/cucumber.json" }, tags = { "~@ignore" })

public class RunnerTest {}
like image 337
Megha Dalvi Avatar asked Sep 28 '17 07:09

Megha Dalvi


People also ask

How do you call a feature file from another feature file in Cucumber?

You cannot call a feature file from another feature file. There are a number of methods of telling cucumber which feature files to run. Run `cucumber --help` and in particular look at -r -t -n -e and -p. You cannot have a master feature file which controls which features are executed.

Can we call a scenario from another scenario in Cucumber?

Not really.. but you can use "background" and "scenario outline" in features..

Can we run multiple feature files in Cucumber?

Cucumber can be executed in parallel using JUnit and Maven test execution plugins. In JUnit the feature files are run in parallel rather than scenarios, which means all the scenarios in a feature file will be executed by the same thread. You can use either Maven Surefire or Failsafe plugin to execute the runners.

What is the right way to execute a specific scenario from the feature file?

You can choose to run a specific scenario using the file:line format, or you can pass in a file with a list of scenarios using @-notation. steps, tags, comments, description, data tables or doc strings.


2 Answers

You don't need to call the first feature from the second feature. What you need to do is have a step in the second feature that can log you in. It can do this by calling code you've created when implementing your first feature.

The first feature is something you might write when you are implementing login for the first time. In doing this you will steps and code that these steps call to log you in.

The sort of code you should be creating is (sorry all examples are ruby i don't do java)

  1. A test user entity that knows its name, email and password
  2. A method that can user the test user to login

Then you can write a helper method e.g.

def login_as(user)
  visit login_path
  fill_in :email, with: user.email
  fill_in :password, with: user.password
  submit_form
end

and now in your second feature you can have something like

Given I am an admin
When I login

and implement these steps as

Given 'I am an admin' do
  # note create_user is a method you would have created when doing user 
  # registration/creation
  @i = create_user(type: admin)
end

When "I login" do
  login_as @i
end

and somewhere you will have some helper methods

module StepHelperMethods
  def create_user
    ...
    return user
  end

  def login_as(user)
    ...
  end
end
World StepHelperMethods

Your code reuse always happens at a much lower level. Ideally you should be re-using helper methods that you have created previously to make other scenarios work. You can also call steps directly (nested steps) but this is a very bad thing to do.

like image 152
diabolist Avatar answered Sep 30 '22 01:09

diabolist


Try this:

Create a RunnerLogin class to call the Login.feature.

In your steps class, in the method that implements the Given Login is successfully done, do something like:

@Given("^Login is successfully done$")
public void login_is_successfully_done() {
    Thread T1 = new Thread(new Thread(() -> {
        JUnitCore jExecFeature = new JUnitCore();
        Result result = jExecFeature.run(RunnerLogin.class);
    }));                

    T1.start();                
    T1.join(); 
}
like image 33
Ricardo Lopes Avatar answered Sep 30 '22 01:09

Ricardo Lopes