Java Cumber无法读取定义文件

Java Cumber无法读取定义文件,java,maven,selenium,junit,cucumber,Java,Maven,Selenium,Junit,Cucumber,我是Cucumber的初学者,我已经在maven项目中编写了登录和进入主页的基本测试。我怀疑POM.xml存在一些一致性问题 请找到下面的文件 我尝试了pom文件中依赖项的多种组合,但问题似乎仍然存在 1) 步骤定义文件 package StepDefinition; import org.junit.Assert; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.se

我是Cucumber的初学者,我已经在maven项目中编写了登录和进入主页的基本测试。我怀疑POM.xml存在一些一致性问题

请找到下面的文件 我尝试了pom文件中依赖项的多种组合,但问题似乎仍然存在

1) 步骤定义文件

package StepDefinition;

import org.junit.Assert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import io.cucumber.java.en.Given;
import io.cucumber.java.en.Then;
import io.cucumber.java.en.When;


public class loginStepDefinition {

    WebDriver driver;


@Given("^User is already on login page$")
  public void user_already_on_login_page(){
    System.setProperty("webdriver.chrome.driver","/Users/nilesh.gupta/Desktop/chromedriver" );
    driver = new ChromeDriver();
    driver.get("https://classic.crmpro.com");

   }
@When("^Tittle of login page is Free CRM$")
public void tittle_login_page_is_Free_CRM() {
    String tittle = driver.getTitle();
    System.out.println("tittle is : " + tittle);
    Assert.assertEquals("#1 Free CRM for Any Business: Online Customer Relationship Software", tittle);
}
@Then("^User enters username and password$")
public void user_enters_username_and_password() {
    driver.findElement(By.xpath("/html/body/div[2]/div/div[3]/form/div/input[1]\n" )).sendKeys("naveenk");
    driver.findElement(By.xpath("/html/body/div[2]/div/div[3]/form/div/input[2]\n" )).sendKeys("test@123");
}

@Then("^User clicks on login button$")
public void user_clicks_on_login_button() {
     driver.findElement(By.className("btn btn-small")).click();
}

@Then("^User is on Home Page$")
public void user_is_on_Home_Page() {
   String tittle= driver.getTitle();
   System.out.println("tittle is : " + tittle );
   Assert.assertEquals("CRMPRO", tittle);
}

}

  • login.feature
  • testrunner.java
  • pom.xml

  • 请尝试用此替换Runner中的代码。重建项目,然后关闭并重新打开它

      package MyRunner;
    
        import org.junit.runner.RunWith;
    
        import io.cucumber.junit.Cucumber;
        import io.cucumber.junit.CucumberOptions;
    
        @RunWith(Cucumber.class)
        @CucumberOptions(
                features = "src/main/java/Features/login.feature",
                glue={"StepDefinition"}
                /*format={"pretty","html:test-output"}*/
        )
        public class testRunner {
        }
    

    请尝试用此替换Runner中的代码。重建项目,然后关闭并重新打开它

      package MyRunner;
    
        import org.junit.runner.RunWith;
    
        import io.cucumber.junit.Cucumber;
        import io.cucumber.junit.CucumberOptions;
    
        @RunWith(Cucumber.class)
        @CucumberOptions(
                features = "src/main/java/Features/login.feature",
                glue={"StepDefinition"}
                /*format={"pretty","html:test-output"}*/
        )
        public class testRunner {
        }
    

    请试着养成在test文件夹
    src/test/java
    而不是
    main
    中编写测试代码的习惯。写入
    src/main/java
    的所有内容都默认打包并交付给客户,而写入
    src/test/java
    的所有内容都不是

    要素选项有助于Cucumber在项目文件夹结构中定位要素文件。 如果功能文件位于深文件夹结构中,请使用features=“src/test/features”

    胶水 用于定位步骤定义文件

    一旦您进行了更改,请参考下面的代码以获取您的跑步者文件

    package MyRunner;   
    import org.junit.runner.RunWith;
    import cucumber.api.CucumberOptions;
    import cucumber.api.junit.Cucumber;
    
    @RunWith(Cucumber.class)
    @CucumberOptions(
     features = "src/test/features"
     ,glue={"src/test/stepDefinition"}
     ,monochrome = false
     )
    
    public class testRunner {
    
    }
    

    请试着养成在测试文件夹
    src/test/java
    而不是
    main
    中编写测试代码的习惯。写入
    src/main/java
    的所有内容都是默认打包并交付给客户的,而放入
    src/test/java
    的所有内容都不是

    要素选项有助于Cucumber在项目文件夹结构中定位要素文件。 如果功能文件位于深文件夹结构中,请使用features=“src/test/features”

    胶水 用于定位步骤定义文件

    一旦您进行了更改,请参考下面的代码以获取您的跑步者文件

    package MyRunner;   
    import org.junit.runner.RunWith;
    import cucumber.api.CucumberOptions;
    import cucumber.api.junit.Cucumber;
    
    @RunWith(Cucumber.class)
    @CucumberOptions(
     features = "src/test/features"
     ,glue={"src/test/stepDefinition"}
     ,monochrome = false
     )
    
    public class testRunner {
    
    }
    

    在您的示例中,我认为实际的问题是您不能在stepdefinitions(Cucumber表达式)中使用锚定“^”或$,并且它不应该出现在较新版本的io.Cucumber中。该样式用于info.cukes版本

    应该是

    @Given("User is already on login page")
    public void user_already_on_login_page(){
        System.setProperty("webdriver.chrome.driver","/Users/nilesh.gupta/Desktop/chromedriver" );
        driver = new ChromeDriver();
        driver.get("https://classic.crmpro.com");    
    }
    
    而不是

    @Given("^User is already on login page$")
    public void user_already_on_login_page(){
        System.setProperty("webdriver.chrome.driver","/Users/nilesh.gupta/Desktop/chromedriver" );
        driver = new ChromeDriver();
        driver.get("https://classic.crmpro.com");    
    }
    

    在您的示例中,我认为实际的问题是您不能在stepdefinitions(Cucumber表达式)中使用锚定“^”或$,并且它不应该出现在较新版本的io.Cucumber中。该样式用于info.cukes版本

    应该是

    @Given("User is already on login page")
    public void user_already_on_login_page(){
        System.setProperty("webdriver.chrome.driver","/Users/nilesh.gupta/Desktop/chromedriver" );
        driver = new ChromeDriver();
        driver.get("https://classic.crmpro.com");    
    }
    
    而不是

    @Given("^User is already on login page$")
    public void user_already_on_login_page(){
        System.setProperty("webdriver.chrome.driver","/Users/nilesh.gupta/Desktop/chromedriver" );
        driver = new ChromeDriver();
        driver.get("https://classic.crmpro.com");    
    }
    

    尝试
    @CucumberOptions(features=“classpath:features/login.feature”,
    尝试
    @cucucumberOptions(features=“classpath:features/login.feature”,
    谢谢你的输入。我以后会记住这一点。你能接受我的答案并按下向上投票按钮吗?谢谢你的输入。我以后会记住这一点。你能接受我的答案并按下向上投票按钮吗?