简体   繁体   中英

Cucumber Java program does not pick up step definition

I'm new with cucumber and learned this doc carefully to understand how could I implement my first cucumber java project. I have done a lot of analysis and gone through almost all the articles related to it over internet, why its not picking up step definition but could not find the cause. However, everything seems to be OK as per my understanding, I have great expectation that you guys can find my fault at one go.

Looking forward for a +ve response.

Hence I'm sharing the code, message(on console window) and folder structure.

Thanks Rafi

Feature file:

@MyApplication
Feature: Post text Hello Rafi on Rafi facebook account
Scenario: Login successfully on Facebook application
Given Open Facebook application                   
When Enter valid id and password
And Click on Login button                         
Then Facebook home page should open   

TestRunner class:

package test.java.runner;
import org.junit.runner.RunWith;
import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;

    //glue = {"helpers", "src.test.java.steps"},
    @RunWith(Cucumber.class)
    @CucumberOptions( 
            features = {"src/features"},        
            glue = {"helpers", "src.test.java.steps"},
            plugin = {"pretty","html:target/cucumber-html-report"},
            dryRun = true,
            monochrome = true,
            tags="@MyApplication",
            strict=false)


    public class TestRunner {

    }

StepDefinition class:

package test.java.steps;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.WebDriverWait;

import cucumber.api.PendingException;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;

public class StepDefinition {

    private static WebDriver driver = null ;
    private static String password = "*********";
    WebDriverWait wait=new WebDriverWait(driver, 30);
    WebElement waitElement;

    @BeforeClass
    public void setup() throws Throwable
    {
        System.setProperty("webdriver.gecko.driver", "C:\\Selenium Automation\\selenium\\Selenium 3 and Firefox Geckodriver\\geckodriver.exe");
        driver = new FirefoxDriver ();
        driver.manage().window().maximize();
    }

    @AfterClass
    public void teardown() throws Throwable
    {driver.quit();}

    // First scenario
    @Given("^Open Facebook application$")
    public void open_Facebook_application() throws Throwable{
        System.out.println("this is not working");
        driver.navigate().to("https://www.facebook.com/");


    }

    @When("^Enter valid id and password$")
    public void enter_valid_id_and_password() throws Throwable{
        driver.findElement(By.name("email")).clear();
        driver.findElement(By.name("email")).sendKeys("rafiras16@gmail.com");
        driver.findElement(By.name("pass")).clear();
        driver.findElement(By.name("pass")).sendKeys(password);

    }

    @When("^Click on Login button$")
    public void click_on_Login_button() throws Throwable {
        driver.findElement(By.xpath("//input[starts-with(@id,'u_0')]")).click();

    }

    @Then("^Facebook home page should open$")
    public void facebook_home_page_should_open() throws Throwable{
        String strTitle = driver.getTitle();
        System.out.print(strTitle);


    }

}

image for message on console window and folder structure BuildPath details

I think the glue attribute on the CucumberOptions class is wrong. If you want to refer to the step definitions using the file path then you need to change that to:

glue = {"helpers", "src/test/java/steps"},

If you want to refer them by package then you need to remove the "src." prefix:

glue = {"helpers", "test.java.steps"},

Try changing,

glue = {"helpers", "src.test.java.steps"}

to

glue = {"test.java.steps"}

And what is this helpers package i don't see it in the screenshot.

The @AfterClass and @BeforeClass annotations are useful only if the class is a junit test class. You have placed them in a normal class, thus they will not be run. This leads to the driver remaining uninitaialized.

The easy way out is to use the cucumber @Before and @After annotation. Change the @BeforeClass to @Before and @AfterClass to @After .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM