Home > Back-end >  How to solve error in BDD Parameterizing?
How to solve error in BDD Parameterizing?

Time:06-30

This is the TestRunner.java:

package StepDefinitions; import io.cucumber.junit.Cucumber; import io.cucumber.junit.CucumberOptions;

import org.junit.runner.RunWith;

@RunWith(Cucumber.class) @CucumberOptions(features="src/test/resources/Features", glue={"StepDefinitions"}, monochrome=true)

public class TestRunner {

}

This is the LoginDemoSteps:

package StepDefinitions;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

import io.cucumber.java.en.*;
public class LoginDemoSteps {
    WebDriver driver = null;
    @Given("browser is open")
    public void browser_is_open() {System.setProperty("webdriver.chrome.driver",System.getProperty("user.dir") "\\src\\test\\resources\\drivers\\chromedriver.exe");
    driver=new ChromeDriver();
    driver.manage().window().maximize();

}

@And("user is on login page")
public void user_is_on_login_page() {
    driver.get("https://example.testproject.io/web/");

}

@When("^User enters (.*) and (.*) $")
public void User_enters_username_and_password(String username, String password) throws InterruptedException {
    driver.findElement(By.id("name")).sendKeys(username);
    driver.findElement(By.id("password")).sendKeys(password);
    Thread.sleep(3000);

}

}

This is the feature file:

Feature: Test login functionality

  Scenario Outline: Check login is successful with valid credentials
    Given browser is open
    And user is on login page
    When User enters <username> and <password>


Examples: 
  | username | password |
  | Sai      |    12345 |
  | Ele      |    12345 |

When I run the script, browser is being launched, then site is opening, but credentials not getting entered in the login/password entry fields:

io.cucumber.junit.UndefinedStepException: The step 'User enters Sai and 12345' and 1 other step(s) are undefined.
You can implement these steps using the snippet(s) below:

@When("User enters Sai and {int}")
public void user_enters_sai_and(Integer int1) {
    // Write code here that turns the phrase above into concrete actions
    throw new io.cucumber.java.PendingException();
}

How to solve this BDD Cucumber error?

CodePudding user response:

can you please replace and try this when annotation

@When("^User enters (. ) and (. )$") public void user_enters_and(String username, String password) throws Throwable { //your code here }

CodePudding user response:

Try your parametrization like this:

When User enters "<username>" and "<password>"

And in your step definitions:

@When("User enters Sai and {string}")

Note that you can not pass Integers as parametrized values, only a char sequence can be passed in Selenium.

  • Related