Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to write a gherkin step on multiple lines?

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.

like image 642
Dragan Nikolic Avatar asked Feb 25 '16 21:02

Dragan Nikolic


People also ask

How do you comment multiple lines on the Gherkin?

9.2) you can simply select the lines and toggle them into comments and back. The default shortcuts are Ctrl+K,C to comment and Ctrl+K,U to uncomment.

Can we have multiple Then in Gherkin?

The Cardinal Rule is a way to break out of the procedure-driven mindset, and banning multiple When-Then pairs per Gherkin scenario is an effective rule for enforcing it.


1 Answers

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 :)

like image 130
Sarabjit Singh Avatar answered Oct 05 '22 22:10

Sarabjit Singh