Unable to run a cucumber feature within IntelliJ - java

I have a feature with a number of steps but the first one is the only working and the rest are working fine. Now when I add new steps to the same feature there not working either its very confusing. Its lis like half the feature is working but the other half is no working.
Please see test runner below:
import io.cucumber.junit.Cucumber;
import io.cucumber.junit.CucumberOptions;
import org.junit.runner.RunWith;
#RunWith(Cucumber.class)
#CucumberOptions(features = "src/test/java/features", glue = {"stepDefinitions"})
public class TestRunner {
}
Folder structure:
Error message:
You can implement this step using the snippet(s) below:
#Given("Add Place Payload with {string} {string} {string}")
public void add_place_payload_with(String string, String string2, String string3) {
// Write code here that turns the phrase above into concrete actions
throw new io.cucumber.java.PendingException();
}
I am also getting a red exclamation mark on the feature:

Related

Passing parameters in cucumber steps

I'm trying to pass one parameter in cucumber step but the method have two
This is my code
#Given("^skip the next scenario named \"(.*)\"$")
#BeforeStep
public void before(Scenario scenario, String name) {
if (scenario.getName().trim().equalsIgnoreCase(name)) {
Assume.assumeTrue(false);
}
}
Is there any idea to pass only the name ?
Cucumber-JUnit allows you to filter scenarios based on tags and name.
The name takes a regex. So given a feature with two scenarios where we want to skip the Addition scenario:
Feature: Basic Arithmetic
Background: A Calculator
Given a calculator I just turned on
Scenario: Addition
When I add 4 and 5
Then the result is 9
Scenario: Another Addition
When I add 4 and 7
Then the result is 11
We have to write a regex that excludes Addition but includes Another Addition. One way to do this is to use a negative look-ahead regex:
package io.cucumber.examples.calculator;
import io.cucumber.junit.Cucumber;
import io.cucumber.junit.CucumberOptions;
import org.junit.runner.RunWith;
#RunWith(Cucumber.class)
#CucumberOptions(name = "^(?!Addition).*$")
public class RunCucumberTest {
}

NullPointerException when using multiple stepdefinitions in Cucumber-jvm

