How to mention fully classified class name in glue attribute in Cucumber - java

I am new in Cucumber and created one sample application
Here is the folder structure of itCucumber folder structure
My test runner class code is
#RunWith(Cucumber.class)
#CucumberOptions(features = "Feature/SampleTest.feature",
glue = { "com.testSteps.Test_Steps" })
public class TestRunner {
}
Now my step definition class is Test_Steps but if I mention that class in glue attribute i.e. glue = { "com.testSteps.Test_Steps" } then test runner is not able to find the test definition class. Below console output I am getting
You can implement missing steps with the snippets below:
#Given("^User is on Home Page$")
public void user_is_on_Home_Page() throws Throwable {
// Write code here that turns the phrase above into concrete actions
throw new PendingException();
}
Please help me define step definition class with glue attribute because in case I have multiple step definition classes in my step definition folder structure then how can I target particular step definition class in glue attribute?

The glue parameter in your #CucumberOptions must point to a package, not a class. So in your case:
glue = {"com.testSteps.", "", ...}
And you features parameter should point to a folder with all the feature files. It can have subdirectories as well.

Related

How to provide external path for step definition files in citrus cucmber

I have a code which is working fine until I change the path of glue code here is the working code
#RunWith(Cucumber.class)
#CucumberOptions(features="D:/citrus/Feature",
strict = true,
glue = { "todo" },
plugin = { "com.consol.citrus.cucumber.CitrusReporter" } )
public class TodoFeatureTest {
}
I am able to specify external path i.e out side of eclipse project but when I want to specify external path for glue ={} option I am getting un implemented steps error what I can do for this. I want to specify step definition files outside of the project.
cucumber.glue= # comma separated package names. example: com.example.glue
see - https://cucumber.io/docs/cucumber/api/#junit
the values for glue must be package names where to find the deep definitions, not paths
it really does not matter where you implement the step definitions (same project or external jars), as long as they are on test class path. make sure correct dependency is added for the test.
Try using:
features={"D:/citrus/Feature"}
Instead of:
features="D:/citrus/Feature"

Cucumber exception when run the test

I have a problem that I think is silly ...
I can't run my tests on the cucumber.
Returns the following error:
cucumber.runtime.CucumberException:
Classes annotated with #RunWith(Cucumber.class) must not define any
Step Definition or Hook methods. Their sole purpose is to serve as
an entry point for JUnit. Step Definitions and Hooks should be defined
in their own classes. This allows them to be reused across features.
Offending class: class Teste.testecucumber
Could anyone help?
Thank you !!
#Runwith is declared in the TestRunner class of a Cucumber project. The Cucumber project has 3 defined types of classes:
Step Definition class
Feature class
Runner class
Please find the below examples for the above classes:
1. Feature Class
The test cases are written in this class
Feature: Title of your feature
I want to use this template for my feature file
#tag1
Scenario: Verify login to the system.
Given I navigate to url
And I enter username as "username"
And I enter password as "password"
When I click Login
Then I should be logged into the system
2. Step Definition class
The feature steps are defined in this class
public class LoginPage(){
#Given("I navigate to the url")
public void navigate() {
/* your code for the above
step comes here*/
}
}
3. Runner Class
The runner class consist of the location of features and step definition. It is a Junit class and cannot contain cucumber step definition annotations. (This is the reason why runner class cannot be a step definition class). But, you can include the BeforeClass, AfterClass (Junit annotations) in this class
import org.junit.runner.RunWith;
import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;
#RunWith(Cucumber.class)
#CucumberOptions(
features = {"classpath:<location of your folder with feature classes / package name>"},
glue = {"<package name>" },
tags = { "<the tags that has to be executed>","can be comma separated multiple tags" }
)
public class testrunner {
//#AfterClass
public static void quitDriver() {
driver.quit();
}
}
Hope this helps you !

Cucumber Runner Class not running Step Definitons

