java - 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.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);
}

}

  • 登录功能
  • 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
    
    
    
    
  • 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 {
    }
    
    
  • 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>
    
    
  • 输出
  • 
    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();
    }
    
    

    最佳答案

    请尝试养成在测试文件夹 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 {
    
    }
    

    关于java - Cucumber无法读取定义文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58842824/

    相关文章:

    使用 32 字节 key 的 Java AES 加密 - key 大小无效

    java - 无法从 Fragment 访问 TextView

    java - 什么是 JUnit 测试用例?我将如何编写一个测试用例?

    java - 通过批处理文件运行 maven java 项目时忽略 log4j 属性

    java - 单击时如何清除editText中的字符串

    java - Eclipse - 当涉及 Maven 时为 "Run as Java Application"

    java - 没有名为 itmd4515PU 的 EntityManager 持久性提供程序

    java - 在哪里可以使用 Eclipse IDE for Java 在 testng 框架中找到 org.testng.TestNg 包?

    ruby - 我可以使用适用于 Ruby 的 Selenium-webdriver 自动阻止 Chrome 请求吗?

    python-3.x - 如何在 python selenium 中设置 Chrome 实验性选项 same-site-by-default-cookie