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

dced5bon  于 2022-11-11  发布在  其他
关注(0)|答案(2)|浏览(184)

我使用Cucumber+Junit+Appium为移动的应用程序开发了一个测试。io.cucumber.junit.UndefinedStepException: The step "I install the application" is undefined. You can implement it using the snippet(s) below:
我尝试了一些来自中型博客和堆栈问题的解决方案,但这没有帮助。
我有一个项目结构:

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

}

Login类上的一些步骤丢失了,但那是因为我想把所有的代码都放到这个问题上。
我希望运行该功能,但实际上这是行不通的。

nfs0ujit

nfs0ujit1#

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

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

而且因为 cucumber 会在跑步者包中搜索胶水,默认情况下也可以完全去除胶水。

icomxhvb

icomxhvb2#

我是一个新手,在Junit Runner上工作,我同意@ M. P. Korstanje的类路径。虽然我只是不得不改变glue来引用类路径的stepDefs--以前没有得到认可。
基本上,这就是我捕获要触发的必要文件所做的工作

feature = { "classpath:path-from-repo-root.feature" },
glue = { "classpath:reference-to-stepDef-folder" }

请记住:对于glue --我习惯于引用包含stepDefs的文件夹,而不是stepDef文件本身。希望这对某些人有帮助。谢谢

相关问题