Doc String in Cucumber framework

In Cucumber, Doc Strings are used to pass a large block of text to a step definition. This is useful when sending descriptions, JSON/XML payloads, multi-line messages, etc.

Syntax

A Doc String is enclosed within triple double quotes """ or triple backticks ```````. It must appear on the line immediately after the step.

Example 1:

And Send an email to the user
  """
  New customer record is created
  The customer record is displayed in the Customer Details page in the application.
  You can search for the record using the search box.
  """


Example 2:

And Send an email to the user


Step Definition for Doc String

The text in the Doc String will automatically be passed as the last argument to the step definition.

@And("Send an email to the user")
public void sendEmail(String doc) {
    System.out.println("Doc is - " + doc);
}


You Can Use It to Pass JSON or XML Too

JSON Payload:

"""
{
  "employee": {
    "name": "John",
    "age": 30,
    "married": true
  }
}
"""


XML Payload:

"""
<employees>
<employee>
<name>tester</name>
<email>tester@domain.com</email>
</employee>

</employees>
"""


When to Use Doc String?

     When a step needs a multi-line message or payload
     To pass formatted data like JSON, XML, HTML
     For email bodies, API requests, descriptions, or logs
 

Related Tutorials