Consider I have below feature files:
Login.feature
Feature: Login on website
Scenario: Login verification on site
Home.feature
Feature: Welcome Page Verification
Scenario: Verify the page that comes after login
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 {}
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.
Not really.. but you can use "background" and "scenario outline" in features..
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.
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.
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)
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.
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();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With