2017-09-28 48 views
0

考慮我有以下特徵文件:調用從另一個特點一個特徵文件中黃瓜

Login.feature

Feature: Login on website 

Scenario: Login verification on site 
    Given Navigate to site login page 
    When User enters username 'admin1' 
    And User enters password 'admin1' 
    And User clicks on login button 
    Then User should not be able to log in successfully 

Home.feature

Feature: Welcome Page Verification 

Scenario: Verify the page that comes after login 
    Given Login is successfully done 
    When The page after login successfully appears 
    Then The test is done 

在Home.feature文件,我需要先執行Login.feature然後調用home.feature。所以當我從亞軍測試中執行回家時,它將依次執行登錄和回家。

RunnerTest.java

import org.junit.runner.RunWith; 

import cucumber.api.CucumberOptions; 
import cucumber.api.junit.Cucumber; 

@RunWith(Cucumber.class) 


@CucumberOptions(strict = false, features = { 
     "src/test/resources/Features/Home.feature", 
      }, glue = { "tests" }, plugin = "html:target/cucumber-reports", format = { "pretty", 
     "json:target/cucumber.json" }, tags = { "[email protected]" }) 

public class RunnerTest {} 

感謝&問候, MEGHA

回答

1

你並不需要從第二個特點叫第一個特徵。你需要做的是在第二個功能中可以登錄你的步驟。它可以通過調用你在實現你的第一個功能時創建的代碼來實現。

第一個功能是您在第一次執行登錄時可能會寫入的內容。在這一過程中,您將步驟和代碼,這些步驟打電話來登錄你進來。

那種你應該創造的(對不起所有的例子都是紅寶石我不這樣做JAVA)

  1. A碼它知道它的名字,電子郵件地址和密碼
  2. 的方法測試用戶的實體,可以用戶測試用戶登錄

然後,你可以寫一個helper方法,如

def login_as(user) visit login_path fill_in :email, with: user.email fill_in :password, with: user.password submit_form end

,現在在你的第二個功能,你可以有類似

Given I am an admin 
When I login 

和實施這些步驟

Given 'I am an admin' do 
    # note create_user is a method you would have created when doing user 
    # registration/creation 
    @i = create_user(type: admin) 
end 

When "I login" do 
    login_as @i 
end 

和地方,你將有一些輔助方法

module StepHelperMethods 
    def create_user 
    ... 
    return user 
    end 

    def login_as(user) 
    ... 
    end 
end 
World StepHelperMethods 

您的代碼重用始終發生在更低的級別。理想情況下,您應該重新使用您之前創建的幫助程序方法,以使其他方案正常工作。你也可以直接調用步驟(嵌套步驟),但這是一件非常糟糕的事情。

0

試試這個:

創建RunnerLogin類調​​用Login.feature

在你的腳步類,在實現 Given Login is successfully done,這樣做的方法:

@Given("^Login is successfully done$") 
public void login_is_successfully_done() { 
    Thread T1 = new Thread(new Thread(() -> { 
     JUnitCore jExecFeature = new JUnitCore(); 
     Result result = jExecFeature.run(RunnerLogin.class); 
    }));     

    T1.start();     
    T1.join(); 
} 
相關問題