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"
Related
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 {}
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.
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.
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
Hi I have setUp a Java project using Maven in eclipse.
I am facing an issue whenever I am trying to run the script. Its is executed by the not opening the desired website which I am parsing from the feature file.
Please have a look to the following code and Image of my directories setup in eclipse
Here is my code for PageStepsDefs.java
package com.workshop.airport.workshop.airport;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import cucumber.api.java.After;
import cucumber.api.java.Before;
import cucumber.api.java.en.Given;
public class PageStepsDefs {
public String ChromeDriverPath="C:\\Users\\zain.jamshaid\\Desktop\\chromedriver.exe";
public WebDriver driver;
String localhost="www.google.com";
#Before
public void deleteAllCookies() {
driver.manage().deleteAllCookies();
driver.manage().window().maximize();
}
#Before
public void setup(){
System.setProperty("webdriver.chrome.driver",ChromeDriverPath);
driver = new ChromeDriver();
}
#Given("^I browse to the (.+) page$")
public void open_page(String url)
{
driver.get(localhost+url);
System.out.println(localhost+url);
}
#After
public void tearDown(){
driver.quit();
}
}
Here is my code for RunCukeTest.java
package com.workshop.airport.workshop.airport;
import cucumber.api.junit.*;
import org.junit.runner.RunWith;
#RunWith(Cucumber.class)
#Cucumber.Options(
tags={"#mysingle"},
format={"pretty", "html:target/cucumber-html-report"},
monochrome=true,
features={"."},
strict=true)
public class RunCukeTest {
}
Here is statements in feature file
Feature: Login Functionality
#mysingle
Scenario: user successfully logins to the application
Given I browse to the / page
Any help will be awesome.
Thanks In advance.
Zain
I think I know the problem. As per your comment, the '/' from feature file is getting parsed to your step correctly. So this is not a cucumber issue. The issue I think is with your url. The url you have is incorrectly formed. URL should start with http://
I think everything will work fine if you change your localhost variable to String localhost="http://www.google.com";
Is it really executing your feature file? Try putting test.feature under src/test/resources/com/workshop/airport/workshop/airport: the JUnit running uses the unit test package as the location for finding the feature files.