I am currently building a framework to test a Rest-API endpoint. As I am planning to write a lot of test cases, I decided to organize the project to allow me to reuse common Step Definition methods.
The structure is as follows;
FunctionalTest
com.example.steps
-- AbstractEndpointSteps.java
-- SimpleSearchSteps.java
com.example.stepdefinitions
-- CommonStepDefinition.java
-- SimpleSearchStepDefinition.java`
However when I try to call SimpleSearchSteps.java methods I get a NullPointerException
CommonStepDefinition Code
package com.example.functionaltest.features.stepdefinitions;
import net.thucydides.core.annotations.Steps;
import com.example.functionaltest.steps.AbstractEndpointSteps;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;
public class CommonStepDefinition {
#Steps
private AbstractEndpointSteps endpointSteps;
#Given("^a base uri \"([^\"]*)\" and base path \"([^\"]*)\"$")
public void aBaseUriAndBasePath(String baseURI, String basePath) {
endpointSteps.givenBasepath(baseURI, basePath);
}
#When("^country is \"([^\"]*)\"$")
public void countryIs(String country)
{
endpointSteps.whenCountry(country);
}
#Then("^the status code is (\\d+)$")
public void theStatusCodeIs(int statusCode) {
endpointSteps.executeRequest();
endpointSteps.thenTheStatusCodeIs200(statusCode);
}
}
SimpleSearchStepDefinition.java
package com.example.functionaltest.features.stepdefinitions;
import net.thucydides.core.annotations.Steps;
import com.example.functionaltest.steps.EndpointSteps;
import cucumber.api.java.en.When;
public class SimpleSearchStepDefinition {
#Steps
private SimpleSearchSteps searchSteps;
#When("^what is \"([^\"]*)\"$")
public void whatIs(String what) {
searchSteps.whenWhatIsGiven(what);
}
}
Looks like you are missing holder class for Cucumber annotation, something like this you should have so that cucumber knows and identified that steps and features of yours:
#RunWith(Cucumber.class)
#CucumberOptions(
glue = {"com.example.functionaltest.features.steps"},
features = {"classpath:functionaltest/features"}
)
public class FunctionalTest {
}
Note that, in your src/test/resources you should have functionaltest/features folder with your .feature files according to this sample, you can ofc, change it by your design
Can you take a look at Karate it is exactly what you are trying to build ! Since you are used to Cucumber, here are a few things that Karate provides as enhancements (being based on Cucumber-JVM)
built-in step-definitions, no need to write Java code
re-use *.feature files and call them from other scripts
dynamic data-driven testing
parallel-execution of tests
ability to run some routines only once per feature
Disclaimer: I am the dev.
I solved this issue by using a static instance of RequestSpecBuilder in the AbstractEndpointSteps instead of RequestSpecification.
Therefore, I was able to avoid duplication of StepDefinitions and NPE issues altogether

Cucumber test not running

I am working on my first feature file/selenium project.
I have created a feature file and runner class.
package cucumberpkg2;
import org.junit.runner.RunWith;
import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;
#RunWith(Cucumber.class)
#CucumberOptions
(features="Features")
public class Runner {
}
I have the feature file test.feature
Feature: Login screen
Scenario Outline: Successful login
Given User is on Login page
When User enters valid UserName and Password
And Clicks the login button
Then User landed on the home page
But whenever I try to run the TestRunner class as a JUnit test, I get the error:
Test class not found in selected project.
Here is the solution to your Question:
As per the current documentation of Cucumber you may require to change the keyword Scenario Outline to Scenario in test.feature file.
Though you mentioned about TestRunner class but your code speaks about Runner class being implemented as public class Runner, be sure which class file are you executing as JUnit test.
You may require to change #CucumberOptions to #Cucumber.Options for previous versions (recent versions work with #CucumberOptions)
Put the following 2 parts in a single line as #Cucumber.Options
(features="Features")
As you would be mentioning #Cucumber.Options(features="Features") ensure that your feature file is placed inside Features sub-directory within the Project Directory.
So you will be having, test.feature file within Features sub-directory with the following code:
Feature: Login screen
Scenario: Successful login
Given User is on Login page
When User enters valid UserName and Password
And Clicks the login button
Then User landed on the home page
Your Runner class will look like:
import org.junit.runner.RunWith;
import cucumber.api.junit.Cucumber;
#RunWith(Cucumber.class)
#CucumberOptions(features="Features")
public class Runner {
}
Finally when you will execute Runner class as a JUnit test you will see the following message on your console:
You can implement missing steps with the snippets below:
#Given("^User is on Login page$")
public void User_is_on_Login_page() throws Throwable {
// Express the Regexp above with the code you wish you had
throw new PendingException();
}
#When("^User enters valid UserName and Password$")
public void User_enters_valid_UserName_and_Password() throws Throwable {
// Express the Regexp above with the code you wish you had
throw new PendingException();
}
#When("^Clicks the login button$")
public void Clicks_the_login_button() throws Throwable {
// Express the Regexp above with the code you wish you had
throw new PendingException();
}
#Then("^User landed on the home page$")
public void User_landed_on_the_home_page() throws Throwable {
// Express the Regexp above with the code you wish you had
throw new PendingException();
}
These warnings can be taken care easily by implementing the glue options to Cucumber.
Let me know if this Answers your Question.
you need to provide full path of the feature file as mentioned below.
#RunWith(Cucumber.class)
#CucumberOptions(
features = {"src/test/resources/com/gaurang/steps/demo.feature",
"src/test/resources/com/gaurang/steps/demo1.feature"
}
)
public class RunAllTest {
}
or if you have too many feature files the best way is to provide tags to feature file and then use those tags to run as mentioned below .
#userRegistrations
Feature: User Registration
RunAllTest.java
#RunWith(Cucumber.class)
#CucumberOptions(tags={"#userRegistrations"})
public class RunAllTest {
}
And you can use multiple tags

Main class run with exception in thread error with JUnit

I'm using the crimson editor along with the command prompt console for compiling and running the programs. I've just installed and am new to JUnit. Currently, I'm following a basic tutorial from TutorialsPoint.com and have followed the steps setting the classpath.
The link to the tutorial is available here:
http://www.tutorialspoint.com/junit/junit_environment_setup.htm
From this tutorial, there's an ending part whereby you will asked to create class files to test the JUnit. Eventually, after compiling, I tried to run the main class but I was presented with a long series of errors so I was hoping you guys can help me out here.
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class TestJunit {
#Test
public void testAdd() {
String str= "Junit is working fine";
assertEquals("Junit is working fine",str);
}
}
Main class:
import org.junit.runner.JUnitCore;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;
public class TestRunner {
public static void main(String[] args) {
Result result = JUnitCore.runClasses(TestJunit.class);
for (Failure failure : result.getFailures()) {
System.out.println(failure.toString());
}
System.out.println(result.wasSuccessful());
}
}
And the screenshot of the console displaying the error:
Try to add hamcrest-core-1.3.jar to your classpath.
Download the jar from Hamcrest site: https://code.google.com/p/hamcrest/
Assert class of JUnit need Hamcrest matchers.
The answer to my problem with the help of Mark A.Fitzgerald and pz74 is that my CLASSPATH value was setted wrongly. I have amended it to C:\JUnit\junit-4.12.jar;C:\HAMCREST\hamcrest-core-1.3.jar; and hamcrest had to be installed as well.

BDD Cucumber tests via jUnit

I there any examples for runing cucumbers via jUnit manually?
I have a simple empty class with #RunWith(Cucumber.class) which has all my feature files.
import cucumber.junit.Cucumber;
import org.junit.runner.RunWith;
#RunWith(Cucumber.class)
public class RunCukesTest {
}
And simple junit test running it:
#Test
public void cucumberFirstPartTests()throws Exception{
Cucumber cucumber = new Cucumber(RunCukesFirstPart.class);
RunNotifier notifier = new RunNotifier();
cucumber.run(notifier);
}
Is there any examples at all for filtering manually tests, using runner scheduler and descriptions of tests for cucumbers? Watched documentation, but for me it is not enough. I will appreciate any links. Thank you.
You can mark each Scenario/Feature with any number of tags, using #TAGNAME
Given that, you can tell the runner to run a selected set of tags
#RunWith(Cucumber.class)
#Cucumber.Options(tags = {"#TAGNAME"})
Is that what you were after?

Categories

Resources