0

の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での複数のステップのファイルに共通のメソッドを実装する、私はこれを持っている

@smoke 
Feature: Feature 1 

Background: 
Given User is Logged in 

Scenario: 
Given Go to this Page 
When Perform this action 

手順クラス:

@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 this Page$") 
public void goToThisPage() 
{ 
    //Implemention 
} 

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

MyFeature2Test.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 
} 

は、だからここに私はすべての手順ファイル(MyFeature1Steps.javaMyFeature2Steps.java、など)にバックグラウンド(与えられたユーザーがログインしている)の実装方法(navigateAndLogin())を記述する必要があります。

testNGのbeforeAllのような共通の場所に '指定されたユーザがログインしました'というメソッドを書くことはできますか?

答えて

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
  • Guiceの

も見hereを持っています。