运行测试时出现黄瓜异常

时间:2019-12-03 21:38:03

标签: java selenium automation automated-tests cucumber

我有一个我认为很愚蠢的问题... 我不能对黄瓜进行测试。

返回以下错误:

cucumber.runtime.CucumberException: 

Classes annotated with @RunWith(Cucumber.class) must not define any
Step Definition or Hook methods. Their sole purpose is to serve as
an entry point for JUnit. Step Definitions and Hooks should be defined
in their own classes. This allows them to be reused across features.
Offending class: class Teste.testecucumber

有人可以帮忙吗?

谢谢!!

1 个答案:

答案 0 :(得分:2)

@Runwith在Cucumber项目的TestRunner类中声明。黄瓜项目具有3种定义的类类型:

  1. 步骤定义类
  2. 功能类
  3. 亚军班

请为上述课程找到以下示例:

1。要素类

测试用例是用此类编写的

   Feature: Title of your feature
   I want to use this template for my feature file

   @tag1
   Scenario: Verify login to the system.
   Given I navigate to url
   And I enter username as "username"
   And I enter password as "password"
   When I click Login
   Then I should be logged into the system

2。步骤定义类

功能步骤在此类中定义

    public class LoginPage(){

     @Given("I navigate to the url")
     public void navigate() {

        /* your code for the above 
        step comes here*/
      }

     }

3。跑步班

runner类由要素的位置和步骤定义组成。这是Junit类,不能包含黄瓜步骤定义注释。 (这就是流道类不能成为步骤定义类的原因)。但是,您可以在此类中包含BeforeClass,AfterClass(Junit注释)

 import org.junit.runner.RunWith;
 import cucumber.api.CucumberOptions;
 import cucumber.api.junit.Cucumber;

 @RunWith(Cucumber.class)
 @CucumberOptions( 
 features = {"classpath:<location of your folder with feature classes / package name>"},
 glue = {"<package name>" },
 tags = { "<the tags that has to be executed>","can be comma separated multiple tags" }
 )



 public class testrunner {

   //@AfterClass
   public static void quitDriver() {
    driver.quit();
   }
}

希望这对您有帮助!