黄瓜Java程序没有选择步骤定义

时间:2018-04-23 10:49:01

标签: java cucumber

我是黄瓜的新手,并仔细学习了这个文档,以了解我如何实现我的第一个黄瓜java项目。我已经做了很多分析,并且经历了几乎所有与互联网相关的文章,为什么它没有采取步骤定义但却找不到原因。但是,根据我的理解,一切似乎都没问题,我非常期待你们能够一次性找到我的错。

期待+ +响应。

因此,我正在共享代码,消息(在控制台窗口上)和文件夹结构。

由于 拉菲

功能文件:

@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类:

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类:

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

3 个答案:

答案 0 :(得分:0)

我认为CucumberOptions类的glue属性是错误的。如果要使用文件路径引用步骤定义,则需要将其更改为:

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

如果你想通过包引用它们,那么你需要删除" src。"前缀:

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

答案 1 :(得分:0)

尝试更改,

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

glue = {"test.java.steps"}

这个帮助程序包是什么,我没有在屏幕截图中看到它。

答案 2 :(得分:0)

仅当类是junit测试类时,@AfterClass@BeforeClass注释才有用。您已将它们放在普通类中,因此它们不会被运行。这导致驾驶员保持原状。

简单的方法是使用黄瓜@Before and @After注释。更改@BeforeClass to @Before and @AfterClass to @After

相关问题