I am writing automated tests with Cucumber and Java.
I can run the step definitions by right-clicking on my Feature file, and running it as a Cucumber Feature, and all steps pass successfully.
But, I need to run them using a Runner Class.
My feature file is in this folder:
src/test/java/features
And my step definitons are in this folder:
src\test\java\com\abc\commercial\def\automation
My Runner Class is also stored in
src\test\java\com\abc\commercial\def\automation
And here is the Runner Class code:
#RunWith(Cucumber.class)
#CucumberOptions(
plugin = {"progress",
"html:build/report/html",
"junit:build/report/junit/cucumber-report.xml",
"json:build/report/json/cucumber-report.json"
},
glue = {"src\\test\\java\\com\\abc\\commercial\\def\\automation"},
features = {"src/test/java/features"}
)
public class QARunner {
public static void main(String[] args) {
// TODO Auto-generated method stub
}
}
When I run the Runner Class as a JUnit Test, I receive the following response in the console:
UUUUUUUUU
Undefined scenarios:
src/test/java/features/US46052
src/test/java/features/postFeatures.feature:43 # As the
2 Scenarios (2 undefined)
9 Steps (9 undefined)
0m0.303s
You can implement missing steps with the snippets below:
#Given("the Application...")
public void the_Application...() {
So the step definitions are not being picked up.
Does it have something to do with where my test runner is located?
I don't think it is picking up the step definitons in the automation folder
Thanks for any help
Try this : No need for main method. glue option should be in package style.
#RunWith(Cucumber.class)
#CucumberOptions(
plugin = {"progress",
"html:build/report/html",
"junit:build/report/junit/cucumber-report.xml",
"json:build/report/json/cucumber-report.json"
},
glue = {"com.abc.commercial.def.automation"},
features = {"src/test/java/features"}
)
public class QARunner {
}

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?

How to link feature and step definition in cucumber

I'm new to Cucumber java and had this problem in initial stages:
I'm not using MAVEN project for some reason. I just created a simple java project in eclipse.
I have my features under "src/dummy/pkg/features", and my implementation "StepDef.java" is under "src/dummy/pkg/features/implementation"
I have written step definitions for Given, When, and Then, but when I run my features file, it is unable to recognize the implementation. How do I link the features with step definitions?
create a class YourClass and it would look something like the below and run it as JUnit test.
#RunWith(Cucumber.class)
#CucumberOptions( monochrome = true,
features = "src/dummy/pkg/features/",
format = { "pretty","html: cucumber-html-reports",
"json: cucumber-html-reports/cucumber.json" },
glue = "your_step_definition_location_package" )
public class YourClass {
//Run this from Maven or as JUnit
}
When you run your Runner class then it will scan all the feature file mentioned within features options and load the them afterward all step definition within package started by text mentioned within glue options will get loaded.
For e.g.
#RunWith(Cucumber.class)
#CucumberOptions(
plugin = { "pretty","json:target/cucumberreports.json" },
glue = "stepDefinition",
features = "src/test/resources/TestCases/",
tags={"#onlythis"},
dryRun=false
)
public class RunTest {
}
Here all the feature file present within
src/test/resources/TestCases/
will get loaded
then all the stepdef within or it's subdirectory will get loaded
stepDefinition
and whenever your step from feature get run then cucumber will look for function corresponding to step's regex and function will run.
for e.g.
whenever step When User enters email id in src/test/resources/TestCases/Login.feature will run then cucumber will find its corresponding function in all stepdef classes
Login.feature
#LoginValidation
Feature: To smoke test functionalities of app
#Browser #ValidLogin
Scenario: Verify scenario in case of successful login
When User enters email id
And User enters password
Then User clicks on sign in button and able to sign in
And moment it will reach class in subdirectory of stepDefinition i.e. in
stepDefinition.ui.home.LoginPageStepDef.java cucumber will find function with #When("^User enters email id$") and will execute this function.
LoginPageStepDef.java
public class LoginPageStepDef {
LoginPage loginPage = new LoginPage(AttachHooks.driver);
private static Logger LOGGER = Logger.getLogger(LoginPageStepDef.class);
#When("^User enters email id$")
public void user_enters_email_id() throws Throwable {
//LoginPage.obj = loginPage;
loginPage.enterEmailId(ConfigManager.getProperty("UserName"));
}
}
You have to convert your project to Cucumber project. Right-click on your project from the Project Explorer > Configure > Convert as Cucumber Project.
Create a runner class something like this and you should be able to execute.
There is also no need to write step definitions manually, just create a feature file and run it, it will create a snippet of the step definition which can be used to create a step definition class:
A class file called Runnerclass is required to run the cucumber:
#RunWith(Cucumber.class)
#CucumberOptions(plugin={"pretty","html:format"},
features = "Features/name.feature",glue={"path where step definitions exist"})
public class RunnerClass {
}

Categories