运行 使用 Cucumber JVM 进行测试时出现 UndefinedStepException

UndefinedStepException when run test using Cucumber JVM

我使用 Cucumber+Junit+Appium 为移动应用程序开发了一个测试。当我尝试 运行 使用 Cucumber 和 JUnit 运行ner 进行测试时,我收到:io.cucumber.junit.UndefinedStepException: The step "I install the application" is undefined. You can implement it using the snippet(s) below:

我尝试了 medium 博客和堆栈问题中的一些解决方案,但这没有帮助。

我有一个项目结构:

src
 |-main
 |--java
 |---{project-name}
 |----config
 |----models
 |----screens
 |----services
 |-test
 |--java
 |---{project-name}
 |----helpers
 |----stepDefinitions
 |-----LoginStep.java
 |-----BaseStep.java
 |-----LoginStep.java
 |----RunCucumber.java
 |--resources
 |---feature
 |----Login.feature 

RunCucumber.java

package com.mobile.automation.framework;

import com.google.inject.Guice;
import com.mobile.automation.framework.module.ServiceModules;
import com.mobile.automation.framework.service.AppiumServer;
import com.mobile.automation.framework.config.drivers.DriverFactory;
import com.mobile.automation.framework.module.ScreensModule;
import io.appium.java_client.AppiumDriver;
import io.cucumber.junit.Cucumber;
import io.cucumber.junit.CucumberOptions;
import org.junit.After;
import org.junit.Before;
import org.junit.runner.RunWith;

@RunWith(Cucumber.class)
@CucumberOptions(
        strict = true,
        monochrome = true,
        glue = "src.test.java.com.mobile.automation.framework.stepDefinition",
        features = "src/test/resources/feature",
        plugin = {"pretty", "html:target/cucumber-report/cucumber.html",
                "json:target/cucumber-report/cucumber.json",
                "junit:target/cucumber-report/cucumber.xml"})
public class RunCucumber {
    public static AppiumDriver driver;

    @Before
    public void setUpDriver() {
        init();
        new AppiumServer().startServer();
        driver = new DriverFactory().getDriver();
    }

    @After
    public void tearDownDriver() {
        if (driver != null) {
            driver.quit();
            new AppiumServer().stopServer();
        }
    }

    private void init() {
        Guice.createInjector(
                new ScreensModule(driver),
                new ServiceModules(driver)
        ).injectMembers(this);
    }
}

Login.feature

Feature: Sign In feature

  Background:
    Given I install application
    And I enable all network activity
    Then I am on Sign Page

  Scenario: Sign In scenario
    Given I am go to the Login Page
    And I fill valid user data using "Config"
    And I click sign in button

    Then I am login in the application

LoginStep.java

package com.mobile.automation.framework.stepDefinition;

import javax.inject.Inject;

import com.mobile.automation.framework.config.ProjectConfig;
import com.mobile.automation.framework.models.User;
import com.mobile.automation.framework.screens.DashboardScreen;
import com.mobile.automation.framework.screens.SignInScreen;
import io.cucumber.java.ParameterType;
import io.cucumber.java.en.And;
import io.cucumber.java.en.Given;
import io.cucumber.java.en.Then;

import static org.assertj.core.api.AssertionsForClassTypes.assertThat;

/**
 * @author Tomash Gombosh
 */
public class LoginStep {
    @Inject
    private SignInScreen signInScreen;
    @Inject
    private DashboardScreen dashboardScreen;

    @Given("^I am go to the Login Page$")
    public void iAmGoToTheLoginPage() {
        dashboardScreen.tapLogin();
    }

    @And("I fill valid user data using {string} {string}")
    public void iFillValidUserDataUsing(String userName, String password) {
        signInScreen.fillLogin(userName, password);
    }

    @And("I fill valid user data using {string}")
    @ParameterType("Config")
    public void iFillValidUserDataUsing() {
        signInScreen.fillLogin(new User(data -> {
            data.setEmail(new ProjectConfig().getBaseUser());
            data.setPassword(new ProjectConfig().getBaseUserPassword());
        }));
    }

    @And("I click sign in button")
    public void iClickSignInButton() {
        signInScreen.clickLogin();
    }

    @Then("I am login in the application")
    public void iAmLoginInTheApplication() {
        assertThat(signInScreen.isDisplayed()).isEqualTo(true);
    }

}

登录class上的一些步骤丢失了,但那是因为我想把所有的代码都放在问题上。

我希望 运行 该功能,但实际上那是行不通的。

通常 src.test.java 不是软件包名称的一部分。尝试使用:

glue = "com.mobile.automation.framework.stepDefinition",

而且由于 Cucumber 默认会在 runners 包中搜索胶水,因此也可以完全删除胶水。