1
 Feature: Login to Website

  #Login in Chrome
  Scenario: Login using Chrome
  Given I open Chrome
  When I browse to Website
  Then I login to Website using "user1" and "password1"

Global Base Class

public class base {

public WebDriver driver;
public Properties prop;
public ChromeOptions coptions;

public WebDriver initializeDriver() throws IOException
{

    String browserName= "chrome";
    System.out.println(browserName);
    String pathToDriver = "";

    if(browserName.equals("chrome"))
    {
        pathToDriver = "C:\\Repositories\\automationProject\\webDrivers\\chromedriver.exe";
        System.setProperty("webdriver.chrome.driver", pathToDriver);
        coptions.addArguments("disable-infobars");
        coptions.addArguments("--start-maximized");
        driver= new ChromeDriver(coptions);
        //execute in chrome driver

    }

    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    return driver;
}

Login

public class login extends base {

@Given("^I open Chrome$")
public void iOpenChrome() throws Throwable {
    driver = initializeDriver();
}

@When("^I browse to Website$")
public void iBrowseToWebsite() {
    driver.get("https://www.website.com/");
}

@Then("^I login to Website using \"([^\"]*)\" and \"([^\"]*)\"£\"$")
public void iLoginToWebsiteUsingAnd£(String username, String password) throws Throwable {
    driver.findElement(By.id("UserName")).sendKeys(username);
    driver.findElement(By.id("Password")).sendKeys(password);
    driver.findElement(By.id("btnLogin")).click();
}

}

The issue is that when running this feature I am getting the below error which I cannot understand why this is happening as it gives me no help in where the error is.

Undefined step: Given I open Chrome

Undefined step: When I browse to Website

Undefined step: Then I login to Website using "user1" and "password1"

user3430861
  • 172
  • 1
  • 3
  • 14

3 Answers3

0

Your function name for the steps is wrong. You are following camelCase approach that is not valid in the case of cucumber. You have write your @Given("^I open Chrome$") step function name as i_open_chrome()instead of iOpenChrome(). You can use Tidy Gherkin chrome extension to generate step definitions.

Valid sample steps.

@Given("^I open Chrome$")
public void i_open_chrome() throws Throwable {
    throw new PendingException();
}

@When("^I browse to Website$")
public void i_browse_to_website() throws Throwable {
    throw new PendingException();
}

@Then("^I login to Website using \"([^\"]*)\" and \"([^\"]*)\"$")
public void i_login_to_website_using_something_and_something(String strArg1, String strArg2) throws Throwable {
    throw new PendingException();
}
Dilip Meghwal
  • 632
  • 6
  • 15
0

You should convert the cucumber feature file to the Stepdefenition file by right click don't type the line of code

Justin Lambert
  • 940
  • 1
  • 7
  • 13
0

Probably your "glue" is not aligned correctly with your test runner or configuration.

Try editing the "glue" option in "Run > Edit Configuration" with the path to your step definitions.

Or, if you are using a test runner class, make sure you get this:

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

@RunWith(Cucumber.class)
@CucumberOptions(
        features = "path\\to\\features",
        glue = "stepdefs"
)
public class RunTest {

}

Where stepdefs is a package name with your step definition file.

Mate Mrše
  • 7,997
  • 10
  • 40
  • 77