使用 Cucumber (and/or Citrus) 执行异步步骤
Asynchronous steps execution with Cucumber (and/or Citrus)
假设我有如下测试(使用黄瓜)-
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"
将验证数据并发送成功响应。因此 "the login form is submitted"
和 "I expose a HTTP-Rest service to validate the user name and password
" 的步骤定义应该异步执行。
你能帮帮我吗 - 我如何使用黄瓜(or/and 柑橘)实现这一目标。
注意:我没有使用任何 stub
应用程序来公开 "I expose a HTTP-Rest service to validate the user name and password
的 HTTP Rest 服务;我正在尝试使用 Citrus
框架公开该服务。
步骤定义写在java中。
首先,您需要在您的项目中设置 citrus-cucumber 扩展。然后你应该能够使用 @CitrusResource 注释将测试运行器实例注入你的步骤 class:
@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
});
}
假设我有如下测试(使用黄瓜)-
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"
将验证数据并发送成功响应。因此 "the login form is submitted"
和 "I expose a HTTP-Rest service to validate the user name and password
" 的步骤定义应该异步执行。
你能帮帮我吗 - 我如何使用黄瓜(or/and 柑橘)实现这一目标。
注意:我没有使用任何 stub
应用程序来公开 "I expose a HTTP-Rest service to validate the user name and password
的 HTTP Rest 服务;我正在尝试使用 Citrus
框架公开该服务。
步骤定义写在java中。
首先,您需要在您的项目中设置 citrus-cucumber 扩展。然后你应该能够使用 @CitrusResource 注释将测试运行器实例注入你的步骤 class:
@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
});
}