javamavencucumbergherkin

Cucumber java - maven- example table - one step undefined


I am an absolute beginner to coding of any sort, and I am currently doing a final project for a course I have been attending.

I am hard stuck on one of my steps, however. I get "step undefined" no matter what I do. Now internet points me towards glue in cucumber options however I am sure that I have specified the package and all my other steps work just fine, in case of bad glue all steps are undefined as I found out earlier.

So, in my case, this one singular step keeps being undefined, and I just cannot work it out.

Here are my step definitions

package Mystore;
import io.cucumber.datatable.DataTable;
import io.cucumber.datatable.UndefinedDataTableTypeException;
import io.cucumber.java.en.And;
import io.cucumber.java.AfterAll;
import io.cucumber.java.BeforeAll;
import io.cucumber.java.Before;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import io.cucumber.java.en.Given;
import io.cucumber.java.en.Then;
import io.cucumber.java.en.When;

import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

public class StepDefinition {

    private static WebDriver driver;

    @Before
    public void setup() {
        driver = new ChromeDriver();
    }

    //@AfterAll
    //public static void teardown() {
       // driver.quit();
    //}

    @Given("User is logged in")
    public void user_is_logged_in() {
        driver.get("https://mystore-testlab.coderslab.pl/index.php");

        WebElement element;
        element = driver.findElement(By.className("user-info"));
        driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        element.click();
        WebElement email = driver.findElement(By.id("field-email"));
        driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        email.click();
        email.sendKeys("gkqvuzdwzxoltwlnij@ckptr.com");
        driver.findElement(By.id("field-password")).sendKeys("qwerty1");
        WebElement sign = driver.findElement(By.id("submit-login"));
        sign.click();

    }
    @When("add address is clicked")
    public void add_address_is_clicked() {

        WebElement address;
        address = driver.findElement(By.xpath("//*[@id=\"footer_account_list\"]/li[4]/a"));
        address.click();
        WebElement clkaddress;
        clkaddress = driver.findElement(By.xpath("//*[@id=\"content\"]/div[2]/a"));
        driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        clkaddress.click();
    }
    @Then("fill in credentials with <alias>")
    public void fill_in_credentials_with(String alias){

        driver.findElement(By.className("col-md-6")).sendKeys(alias);
        //alias.sendKeys(Userdata.cell(1,1));
    }
    @And("submit and close the page")
        public void ubmit_and_close_the_page() {
            driver.quit();
    }
}

My feature

Feature: Create address on mystore

  Scenario Outline: Test filling in address
    Given User is logged in
    When add address is clicked
    Then fill in credentials with <alias>
    And submit and close the page

    Examples:
      | alias | address  | city     | zip    | country | phone     |
      | RWT   | Lubelska | Warszawa | 01-999 | Poland  | 498100234 |

My runner

package Mystore;

import org.junit.runner.RunWith;
import io.cucumber.junit.CucumberOptions;
import io.cucumber.junit.Cucumber;

//glue = {"helpers", "src.test.java.steps"},
@RunWith(Cucumber.class)
@CucumberOptions(
        features = "src/test/Resources/Mystore/Userdata.feature",
        glue = {"Mystore"},
        snippets = CucumberOptions.SnippetType.CAMELCASE,
        plugin = {"pretty"}
)
        //format = {"pretty"})

public final class RunCucumberTest {

}

my POM

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.zaliczenie</groupId>
    <artifactId>zaliczenie</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>20</maven.compiler.source>
        <maven.compiler.target>20</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>4.14.0</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/io.cucumber/cucumber-java -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13.2</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-support</artifactId>
            <version>4.13.0</version>
        </dependency>
        <dependency>
            <groupId>io.cucumber</groupId>
            <artifactId>gherkin</artifactId>
            <version>22.0.0</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>io.cucumber</groupId>
            <artifactId>cucumber-junit</artifactId>
            <version>7.0.0</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>io.cucumber</groupId>
            <artifactId>cucumber-java</artifactId>
            <version>7.0.0</version>
        </dependency>

    </dependencies>

</project>

Error

io.cucumber.junit.UndefinedStepException: The step 'fill in credentials with RWT' is undefined.
You can implement this step using the snippet(s) below:

@Then("fill in credentials with RWT")
public void fillInCredentialsWithRWT() {
    // Write code here that turns the phrase above into concrete actions
    throw new io.cucumber.java.PendingException();
}

I looked at glue already and it should be fine. I did make sure my dependencies aren't outdated or incompatible with one another. I tried several different methods.

File structure looks like this

enter image description here


Solution

  • In your scenario you have a step:

    Then fill in credentials with <alias>
    

    Because this is a step in a scenario outline the <alias> gets replaced with the value of your examples.

    Examples:
          | alias | address  | city     | zip    | country | phone     |
          | RWT   | Lubelska | Warszawa | 01-999 | Poland  | 498100234 |
    

    So your step is now

    Then fill in credentials with RWT
    

    This does not match your step definition. The fish brackets are treated as regular text here.

    @Then("fill in credentials with <alias>")
    

    Which is why Cucumber suggests:

    @Then("fill in credentials with RWT")
    

    You can change your step to

    Then fill in credentials with "<alias>"
    

    And your step definition to

    @Then("fill in credentials with {string}")
    

    Then both will match.

    Also by adding the quotes in the step, Cucumber will suggest parametrized step definitions.

    Btw, you do not need the gherkin dependency. It is a transitive dependency and is included automatically.