簡體   English   中英

IntelliJ上的黃瓜:我設置新項目后即會出錯

[英]Cucumber on IntelliJ: getting errors as soon as I set up new project

我已經被困了幾個小時了,我有點困惑,因為我試圖遵循很多教程,以IntelliJ作為IDE來設置Cucumber(Java版)+ Selenium,但是我總是最終從剛開始的時候,所以我猜可能是本教程中沒有提到的內容或我的IDE上的某些配置錯誤。

這是我嘗試過的東西:

首先,在IntelliJ IDEA中創建一個Maven項目,並將對黃瓜,junit和硒的依賴關系添加到我的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>

然后,我為項目創建了一些結構:

  • src / test中名為“ resources”的文件夾。
  • src / test / resources中一個名為“ features”的文件夾。
  • src / test / java中名為“ step_definitions”的軟件包。
  • src / test / resources / features文件夾中名為MyTest.feature的文件

在MyTest.feature中,我添加了一個簡單的測試,如下所示:

Feature: Check addition in Google calculator

  Scenario: Addition
    Given I open google
    When I enter "2+2" in search textbox
    Then I should get the result as "4"

然后從我的功能文件中使用IDE功能自動生成步驟定義(alt + enter>創建步驟定義),然后得到一個新文件: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();
        });}
}

問題是,這已經顯示出錯誤。 無法識別“ Given”關鍵字: 無法解析方法“ Given(java.lang.String)”

在“ new PendingException()”上,我得到: 不兼容的類型。 必需:java.lang.Throwable。 找到:黃瓜.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();
    }
}

截圖

我想念什么? 有什么辦法可以在IntelliJ IDE上設置一個使用Cucumber(Java)+ Selenium的新項目? 還是根本不可能?

謝謝!

顯然,我最近下載的Java JDK 9是罪魁禍首。 我回到第1方塊,開始使用JDK 8進行項目,現在一切都按預期進行。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM