@Before不会在java Cucumber Step中执行
我有一个Cucumber Step类,我正在尝试初始化所有场景的页面模型。 所以我添加了一个@Before带注释的方法:
@Before() private void beforeScenario() { LOGGER.info("Running before!"); loginPage = BrowserDriver.getPageModel(LoginPage.class); }
然后,我有一堆依赖于loginPage设置的步骤。 例如
@When("^I click the help link$") public void I_click_the_help_link() { loginPage.clickHelpLink(); }
我有多个Step类。 上述两种方法都在同一个Step类中。 但是loginPage始终为null。 永远不会调用beforeScenario方法。 我是否完全误解了@Before是如何工作的? 关于如何获得我想要的工作的任何提示?
编辑:我还有一个@After带注释的方法,它可以按照预期在每个场景之后运行。
编辑:Pom可以在http://pastebin.com/PJ6qQRK9上看到
-
确保你使用的是
cucumber.annotation.Before
而不是org.junit.Before
。 黄瓜不会处理JUnit注释。 ( 此博客文章的Scenario Hooks部分中提供了更多信息。) -
确保您的@Before方法是
public
,而不是private
。
这个问题有一个公认的答案,但是我想指出Matt Watson的评论,它为我解决了这个问题,而我在其他地方没有看到类似的建议:
我有一些关于我的黄瓜-jvm测试的游戏,我想我已经发现了它。 你的
@Before
方法应该是public
而不是private
@Before
方法必须是public
。
您好我知道这是一个旧post,但这些解决方案都不适合我。 所以我要分享我的解决方案。
我在包下创建了类Hooks
: com.mycompany.automation.util
package com.mycompany.automation.util; import com.mycompany.automation.rest.database.AS400DBManager; import cucumber.api.java.After; import java.sql.SQLException; /** * @author Julian Mesa * @version 0.1.0 * @since 0.1.0 */ public class Hooks { @After public void beforeScenario() throws SQLException, ClassNotFoundException { System.out.print("Closing connection."); AS400DBManager.getInstance().closeConnection(); } }
然后我在跑步者的胶水选项中指定了包:
@RunWith(CucumberWithSerenity.class) @CucumberOptions( features = "src/test/resources/features", glue = {"com.mycompany.automation.features.steps", "com.mycompany.automation.util"} )
它奏效了。