Run step in java

53 views
Skip to first unread message

David Vieira

unread,
Apr 29, 2014, 3:51:54 PM4/29/14
to subs...@googlegroups.com
How can I run, in a java Step, a Step defined in a .substep file ?

Thanks in advance,
David Vieira

David Vieira

unread,
Apr 29, 2014, 9:57:06 PM4/29/14
to subs...@googlegroups.com
Another question:

How can I reuse Scenarios to avoid replications, like the GivenStories of JBehave ?

I'm trying to do this in a Step, but I'm having trouble trying to discover how to get the execution context:

@Step("Dado:   que execute o cenário \"([^\"]*)\" de \"([^\"]*)\"")

    public void teste(String cenario, String featureFile) {

final FeatureFileParser fp2 = new FeatureFileParser();

        final FeatureFile fFile = fp2.loadFeatureFile(new File("src/test/resources/" + featureFile));

        System.out.println(fFile);

        

        

        TestParameters parameters = new TestParameters(null, null, cenario);

final ExecutionNodeTreeBuilder nodeTreeBuilder = new ExecutionNodeTreeBuilder(parameters );


        // building the tree can throw critical failures if exceptions are found

        RootNode rootNode = nodeTreeBuilder.buildExecutionNodeTree("description");

        List<FeatureNode> children = rootNode.getChildren();

        

        BasicScenarioNode node = null;

        for (FeatureNode featureNode : children) {

// if (featureNode instanceof BasicScenarioNode) {

//

// }

}

        BasicScenarioNodeRunner basicScenarioNodeRunner = new BasicScenarioNodeRunner();

basicScenarioNodeRunner.run(node, null);

    }

Cheers !

David Vieira

unread,
Apr 29, 2014, 11:04:12 PM4/29/14
to subs...@googlegroups.com
Ugly hack that is working:

@Step("Dado:   que execute o cenário \"([^\"]*)\" de \"([^\"]*)\"")

    public void teste(Integer cenario, String featureFile) {

final FeatureFileParser fp2 = new FeatureFileParser();

        final FeatureFile fFile = fp2.loadFeatureFile(new File("src/test/resources/" + featureFile));

        System.out.println(fFile);

        

        

        TestParameters parameters = (TestParameters) ExecutionContext.get(Scope.SUITE, "parameters");

        TestParameters parameters2 = new TestParameters(new TagManager(""), parameters.getSyntax(), ("src/test/resources/" + featureFile));

parameters2.init();

        final ExecutionNodeTreeBuilder nodeTreeBuilder = new ExecutionNodeTreeBuilder(parameters2);


        // building the tree can throw critical failures if exceptions are found

        RootNode rootNode = nodeTreeBuilder.buildExecutionNodeTree("description");

        BasicScenarioNode node = null;

        int i = 1;

        for (ScenarioNode<?> scenario : rootNode.getChildren().get(0).getChildren()) {


        if (scenario instanceof BasicScenarioNode

        && i == cenario) {

        node = (BasicScenarioNode) scenario;

        break;

        }

        

        i++;

        }


        BasicScenarioNodeRunner basicScenarioNodeRunner = new BasicScenarioNodeRunner();

RootNodeExecutionContext rootNodeExecutionContext = (RootNodeExecutionContext) ExecutionContext.get(Scope.SUITE, "context");

try {

Field f = WebdriverSubstepsPropertiesConfiguration.class.getDeclaredField("reuseWebdriver");

    f.setAccessible(true);

    f.set(WebdriverSubstepsPropertiesConfiguration.INSTANCE, true);

basicScenarioNodeRunner.run(node, rootNodeExecutionContext);

f.set(WebdriverSubstepsPropertiesConfiguration.INSTANCE, false);

} catch (Exception e) {

e.printStackTrace();

David Moss

unread,
May 1, 2014, 4:28:34 AM5/1/14
to David Vieira, subs...@googlegroups.com
Hi David,

There isn't currently a way to execute a step defined within a substep file from within Java code.

However, I've not yet come across a case in which I needed to.  Typically you would only need to implement your lower-level, fine grained actions in code, and compose them together into larger actions in a substep, rather than trying to call everything from Java.

Perhaps if you could provide an example I can suggest how it may be restructured to avoid the need to do this?

Regards,
Dave



--
You received this message because you are subscribed to the Google Groups "Substeps" group.
To unsubscribe from this group and stop receiving emails from it, send an email to substeps+u...@googlegroups.com.
To post to this group, send email to subs...@googlegroups.com.
Visit this group at http://groups.google.com/group/substeps.
For more options, visit https://groups.google.com/d/optout.

David Moss

unread,
May 1, 2014, 5:40:17 AM5/1/14
to David Vieira, subs...@googlegroups.com
Hi David,

Regarding reusing whole scenarios, glad you've got this working!  

As you've found this is also not something that's directly supported, though obviously by reusing some of the internal parsing code it's possible to do.  A word of caution though; this isn't how we envisaged this code being used.  Some of these classes are part of the core substeps codebase and are liable to change in future releases.  

Are you aware that you can use a Background step to execute steps before each scenario in a feature?  For example:

Feature: A feature to test BDD Runner functionality

Background: scenario background
    Background step 1
    Background step 2

Scenario: Simple top level scenario
    When something
    Then something else


Backgrounds are a more idomatic way for setting up data or other preconditions.  A disadvantage of using GivenStories like behaviour is that it introduces language that's not natural to non-technical users into the feature and can make scenarios more difficult to understand.  This kind of support was removed from Cucumber in 2009 for these very reasons.



Regards,
Dave.



  


--

David Vieira

unread,
May 1, 2014, 8:40:17 PM5/1/14
to subs...@googlegroups.com, David Vieira
Hi Dave,

Thanks for the reply !

My scenario is:

we have about 20 roles in the system, and It's very hard to maintain all these users consistent to the testing.

I want to make the only premisse to the execution of the test a user called ADMIN, and this user can register all the users and save theirs new logins.

So if a scenarios starts with "Given i'm logged with user A", I register him (only if not registered in the SUITE context)

I was trying to separate the registration feature in a folder and reuse then when needed. 

I hope I make clear my requirements,

Thanks very much !
David Vieira

Ps: Sorry for my poor English.

Ian Moore

unread,
May 12, 2014, 4:00:13 AM5/12/14
to subs...@googlegroups.com, David Vieira
Hi David,

That's very interesting as we've had a very similar request from a project we run here, where user creation was defined in a set of steps with other scenarios wanting to use those users, creating them if necessary, but not failing if they already existed.

One way we thought we might be able to achieve this is to allow certain steps to be able to fail gracefully, perhaps something like this:

# a bit verbose and chunky, but precise
? (e=TimedOutException, msg="sorry timed out") ClickLink mightNotBeThere       # a TimedOutException with that message would be permissable

# quick, fluent, broad brush...
? ClickLink mightNotBeThere   #any exception would be ok

These are just some initial ideas that might meet your requirement..?

In terms of a roadmap, we are in the process of putting one together for Substeps and a plan as to how to get some of these features implemented, watch this space!  One item that we are hoping to implement is better class / library isolation for the running of tests using different classloaders - executing features directly from a step might cause issues in this mode. 

Thanks for the feedback and suggestions
Kind regards
Ian Moore
Reply all
Reply to author
Forward
0 new messages