This has been puzzling me for half a day now. I can't seem to find the issue. Basically I have my Test runner, feature file, and the steps file in my workspace. The java files are in the same package (i.e. no package).
Below is my TestRunner.java
import org.junit.Test;
import org.junit.runner.RunWith;
import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;
#RunWith(Cucumber.class)
#CucumberOptions(features = "test/resources/features", tags = { "~#Ignore" })
public class TestRunner {
#Test
public void feature() {
}
}
My feature file, helloWorld.feature
Feature: Simple Test Feature
Scenario: Run Scenario ONE
GIVEN step one
WHEN step two
THEN step three
and my steps file CucumberJava.java,
import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;
public class CucumberJava {
#Given("^step one$")
public void step_one() {
System.out.println("step one");
}
#When("step two")
public void step_two() {
System.out.println("step two");
}
#Then("^step three$")
public void step_three() {
System.out.println("step three");
}
}
When I execute TestRunner.java as JUnit, everything passes, but I get the following in the console:
0 Scenarios
0 Steps
0m0.000s
WHY? In fact, when I remove CucumberJava.java from the project, I get the exact same output. What am I missing?
I also tried setting the glue option in TestRunner.java code too; still the same result.
Your help is highly appreciated.
The feature file words like Given etc are in uppercase in your feature file. They need to be like Given ie sentence case.
Feature: Simple Test Feature
Scenario: Run Scenario ONE
Given step one
When step two
Then step three
Also you might need to append a 'src' to the feature path in the runner. Like this features = "src/test/resources/features", if you are using Maven. Also no need to have a #Test annotation and method inside the runner.
Related
Im trying to run a simple feature file with java and intellij and I cant seem to get it working.
Ive set up my Cukes test runner class
package config;
import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;
import org.junit.runner.RunWith;
#RunWith(Cucumber.class)
#CucumberOptions(monochrome = true,
features = {"src/test/resources/features/"},
dryRun = false,
glue = "com.ecs.googleuat"
)
public class RunCukesTest {
}
feature:
Feature: home
Scenario: home
Given I am on the home page
step definitions:
import cucumber.api.PendingException;
import cucumber.api.java.en.Given;
public class MyStepdefs {
#Given("^I am on the home page$")
public void iAmOnTheHomePage() {
System.out.println("Hello");
}
}
project structure:
Im using a maven project with the java cucumber plugins.
When I run the feature file I get the following error:
Undefined step: Given I am on the home page
1 Scenarios (1 undefined)
1 Steps (1 undefined)
0m0.000s
You can implement missing steps with the snippets below:
#Given("^I am on the home page$")
public void i_am_on_the_home_page() throws Throwable {
// Write code here that turns the phrase above into concrete actions
throw new PendingException();
}
Process finished with exit code 0
Please help me understand what Im doing wrong.
Thanks
Not 100% sure but I think the issue is with the method name. The easy solution just copy-paste the suggested step definition inside MyStepdefs and remove exception and add your println and then run. Also try removing the glue com.ecs.googleuat you added.
Please follow this for further help!
Though I would strictly recommend going ahead with java-8.
Anyways, If you are comfortable with Java-8. Remove the glue inRunCukesTest.java. And update your MyStepdefs.java
public class MyStepdefs implements En {
public Stepdefs() {
Given("^I am on the home page$", () -> {
System.out.println("Hello");
});
}
}
En automatically implements default Glue for you.
Also, use appropriate dependencies. Follow this for same.
Edit:
For Java 8 please make sure that if you are using IntelliJ Idea, then Project SDK is enabled to 1.8.
I am trying to build a Cucumber BDD framework with multiple step definition files. I am still trying to understand how to use picocontainer to run the step definition files. My problem is that once I have added the picocontainer jar into a project's build path, when executing the test runner it is unable to find any scenarios or steps.
Console
Java project build path
My project contains:
• A feature file
• 2 step definition files
• 1 test runner
• Utilities package with a webdriver initializer method
My feature file has the following steps:
The first 2 gherkin steps are glued to methods in following step definition class:
package stepDefinitions;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import cucumber.api.java.en.Given;
public class SD_HomePage {
WebDriver driver;
#Given ("^the user is on the websites homepages$")
public void user_is_on_the_websites_homepage() {
driver = utilities.WebDriverInitializer.openWebdriver("Chrome");
driver.get("https://www.forExample.com/");
}
#Given("^then clicks on AboutUs title$")
public void then_clicks_on_AboutUs_title() throws Throwable {
driver.findElement(By.xpath("//a[#href='/en/about-us'][1]")).click();
}
}
The third gherkin step is glued to this separate step def class:
package stepDefinitions;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import cucumber.api.java.en.When;
public class SD_AboutUsPage {
WebDriver driver;
#When("^the user clicks on Contact widget$")
public void the_user_clicks_on_Contact_widget() throws Throwable {
driver.findElement(By.xpath("//span[#class='icon-envelope listCta__img'][1]")).click();
}
}
When executing the test from the test runner no scenarios or steps will be found by the runner:
package testRunners;
import org.junit.runner.RunWith;
import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;
#RunWith(Cucumber.class)
#CucumberOptions(features = "Features", glue = "stepDefinitions")
public class TestRun_NewsletterForm {
}
Console results from the Testrunner
However, when I remove the picocontainer the scenario and steps will be found. This will leave me with the original problem of not being able to make use of a shared state Webdriver.
Test Run after picocontainer jar is removed
I am aware that in this project I have not yet set up a class that will contain the shared state Webdriver, and constructors on the step definition pages. I have another project that has that is affected by this same issue but I felt that it would make this issue more complicated if I used that example.
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.
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?
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"