Skip to content

Commit

Permalink
[Jython] Access to scenario in Before and After hooks. Closes cucumbe…
Browse files Browse the repository at this point in the history
…r#582.

The implementation is a little ghetto. I tried to get the arity
of the hook function using inspect.getargspec, but couldn't get
it to work. This implementation will most likely be sufficient though.
  • Loading branch information
aslakhellesoy committed Sep 5, 2013
1 parent 89982ea commit ba6a1c3
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 9 deletions.
1 change: 1 addition & 0 deletions History.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
## [1-1-5-SNAPSHOT (Git master)](https://github.com/cucumber/cucumber-jvm/compare/v1.1.4...master)

* [Jython] Access to scenario in Before and After hooks ([#582](https://github.com/cucumber/cucumber-jvm/issues/582) Aslak Hellesøy)
* [Core] Replace placeholders in the Scenario Outline title ([#580](https://github.com/cucumber/cucumber-jvm/pull/580), [#510](https://github.com/cucumber/cucumber-jvm/issues/510) Jamie W. Astin)
* [JUnit/Core] `@cucumber.junit.api.Cucumber.Options` is deprecated in favour of `@cucumber.api.Options` ([#549](https://github.com/cucumber/cucumber-jvm/issues/549) Aslak Hellesøy)
* [JUnit] Inherit Information of @Cucumber.Options ([#568](https://github.com/cucumber/cucumber-jvm/issues/568) Klaus Bayrhammer)
Expand Down
3 changes: 3 additions & 0 deletions core/src/main/java/cucumber/api/Scenario.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
* It allows writing text and embedding media into reports, as well as inspecting results (in an After block).
*/
public interface Scenario {
/**
* @return source_tag_names. Needed for compatibility with Capybara.
*/
Collection<String> getSourceTagNames();

/**
Expand Down
32 changes: 24 additions & 8 deletions jython/src/main/java/cucumber/runtime/jython/JythonBackend.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package cucumber.runtime.jython;

import cucumber.api.Scenario;
import cucumber.runtime.Backend;
import cucumber.runtime.CucumberException;
import cucumber.runtime.Glue;
Expand All @@ -9,13 +10,12 @@
import cucumber.runtime.snippets.FunctionNameSanitizer;
import cucumber.runtime.snippets.SnippetGenerator;
import gherkin.formatter.model.Step;
import org.python.core.PyException;
import org.python.core.PyInstance;
import org.python.core.PyObject;
import org.python.core.PyString;
import org.python.core.*;
import org.python.util.PythonInterpreter;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.List;

public class JythonBackend implements Backend {
Expand Down Expand Up @@ -87,10 +87,26 @@ public void addAfterHook(PyInstance hookDefinition) {
glue.addAfterHook(new JythonHookDefinition(this, hookDefinition));
}

public void executeHook(PyInstance hookDefinition, Object[] scenarioResults) {
PyObject[] pyArgs = new PyObject[1];
pyArgs[0] = pyWorld;
hookDefinition.invoke("execute", pyArgs);
public void executeHook(PyInstance hookDefinition, Scenario scenario) {
try {
// Try to pass the scenario
hookDefinition.invoke("execute", pyWorld, Py.java2py(scenario));
} catch (PyException e) {
if (getStacktrace(e).contains("takes exactly 1 argument (2 given)")) {
// The stepdef doesn't want the scenario
hookDefinition.invoke("execute", pyWorld);
} else {
// Some other error. Just rethrow.
throw e;
}
}
}

private String getStacktrace(PyException e) {
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
e.printStackTrace(pw);
return sw.getBuffer().toString();
}

public void execute(PyInstance stepdef, Object[] args) throws Throwable {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public JythonHookDefinition(JythonBackend backend, PyInstance hookDefinition) {

@Override
public void execute(Scenario scenario) throws Throwable {
backend.executeHook(hookDefinition, new Object[]{scenario});
backend.executeHook(hookDefinition, scenario);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,8 @@ def I_have_cukes_in_my_belly(self, arg1):
if (self.n != val):
raise(Exception("Default cukes were %d, not %d" % (self.n, val)))

@After()
def we_can_get_the_scenario(self, scenario):
if(scenario.getStatus() != 'passed'):
print("Oh no!")

0 comments on commit ba6a1c3

Please sign in to comment.