Generate java test class from features in cucumber - java

I am new to cucumber and wanted to understand if there is any plugin to generate java test class code from a cucumber feature file.
Example : I have the below scenario -
Scenario: Determine past date
Given today is 2011-01-20
When I ask if Jan 19, 2011 is in the past
Then the result should be yes
Is there a way to generate test class with the methods for each?
I am just looking to generate the skeleton of the class so that it speeds up the development process.

You can run the feature with a runner class like:
import org.junit.runner.RunWith;
import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;
#RunWith(Cucumber.class)
#CucumberOptions(
dryRun = false,
strict = true,
plugin = {"pretty"},
features = {"path/to/features"},
glue = {"package.of.steps"},
tags = {"#TagsToRun"})
public class MyCucumberTestRunnner {
public MyCucumberTestRunnner() {
}
}
This can be executed as JUnit Test and Cucumber will tell you, that there are missing steps and will provide you the Step Skeletons.
if the glue code is in the same package you dont need to provide the information

Related

Cucumber hooks are not running. I am using io.cucumber.java8

I am trying to run steps required before and after the scenarios using Cucumber hooks, but the methods in hooks class are not running. Below is the hooks class.
Used dependencies :
<cucumber.version>6.11.0</cucumber.version>
package com.neobank.hooks;
import com.neobank.core.Driver;
import io.cucumber.java8.En;
public class ScenarioHooks implements En {
public ScenarioHooks() {
Before(Driver::startAppiumDriver);
After(Driver::resetApp);
After(Driver::stopChromeDriver);
}
}
Glue Path:
#RunWith(Cucumber.class)
#CucumberOptions(
plugin = {"pretty", "html:target/cucumber.html",
"json:target/cucumber.json"},
features = {"src/test/resources/features"},
glue = {"com.neobank.steps", "com.neobank.hooks"},
tags = "#Onboardingtwo"
Found out the issue here. Was trying to run the cucumber scenarios individually from feature files. If hooks are to be executed then we have to run using the runner file.

cucumber using java - Null pointer exception on executing tests from runner class

i am getting null pointer execption when executing scenarios from runner class. when i execute from feature file then tests are executed without any error, I am using tags to run the scenarios and i have mentioned the tags in runner class, please let me know what might be the reason.
Runner Class Code:
#RunWith(Cucumber.class)
#CucumberOptions( features={"Features"} ,glue={"project.stepdef"} ,tags = {"#chrome","#smoke"} , format = {"pretty", "html:target/site/cucuber-pretty","json:target/site/cucumber.json"} // ,monochrome = true )
public class CucumberRunner
{
}
You need to make some minor tweaks to the runner Class as follows:
import org.junit.runner.RunWith;
import cucumber.api.junit.Cucumber;
#RunWith(Cucumber.class)
#CucumberOptions(
features="Features",
glue="stepdef",
tags = {"#chrome","#smoke"},
plugin = {"html:target/cucumber-html-report",
"pretty:target/cucumber-pretty.text",
"json:target/cucumber.json"},
)
public class CucumberRunner {}

cucumber feature file to run in order

Problem: I need to run my cucumber .feature file to execute in an order defined by me and rather not to run in the default order which is the folder structure.
I am running Appium for Android Native Apps built using cucumber .features file.
on windows machine, running on actual devices.
Now my Runcuckes file looks like below:
package runner;
import org.junit.runner.RunWith;
import org.testng.annotations.Test;
import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;
import cucumber.api.testng.AbstractTestNGCucumberTests;
//#RunWith(Cucumber.class)
#CucumberOptions(features = { "src/test/java/features" },
glue = { "Steps" },
monochrome = true,
tags = { "#CustomerInsightsSurveyPopupGiveFeedback,"
+ "#TestAccountSceanrios"
+ "#ShortlistPage,"
+ "#SavedSearchesPage,"
+ "#SearchResultPage,"
+ "#Short,"
+ "#SuggestedSearch" })
// public class RunCucke {
public class RunCucke extends AbstractTestNGCucumberTests {
}
Running your features or scenarios in order is Cuking the WRONG way.
In all testing linking one test to another is an anti-pattern. It makes your tests fragile and difficult to debug. Each test should be independent of every other test.
In Cucumber you use Givens to setup the state of your scenario. When's to actually do something. Then's to check your results. Your scenarios Given's should include everything needed to setup your application so you can do your When.
Cucumber encourages you to run your scenarios in a random order, and to reset just about everything between each scenario. Don't work against this, you will make things much more difficult if you do.

