Behat Multiline Strings (PyStrings)

Profile picture for user devraj

Multiline Strings (also known as PyStrings) are useful for specifying a larger piece of text. The inspiration for PyString comes from Python where """ is used to describe docstrings. PyString concept is exactly same as Cucumber Doc String. So, in Cucumber you will find Doc String instead of PyString.

PyString should be written within pair of triple quotes ("""). PyString are handy for passing a larger piece of text to a step definition. 

PyString allows you to specify a larger piece of text that could not fit on a single line. For example, if you need to describe the precise content of an email message or contact us message, you could use PyString.

Consider below example:

@contactus
Feature: As an Ecommerce store owner,
  I want customer are able to contact me in case of any query

  @regression
  Scenario: Fill in Contact Us form
    Given I am on the homepage
    And I follow "Contact us"
    When I fill in "email" with "goswami.tarun77@gmail.com"
    And I fill in "message" with:
      """
      Dear,

      Its been more than a week, I have not received my order.

      Thanks,
      Tarun Goswami
      """

We use PyString to parse big data as one chunk. When we have lot of text to enter in multiple line this is good solution. 

Indentation of the opening """ is not important, although common practice is two spaces in from the enclosing step. The indentation inside the triple quotes, however, is significant. Each line of the string passed to the step definition’s callback will be de-indented according to the opening """. Indentation beyond the column of the opening """ will therefore be preserved.

Here we are using all predefined steps of Mink and once you will execute above scenario, the text in """ will be entered in same format. Check below screenshot:

PyStrings are stored in a PyStringNode instance, which you can simply convert to a string with (string) $pystring or $pystring->getRaw() as in the example above.

/**
* @When a blog post named :arg1 with:
*/
public function aBlogPostNamedWith($arg1, PyStringNode $string)
{
    print($string->getRaw());
}
Tags