40

I am new to the Gherkin language and this seems to me like very basic question but I could not find answer to it.

I am aware that it is possible to write multi-line step argument in Gherking, like this:

Given a blog post named "Random" with Markdown body
  """
  Some Title, Eh?
  ==============
  Here is the first paragraph of my blog post. Lorem ipsum dolor sit amet,
  consectetur adipiscing elit.
  """

My question is about writing single step on multiple lines, something like this:

Given that Gherkin language allows me to write my step definitions \
   on multiple lines
Then my test cases would be easier to read :)

In the example above I used '\' as line continuation symbol. BTW, I tried the example above and got parser error.

Dragan Nikolic
  • 1,536
  • 3
  • 17
  • 24
  • 6
    I think this is a better question than the people below are giving you credit for. I'm a succinct writer and I'm constantly running out of space. – Ryan Shillington Feb 24 '17 at 21:09
  • Agreed. Multiline would be a nice readable way to construct some of my behave/gherkin steps. – Mary Bergman Jan 22 '21 at 10:41
  • Using tables is often a good alternative when specifying multiple settings in one step. Variants of setting combinations can then be supported without step explosion (lots of new steps). The "And" or "But" can work, but not when the settings need to go together in one atomic unit. – Mary Bergman Jan 22 '21 at 10:43

2 Answers2

33

Yes, we can write a gherkin step on multiple lines using triple set of double quotes ("""). Gherkin recognizes the triple set of double quotes as the bounding delimiters for the multi-line string and passes it in. Whatever content you write between triple set of double quotes will be passed to your step definition as a single string.

As in my current capybara project, I have written gherkin step on multiple lines as shown below:

Scenario: Some test scenario
  Given Bob is on "www.abc.com"
  When Bob creates team "Test Team"
  Then Bob sees message:
      """
      As the Team Captain you will be responsible for paying for the team after 
      registration closes. You will be emailed instructions at the close of 
      registration.
      """
And Bob clicks "Next" button

Step definition for multi line gherkins step:

And(/^(\S*) sees message:$/) do |user, message|
  page.should have_content(message)
end

In this I have used the content passed as it is. You can also split your content and use as required. For more information please refer to below mentioned link: http://asymmetrical-view.com/2011/06/02/cucumber-gherkin-and-multiline-arguments.html

Hope this helps :)

Sarabjit Singh
  • 786
  • 4
  • 4
  • 25
    This is good information but is not answer to the question asked. The same information is already provided in the question itself as I'm aware that arguments can be written on multiple lines. The question is really about writing single long Gherkin step that has no arguments, on multiple lines. Thomas' answer provides at least a workaround how to achieve that, and is also a good practice. That's why I marked it as correct answer (at least for now :) – Dragan Nikolic Apr 03 '17 at 22:26
  • Nevertheless this was exactly what I was looking for :) Much appreciated @sarabjit-singh – chriszo111 Jan 29 '21 at 15:35
-8

You tried and got a parse error. I assume that writing the long line on a single line did not give you a parse error. You have therefore answered your own question with no.

I would like to suggest that you ask yourself why the lines has to be long. The wanted behaviour, does it really need so many details that the line gets very long?

It is often possible to express your self shorter by rephrasing a sentence. It is also possible that you could break the long sentence by using the Gherkin keywords And or But.

Thomas Sundberg
  • 4,098
  • 3
  • 18
  • 25
  • 3
    Thanks Thomas, I accept this as correct answer. It is not really possible to write a single step across multiple lines, however And or But can be used to achieve the same purpose. – Dragan Nikolic Feb 27 '16 at 20:26