Search code examples
javaseleniumcucumbertestngbdd

Snippet from Cucumber Java TestNG looks strange


I am trying Cucumber for my Selenium Java + TestNG project. Once I run the .feature file with empty stepdefinition, I should get a snippet that I can just copy paste to my step definition class. But I got a strange format that can't be used out of the box.

Given should be @Given , right? There is no public void() .

What could be the cause? Thank You.

2 Scenarios (2 undefined)
5 Steps (5 undefined)
0m0.000s
You can implement missing steps with the snippets below:
Given("^User is on Home Page$", () -> {
    // Write code here that turns the phrase above into concrete actions
    throw new PendingException();
});
When("^User enters UserName and Password$", () -> {
    // Write code here that turns the phrase above into concrete actions
    throw new PendingException();
});
Then("^He can visit the practice page$", () -> {
    // Write code here that turns the phrase above into concrete actions
    throw new PendingException();
});
When("^User LogOut from the Application$", () -> {
    // Write code here that turns the phrase above into concrete actions
    throw new PendingException();
});
Then("^he cannot visit practice page$", () -> {
    // Write code here that turns the phrase above into concrete actions
    throw new PendingException();
});

This is my glue file

package glue;
import java.io.IOException;
import org.testng.annotations.Test;
import cucumber.api.CucumberOptions;
import cucumber.api.testng.AbstractTestNGCucumberTests;
import cucumber.api.testng.TestNGCucumberRunner;
@Test(groups = "cucumber")
@CucumberOptions(features = "C:\\Users\\workspace\\cucumber\\src\\test\\java\\feature\\logintest.feature", 
glue = "C:\\Users\\workspace\\cucumber\\src\\test\\java\\glue\\Glue.java", 
plugin = {"html:C:\\Users\\workspace\\cucumber\\logs"})
 
public class Glue extends AbstractTestNGCucumberTests {
    @Test(groups = "cucumber", description = "Runs Cucumber Features")
    public void runCukes() throws IOException {
        
    }
}

This is my .feature file

Feature: Login
Scenario: Successful Login with Valid Credentials
Given User is on Home Page
When User enters UserName and Password
Then He can visit the practice page
Scenario: Successful LogOut
When User LogOut from the Application
Then he cannot visit practice page

This is my dependencies

<!-- https://mvnrepository.com/artifact/info.cukes/cucumber-testng -->
<dependency>
    <groupId>info.cukes</groupId>
    <artifactId>cucumber-testng</artifactId>
    <version>1.2.5</version>
</dependency>
 
 <!-- https://mvnrepository.com/artifact/info.cukes/gherkin -->
<dependency>
    <groupId>info.cukes</groupId>
    <artifactId>gherkin</artifactId>
    <version>2.12.2</version>
</dependency>

<dependency>
    <groupId>info.cukes</groupId>
    <artifactId>cucumber-java8</artifactId>
    <version>1.2.5</version>
    <scope>test</scope>
</dependency>


Solution

  • You are using cucumber-java8 dependency, for that below is the format for stepDefintion file(make sure your java compiler version should be 1.8).

    @Given Annotation will be in cucumber-java

    Given Annotation will be in cucumber-java8

    import cucumber.api.java8.En;
    
    public class stepDefinitions implements En{
    
    public stepDefinitions(){
        Given("^User is on Home Page$", () -> {
            // Write code here that turns the phrase above into concrete actions
            System.out.println("Test1");
        });
    
        When("^User enters UserName and Password$", () -> {
            // Write code here that turns the phrase above into concrete actions
            System.out.println("Test2");
        });
    
        Then("^He can visit the practice page$", () -> {
            // Write code here that turns the phrase above into concrete actions
            System.out.println("Test3");
        });
    
        When("^User LogOut from the Application$", () -> {
            // Write code here that turns the phrase above into concrete actions
            System.out.println("Test4");
        });
    
        Then("^he cannot visit practice page$", () -> {
            // Write code here that turns the phrase above into concrete actions
            System.out.println("Test5");
        });
       }
     }
    

    I executed it in my machine its working, try it and let me know if it works for you.

    Link for reference: http://codoid.com/cucumber-lambda-expressions/