Java Cucumber on IntelliJ:我一开始做新项目就会出错

Java Cucumber on IntelliJ:我一开始做新项目就会出错,java,intellij-idea,cucumber,Java,Intellij Idea,Cucumber,我已经被困了好几个小时,我有点困惑,因为我已经尝试按照一些教程来设置Cucumber(Java版本)+Selenium,使用IntelliJ作为IDE,但我总是从一开始就出现错误,所以我猜要么是教程没有提到,要么是IDE上存在一些错误配置 这是我尝试过的: 首先,在IntelliJ IDEA中创建了一个Maven项目,并将cucumber、junit和selenium的依赖项添加到my pom.xml中: <!-- https://mvnrepository.com/artifact/in

我已经被困了好几个小时,我有点困惑,因为我已经尝试按照一些教程来设置Cucumber(Java版本)+Selenium,使用IntelliJ作为IDE,但我总是从一开始就出现错误,所以我猜要么是教程没有提到,要么是IDE上存在一些错误配置

这是我尝试过的:

首先,在IntelliJ IDEA中创建了一个Maven项目,并将cucumber、junit和selenium的依赖项添加到my pom.xml中:

<!-- https://mvnrepository.com/artifact/info.cukes/cucumber-java -->
    <dependency>
        <groupId>info.cukes</groupId>
        <artifactId>cucumber-java</artifactId>
        <version>1.2.5</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>3.8.1</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/junit/junit -->
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
        <scope>test</scope>
    </dependency>

    <!-- https://mvnrepository.com/artifact/info.cukes/cucumber-junit -->
    <dependency>
        <groupId>info.cukes</groupId>
        <artifactId>cucumber-junit</artifactId>
        <version>1.2.5</version>
        <scope>test</scope>
    </dependency>
然后,从我的功能文件中,我使用IDE功能(alt+enter>create step definitions)自动生成了步骤定义,我得到了一个新文件:MyStepDefs.java,我将它放在src/test/java/step_definitions中(只保留在src/test/java中没有区别),内容如下:

package step_definitions;
import cucumber.api.PendingException;

public class MyStepdefs {
    public MyStepdefs() {
        Given("^I open google$", () -> {
            // Write code here that turns the phrase above into concrete actions
            throw new PendingException();
        });}
}
问题是,这已经显示出错误。无法识别“给定”关键字:无法解析方法“给定(java.lang.String)”

在“newPendingException()”中,我得到:不兼容的类型。必需:java.lang.Throwable。找到:cumber.api.PendingException

这听起来有点可疑,因为它是自动生成的代码,所以我假设它应该是无错误的(但事实并非如此)

所以我试着用教程中的内容替换自动生成的代码,但在@Before、@After、@Given、@When、@then关键字上出现了“不适用于方法”错误

package step_definitions;

import org.junit.Assert;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.By;
import cucumber.api.java.After;
import cucumber.api.java.Before;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;

public class googleCalcStepDefinition {

    protected WebDriver driver;

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

    @Given("^I open google$")
    public void I_open_google() {
        //Set implicit wait of 10 seconds and launch google
        driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
        driver.get("https://www.google.co.in");
    }

    @When("^I enter \"([^\"]*)\" in search textbox$")
    public void I_enter_in_search_textbox(String additionTerms) {
        //Write term in google textbox
        WebElement googleTextBox = driver.findElement(By.id("gbqfq"));
        googleTextBox.sendKeys(additionTerms);

        //Click on searchButton
        WebElement searchButton = driver.findElement(By.id("gbqfb"));
        searchButton.click();
    }

    @Then("^I should get result as \"([^\"]*)\"$")
    public void I_should_get_correct_result(String expectedResult) {
        //Get result from calculator
        WebElement calculatorTextBox = driver.findElement(By.id("cwos"));
        String result = calculatorTextBox.getText();

        //Verify that result of 2+2 is 4
        Assert.assertEquals(result, expectedResult);

        driver.close();
    }

    @After
    public void closeBrowser() {
        driver.quit();
    }
}

我错过了什么?我有没有办法在IntellijIDE上建立一个使用Cucumber(Java)+Selenium的新项目?还是根本不可能


谢谢

显然,我最近下载的Java JDK 9就是罪魁祸首。我回到第1步,用JDK 8开始了项目,现在一切正常。

我已经从IntelliJ IDEA 2017.3.2和中的链接打开了项目。查看插件是否已启用。您是对的。Cucumber还不支持Java9。
package step_definitions;

import org.junit.Assert;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.By;
import cucumber.api.java.After;
import cucumber.api.java.Before;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;

public class googleCalcStepDefinition {

    protected WebDriver driver;

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

    @Given("^I open google$")
    public void I_open_google() {
        //Set implicit wait of 10 seconds and launch google
        driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
        driver.get("https://www.google.co.in");
    }

    @When("^I enter \"([^\"]*)\" in search textbox$")
    public void I_enter_in_search_textbox(String additionTerms) {
        //Write term in google textbox
        WebElement googleTextBox = driver.findElement(By.id("gbqfq"));
        googleTextBox.sendKeys(additionTerms);

        //Click on searchButton
        WebElement searchButton = driver.findElement(By.id("gbqfb"));
        searchButton.click();
    }

    @Then("^I should get result as \"([^\"]*)\"$")
    public void I_should_get_correct_result(String expectedResult) {
        //Get result from calculator
        WebElement calculatorTextBox = driver.findElement(By.id("cwos"));
        String result = calculatorTextBox.getText();

        //Verify that result of 2+2 is 4
        Assert.assertEquals(result, expectedResult);

        driver.close();
    }

    @After
    public void closeBrowser() {
        driver.quit();
    }
}