在Cucumber中跨多个步骤文件实现常用方法

时间:2016-07-28 19:45:36

标签: java selenium-webdriver cucumber automated-tests cucumber-junit

的pom.xml

<dependency>
   <groupId>info.cukes</groupId>
   <artifactId>cucumber-java</artifactId>
   <version>1.1.8</version>
   <scope>test</scope>
</dependency>
<dependency>
   <groupId>info.cukes</groupId>
   <artifactId>cucumber-core</artifactId>
   <version>1.1.8</version>
   <scope>test</scope>
</dependency>
 <dependency>
    <groupId>info.cukes</groupId>
    <artifactId>cucumber-junit</artifactId>
    <version>1.1.8</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>info.cukes</groupId>
    <artifactId>cucumber-jvm-deps</artifactId>
    <version>1.0.3</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>info.cukes</groupId>
    <artifactId>cucumber-picocontainer</artifactId>
    <version>1.1.8</version>
</dependency>
<dependency>
    <groupId>info.cukes</groupId>
    <artifactId>gherkin</artifactId>
    <version>2.12.2</version>
</dependency>
<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.11</version>
    <scope>test</scope>
</dependency>

规范

|-src
 |-test
  |-java
   |-MyFeature1
     |-MyFeature1Steps.java 
     |-MyFeature1Test.java
   |-MyFeature2
     |-MyFeature2Steps.java 
     |-MyFeature2Test.java
  |-resources
    |-Features
    |-MyFeature1
      |-MyFeature1.feature
    |-MyFeature2
      |-MyFeature2.feature

MyFeature1Test.java中,我有这个:

    @RunWith(Cucumber.class)
    @CucumberOptions(format = { "pretty", "html:target/cucumber", "json:target/cucumber.json" }, features = "classpath:Features" , tags = "@registration", glue={"classpath:MyFeature1/MyFeature1Steps.java"})
    public class MyFeature1Test
    {   
    }

情景文件MyFeature1.feature

@smoke
Feature: Feature 1

Background: 
Given User is Logged in

Scenario: 
Given Go to this Page
When Perform this action

步骤类:MyFeature1Steps.java

public class MyFeature1Steps

@Given("^User is Logged in$")
public void navigateAndLogin()
{
    //Implemention
}

@Given("^Go to this Page$")
public void goToThisPage()
{
    //Implemention
}

@Then("^Perform this action$")
public void verifyAbc()
{
    //Implemention
}

MyFeature2Test.java中,我有这个:

    @RunWith(Cucumber.class)
    @CucumberOptions(format = { "pretty", "html:target/cucumber", "json:target/cucumber.json" }, features = "classpath:Features" , tags = "@registration", glue={"classpath:MyFeature1/MyFeature2Steps.java"})
    public class MyFeature2Test
    {   
    }

情景文件MyFeature2.feature

@Abc
Feature: Feature 2

Background: 
Given User is Logged in

Scenario: 
Given Go to that Page
When Perform that action

另一个步骤:MyFeature2Steps.java

public class MyFeature1Steps

@Given("^User is Logged in$")
public void navigateAndLogin()
{
    //Implemention
}

@Given("^Go to that Page$")
public void goToThatPage()
{
    //Implemention
}

@Then("^Perform that action$")
public void performThis()
{
    //Implemention
}

所以在这里我需要在我的所有步骤文件(navigateAndLogin()MyFeature1Steps.java等)中编写背景(给定用户登录)实现方法(MyFeature2Steps.java)。

我可以在公共场所编写“给定用户登录”的方法,例如testNG中的beforeAll或者可能在套件之前吗?

1 个答案:

答案 0 :(得分:0)

要在不同的步骤类之间共享步骤,您可以实现以下选项之一:

助手类

您可以定义一个帮助程序类,然后可以将其添加到任何步骤类中,它将用于共享数据。那么看看下面的示例类:

class KnowsTheDomain {
    private Account myAccount;
    private CashSlot cashSlot;

    public Account getMyAccount() {
        if (myAccount == null){
            myAccount = new Account();
        }

    return myAccount;

    }

    public CashSlot getCashSlot() {
        if (cashSlot == null){
            cashSlot = new CashSlot();
        }

        return cashSlot;
    }
}

然后你可以在你的步骤类中使用它:

public AccountSteps() {
    helper = new KnowsTheDomain();
}

依赖注入

这是更好,更清洁的方法。这个主题相当广泛,我不会尝试在此处介绍任何主题,只是为了让您知道要查找的主题,下面是您可能要考虑的工具列表:

  • PicoContainer的
  • 吉斯
  • 弹簧

还要看here