【问题标题】:Cucumber not able to read the definition fileCucumber 无法读取定义文件
【发布时间】:2020-03-09 14:06:50
【问题描述】:

我是 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);
}

}

  1. login.feature
Feature: Free CRM Login Feature

  Scenario: Free CRM Login Test Scenario

    Given User is already on login page
    When  Tittle of login page is Free CRM
    Then  User enters username and password
    Then  User clicks on login button
    Then User is on Home Page



  1. testrunner.java
package MyRunner;

import org.junit.runner.RunWith;

import io.cucumber.junit.Cucumber;
import io.cucumber.junit.CucumberOptions;

@RunWith(Cucumber.class)
@CucumberOptions(
        features = "/Users/nilesh.gupta/eclipse-workspace/CucumberBDD/src/main/java/Features/login.feature",
        glue={"stepDefinition"}
        /*format={"pretty","html:test-output"}*/
)
public class testRunner {
}

  1. pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>CucumberBDD</groupId>
  <artifactId>CucumberBDD</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>CucumberBDD</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <cucumber.version>4.8.0</cucumber.version>
    <selenium.version>3.5.3</selenium.version>
  </properties>

  <dependencies>

       <dependency>
            <groupId>io.cucumber</groupId>
            <artifactId>cucumber-junit</artifactId>
            <version>${cucumber.version}</version>
        </dependency>

      <dependency>
            <groupId>io.cucumber</groupId>
            <artifactId>cucumber-java</artifactId>
            <version>${cucumber.version}</version>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>${selenium.version}</version>
        </dependency>
  </dependencies>
</project>

  1. 输出

There were undefined steps. You can implement missing steps with the snippets below:

@Given("User is already on login page")
public void user_is_already_on_login_page() {
    // Write code here that turns the phrase above into concrete actions
    throw new cucumber.api.PendingException();
}

@When("Tittle of login page is Free CRM")
public void tittle_of_login_page_is_Free_CRM() {
    // Write code here that turns the phrase above into concrete actions
    throw new cucumber.api.PendingException();
}

@Then("User enters username and password")
public void user_enters_username_and_password() {
    // Write code here that turns the phrase above into concrete actions
    throw new cucumber.api.PendingException();
}

@Then("User clicks on login button")
public void user_clicks_on_login_button() {
    // Write code here that turns the phrase above into concrete actions
    throw new cucumber.api.PendingException();
}

@Then("User is on Home Page")
public void user_is_on_Home_Page() {
    // Write code here that turns the phrase above into concrete actions
    throw new cucumber.api.PendingException();
}

【问题讨论】:

  • 试试@CucumberOptions(features = "classpath:Features/login.feature",

标签: java maven selenium junit cucumber


【解决方案1】:

在您的示例中,我认为实际的问题是您不能在 stepdefinitions(黄瓜表达式)中使用锚点 ^ 或 $,并且对于较新版本的 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");    
}

【讨论】:

    【解决方案2】:

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

    Features 选项帮助 Cucumber 在项目文件夹结构中找到 Feature 文件。 如果 Feature 文件位于深层文件夹结构中,请使用 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 {
    
    }
    

    【讨论】:

    • 感谢您的意见。以后我会牢记这一点。
    • 你能接受我的回答并按upvote按钮吗?
    【解决方案3】:

    试试,用这个替换你在 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 {
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-12-07
      • 2018-06-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-24
      相关资源
      最近更新 更多