In vanilla Cucumber, anything output by a call to puts
in a step definition is captured as "test output" and formatted accordingly, as in the following output example:
Feature: A simple thing
Scenario: A simple scenario # features/simple.feature:3
Given I do a step # features/steps/step.rb:1
This text is output with puts
As you see above, it's helpfully indented in the "pretty" output format. In the JSON format, it's even captured in a structured way:
"keyword": "Scenario",
"name": "A simple scenario",
"line": 3,
"description": "",
"id": "a-simple-thing;a-simple-scenario",
"type": "scenario",
"steps": [
{
"keyword": "Given ",
"name": "I do a step",
"line": 4,
"output": [
"This text is output with puts"
],
}
],
The above is generated with a trivial feature file and a step definition like the following:
Given(/^I do a step$/) do
puts 'This text is output with puts'
end
Is there an equivalent function when implementing Cucumber steps in Java that I can use to have this output captured the same way? Printing to System.out
results in bypassing the capturing mechanism, much like using STDOUT.puts
in Ruby.
I haven't seen any Cucumber-JVM examples that make use of this feature, unlike many of Ruby Cucumber's examples, but there is clearly an entry in the JSON output by Cucumber-JVM for "output," and therefore I imagine there must be a way to write to that field.
It looks like this can be accomplished by writing to the object representing the currently running scenario.
public class StepDefs {
private Scenario scenario;
/* Need to capture the scenario object in the instance to access it
* in the step definition methods. */
@Before
public void before(Scenario scenario) {
this.scenario = scenario;
}
@Given("^I do a step$")
public void iDoAStep() {
scenario.write("This text is output with scenario.write");
}
}
As with the puts
call in Ruby, this gets captured in the JSON output file. It's also colored in the "pretty" output, though it's not indented like the Ruby implementation. Nevertheless, this seems like the closest equivalent I can find.