Cucumber:- Unable to generate step definitions by running feature file as well as testrunner class

I am trying to generate the step definitions from my feature file and as well as I have also designed test runner class but upon execution both give output on console as :-
0 scenarios
0 steps
0m0s.000s
Even though my feature file contains scenarios and steps.
Remove the colon (:) after the keywords (Given, When, etc) in your feature file.
Since you haven't shared any code or much details as to what you've done the only assumption that I can make is you have done something wrong in your testrunner class.
#RunWith(Cucumber.class)
#CucumberOptions(
features = "Feature"
,glue={"stepDefinition"}
)
public class TestRunner {
}
in the features make sure the path to your feature files is correct. i.e. if they are stored at some other directory, provide the path for the same
Ex: features = {"src/test/java/features"}
Also, please share your project structure, your feature file and your testrunner class code if possible in case this doesn't work for you.
Actually my runner class file looks like this:-
package runner;
import org.testng.annotations.Test;
import cucumber.api.CucumberOptions;
import cucumber.api.testng.AbstractTestNGCucumberTests;
#CucumberOptions(features={"src//test//resources//featurefiles"},glue= {"im801clsteps"},plugin={"html:target/cucumber-html-report",
"json:target/cucumber.json", "pretty:target/cucumber-pretty.txt"})
#Test
public class MainRunner extends AbstractTestNGCucumberTests {
}
And I am using testng not junit to run my tests,please let me know why I am wrong?

Getting no result when run through cucumber. Trying to implement BDD

Already tried almost all solutions which are on SO but still missing something here.
I have created simple JAVA program, Added Feature file and Class for cucumber. When I run I am getting output :
#Search Scenario Outline: Successful Open Google.com [90m#
Open_Google.feature:4[0m
[36mGiven [0m[36mUser is with blank page[0m
[36mWhen [0m[36mUser enter URL[0m
[36mThen [0m[36mGoogle WebSite should open[0m
0 Scenarios
0 Steps
0m0.000s
Feature File :
Feature: Open Google WebSite
#Search
Scenario Outline: Successful Open Google.com
Given User is with blank page
When User enter URL
Then Google WebSite should open
Test Runner Class :
import org.junit.runner.RunWith;
import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;
#RunWith(Cucumber.class)
#CucumberOptions(
features = "Feature"
)
public class TestRunner {
}
Test Case Class :
public class cucumber_test {
public static WebDriver driver;
public static void main(String[] args) {
// TODO Auto-generated method stub
System.setProperty("webdriver.chrome.driver", "D:\\chromedriver.exe");
driver = new ChromeDriver();
driver.get("http://www.google.com");
driver.manage().window().maximize();
System.out.println("Google open successfully");
}
}
Using Selenium Webdriver, JAVA, Junit and cucumber.
Also Am I doing right? Is it correct method to use cucumber?
It seems like the runner is unable to find your feature file. Is it located in the resources? If it is, try referencing the whole classpath like
import org.junit.runner.RunWith;
import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;
#RunWith(Cucumber.class)
#CucumberOptions(
features = "classpath:com/yourgroup/yourartifact/cucumber/features"
)
public class TestRunner {
}
Above is just an example, of course you have to alter that classpath depending on where your features are located.
You need to reference the location of your features and your step definitions. the runner should look something like this:
import org.junit.runner.RunWith;
import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;
#RunWith(Cucumber.class)
#CucumberOptions(
features = {"path/to/features/"},
glue = {"classpath:package.name.of.stepsDefinitions"},
)
public class TestRunner {
}
Note the path notation for the feature files
and the package notation for the glue code (step definitions)
I believe you still facing same problem. You could try this.
import org.junit.runner.RunWith;
import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;
#RunWith(Cucumber.class)#CucumberOptions(plugin = {
"pretty", "json:target/Open-Google-WebSite.json"},
features = {"src/test/FeatureFilePackage"},
glue = {"com.java.cucumber_test"})
public class TestRunner {
}
it seems your test is running through testng which is not showing any specific error i would recommend you remove testNg dependency from your pom file run your test (through Junit) and you will be able to see specific error and after resolving it you will able to run your class easily
expected error might be "duplicate step defination"

Categories