2017-08-29 134 views
2

假設我有一個測試像下面(使用黃瓜) -異步步驟與黃瓜(和/或柑橘)執行

Scenario: Login successfully to Facebook 
    Given the user test exists 
    And user name and password is entered 
    When the login form is submitted 
    Then I expose a HTTP-Rest service to validate the user name and password 
    When I receive a validation success message 
    Then display the welcome message to the user 

在這裏,當"the login form is submitted"被調用時,它請求提交給一個HTTP REST服務這將使用"I expose a HTTP-Rest service to validate the user name and password"將用戶名和密碼傳遞給另一個HTTP休息服務(將由Citrus Framework公開),這將驗證數據併發送成功響應。因此步定義爲"the login form is submitted""I expose a HTTP-Rest service to validate the user name and password「應異步執行

能否請你幫我 - 我怎麼能做到這一點用黃瓜(或/和柑橘)

注意:我不使用任何stub應用程序公開HTTP Rest服務爲"I expose a HTTP-Rest service to validate the user name and password「;我試圖使用Citrus框架公開服務。

步驟定義是用java編寫的。

+0

如果您在提交登錄表單之後再調用'Thread.sleep',該怎麼辦? – alayor

+0

你好,直到執行完「登錄表單被提交」的時候,黃瓜纔會執行「我公開一個HTTP-Rest服務來驗證用戶名和密碼」。 我正在尋找能夠同時執行這些步驟的東西。 – Bappa

回答

2

首先您需要在您的項目中設置柑橘黃瓜擴展。然後,你應該能夠使用@CitrusResource註釋:注射測試運行實例的步驟類:

@CitrusResource 
private TestRunner runner; 

您也可以注入應該接收請求的HTTP服務器實例。

@CitrusEndpoint(name = "userServer") 
private HttpServer userServer; 

然後可以使用測試運行和服務器接收該請求,併發送在步驟定義的響應:

@Then("^I expose a HTTP-Rest service to validate the user name and password$") 
public void exposeHttpRestService() { 
    runner.http(http -> http.server(userServer) 
     .receive() 
     .post() 
     .payload("{\"username\": \"test\", \"password\": \"secret\"}")); 

    runner.http(http -> http.server(userServer) 
     .send() 
     .response(HttpStatus.OK)); 
} 

登錄表單應在一個單獨的步驟定義使用單獨的提交線程爲了創建異步性質:

@When("^the login form is submitted$") 
public void submitForm() { 
    ExecutorService executor = Executors.newSingleThreadExecutor(); 
    executor.submit(() -> { 
     // do submit the form 
    });  
}