Calling one feature file from another feature in cucumber
Asked Answered
H

3

7

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 {}
Holbrook answered 28/9, 2017 at 7:21 Comment(0)
L
3

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.

Lactone answered 28/9, 2017 at 9:36 Comment(0)
M
0

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(); 
}
Microsporophyll answered 14/10, 2017 at 9:54 Comment(0)
L
-1

Behind every feature there is a method implemented in your step definitions, so you will not need to write your features in that manner, you will just need to call the method that you have already implemented.

The best solution for frequently used functionalities of your application like Login for e.g is to have Helper methods. You can implement these methods as global and use them in your every step definition file.

public void LoginUserGlobal(String user, String pass) throws Throwable { LoginPageObjects LP = new LoginPageObjects(driver);

    LP.user().sendKeys(user);
    LP.pass().sendKeys(pass);
    LP.loginButton().click();
}

And if you need to use it in any of your features you will code it like:

@When("^User logs in with \"([^\"]*)\" and \"([^\"]*)\"$")
public void user_logs_in_with_something_and_something(String user, String pass) throws Throwable {
  GlobalClassWhereIsYourLoginMethod Lin = new GlobalClassWhereIsYourLoginMethod (driver)
  LoginUserGlobal(user,pass);
}

or if it is not so used part of app, you will call it from a step definition method where is already implemented.

Luganda answered 25/9, 2020 at 15:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.