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 {}
Related
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.
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.
My requirement is to run Cucumber test cases using Spring boot to run through a Custom java main class.
I am able to run Cucumber test suite fine if i am using following config class:-
#RunWith(Cucumber.class)
#CucumberOptions(
plugin = {
"html:target/cucumber-html-report",
"json:target/cucumber.json", "pretty:target/cucumber-
pretty.txt",
"usage:target/cucumber-usage.json", "junit:target/cucumber-
results.xml" },
features = { "src/test/resources" },
tags = {"#passed"},
glue = "cucumberTest.steps")
public class RunGwMLCompareTests {
}
And following class to load
#RunWith(SpringRunner.class)
#ActiveProfiles("dev")
#SpringBootTest(classes = Application.class)
public class AbstractDefinitions {
public AbstractDefinitions() {
}
}
And when i run RunGwMLCompareTests class,Now using this config my Cucumber test cases are running , It loads my Spring boot context and then exceutes all cases defined in feature.
Now my issue is that i want to run this from seperate main java class. I have created my java class as follows :-
package cucumberTest;
import cucumber.runtime.ClassFinder;
import cucumber.runtime.RuntimeOptions;
import cucumber.runtime.io.MultiLoader;
import cucumber.runtime.io.ResourceLoader;
import cucumber.runtime.io.ResourceLoaderClassFinder;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class CucumberMainTest {
public static void main(String[] args) throws IOException {
ClassLoader classLoader = CucumberMainTest.class.getClassLoader();
ResourceLoader resourceLoader = new MultiLoader(classLoader);
ClassFinder classFinder = new
ResourceLoaderClassFinder(resourceLoader, classLoader);
List<String> pluginList = new ArrayList<>();
pluginList.add("--plugin");
pluginList.add("html:target/cucumber-html-report");
pluginList.add("--plugin");
pluginList.add("json:target/cucumber.json");
RuntimeOptions ro = new RuntimeOptions(pluginList);
ro.getFilters().add("#passed");
ro.getFeaturePaths().add("src/test/resources");
ro.getGlue().add("cucumberTest/steps");
cucumber.runtime.Runtime runtime = new
cucumber.runtime.Runtime(resourceLoader, classFinder, classLoader,
ro);
runtime.run();
}
}
It executes my test cases but does not load my SpringContext, as a result no spring beans are loadedand i get null pointer exception.. Any help is geatly appreciated.
Regards,
Vikram Pathania
One solution i found was to run RunGwMLCompareTests class from outside which handles my Spring context automatically.
So basically i am using gradle to create batch file which does nothing but automatically creates a batch with all relative depencies in my class path and then run following command:
"%JAVA_EXE%" -classpath "%CLASSPATH%" org.junit.runner.JUnitCore
cucumberTest.runners.RunGwMLCompareTests
where,
JAVA_EXE=C:\Program Files\Java\jdk1.7.0_65/bin/java.exe
And CLASSPATH is lib folder where all jars are present.
CLASSPATH=%APP_HOME%\lib\*.jar
Now running this batch i am using
features = { "src/test/resources" } <tag>
to put my feature files outside so that it can edited on the fly.
I hope this helps somebody out there.
by doing this , i am able to run RunGwMLCompareTests class.
Now using this config my Cucumber test cases are running and It also loads my Spring boot context and then exceutes all cases defined in feature.
Regards,
Vikram Pathania
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"
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