33

I have a story line as follows

Scenario: 

Given user is on Login page

When user types login details with xxx as user xxx as passwd and submits

Then dashboard is shown

please advise, how to comment or not to make run for a line(example: 2 line should not undergo for test after 1 directly 3 line)

Daniel
  • 3,541
  • 3
  • 33
  • 46
Vijay
  • 1,026
  • 3
  • 11
  • 28

4 Answers4

39

You want to temporarily disable a step in your scenario? Comment the line using the prefix "!-- ", such as

Given user is on Login page
!-- When user types login details with xxx as user xxx as passwd and submits
Then dashboard is shown

The space right after the !-- is mandatory. Thanks @flaz14

See documentation on JBehave: http://jbehave.org/reference/stable/grammar.html

ACV
  • 9,964
  • 5
  • 76
  • 81
dertseha
  • 1,086
  • 8
  • 11
  • 1
    Doesn't work anymore. Commented line is merged with previous parseable line. Does anyone know some trick to comment part of *.story file? – dexx Oct 13 '16 at 14:21
  • Though @dexx wrote this on 2013, it still doesn't work. Anything new on this? – Nom1fan Nov 08 '16 at 13:18
  • Hello there, @Nom1fan - Do you have examples to reproduce this? (I don't have a recent installation at hand) Because if so, it would be a long unnoticed bug; If you search the source repository at https://github.com/jbehave/jbehave-core for the string "!--" you'll find various examples and the corresponding code to handle it. – dertseha Dec 04 '16 at 06:19
  • @flaz14: That's not clear from the grammar or the FAQ. :-( – Martin Schröder Apr 04 '17 at 14:55
  • The space right after the !-- should be omittted. It Must follow with the first letter of the comment. – Carlos Galo Campos Nov 13 '17 at 10:27
6

I know this an old question, but I have found a solution for this, and I share for the community :

'!-- ' is the sign for cancellable step, means steps that need to be disabled.

In our project, we use a story Parser :

this.currentConf.useStoryParser(new TransformingStoryParser(this.currentConf.storyParser(), this.transformComment, this.transformCutter));

We needed to put documentation in user stories, so we made a difference between comments and cancellable steps :

!-- Then ... => Cancellable steps (native way)

!--| This is a a comment... => Comment

With the transformer, comments are stripped from the story before it is executed.

This is the same for '|--', a cancellable example. We have introduced '|--|', or a 'cutter' to prevent the rest of the story to be executed.

The classes (this.transformComment, this.transformCutter) :

    public class ParserTransformingComment implements StoryTransformer {

        public ParserTransformingComment() {
        }


        /**
         * Suppression de commentaires
         */
        private Rewriter vC = new Rewriter("(?ms)^((!--\\|[^\\r\\n]*)(?:\\r?\\n)?)") {
            @Override
            public String replacement() {
                System.out.println("Ignoré : " + this.group(1));            
                return "";
            }
        };

        @Override
        public String transform(String pStoryAsText) {
            return vC.rewrite(pStoryAsText);
        }

    }

    public class ParserTransformingCutter implements StoryTransformer {    
        public ParserTransformingCutter() {
        }

        /**
         * Gestion des 'cutter'.
         */
        private Rewriter vS = new Rewriter("(?ms)^(\\|--\\|.*)") {
            @Override
            public String replacement() {
                System.out.println(">>> Cette section va être ignorée : ");
                System.out.println(StringUtils.lineStart("> ",this.group(1)));
                System.out.println("-------------");            
                return "";
            }
        };


        @Override
        public String transform(String pStoryAsText) {
            return vS.rewrite(pStoryAsText);
        }

    }

EDIT: As of Jbehave 4.3, comments are handled in a better way, even without StoryTransformer :


!--BlahBlah -- ignored completely by Jbehave
!-- Then ... -- steps ignored (registered in the report)
!-- Blah blah -- comment (registered in the report)

Regards, David C.

David Clain
  • 61
  • 1
  • 2
2

If you are using Eclipse IDE then you can use shortcut Ctrl + T to comment single / multiple story line.

Daniel
  • 3,541
  • 3
  • 33
  • 46
1

Use this !-- When user types login details with xxx as user xxx as password and submits. Note :For comment "!--" space is mandatory otherwise it will execute the step.

Daniel
  • 3,541
  • 3
  • 33
  • 46
Suraj
  • 317
  • 4
  • 7