0

I have a step definition in which I would like to have multiple pystrings input by the feature file. Is this possible, and if so what, syntax would you use to do that?

Currently my step definition looks like:

 @Then /^(?:I )?get a value "([^"]+)" when I have a username "([^"]+)" and a password "([^"]+)" and I send a "([A-Z]+)" request to "([^"]+)" with form data: and header data:$/

Then my actual feature file looks like:

 Then I get a value "orange" when I have a username "jsmith" and a password "password" and I send a "POST" request to "www.google.com" with form data: 

     """
           {
               "key1": "value1",
               "key2": "value2",
           }
     """
and header data:

     """
           {
               "key1": "value1",
               "key2": "value2",
           }
     """
Nabsta
  • 101
  • 3
  • 12
  • Your whole `Then` definition seems like you're handling multiple definitions in a single one which is pretty ugly to me. It's like `When I open the door And I close the door Then I open the door again Then I see nothing` in a single step rather than in 4 lines. If I was you I would refactor whole thing. It is ugly, not readable, not user friendly, eye watering, you name it! – BentCoder Aug 15 '15 at 08:26
  • I get that it is ugly, but I'm also having trouble retaining all the header and post values from one step definition to the next--which is necessary. For every customized step it appears to be resetting the shared public variables. Maybe that would have been a better question. – Nabsta Aug 17 '15 at 13:38

1 Answers1

0

For every customized step it appears to be resetting the shared public variables.

It shouldn't happen!

You can separate all those lines like below, store data in variables and process them at the end so you don't have to do all in one go. If one of them must run in the middle then you can.

Some examples:

http://www.inanzzz.com/index.php/post/ajqn/api-request-response-testing-with-behat-v2-includes-json-xml-html-and-cli

http://www.inanzzz.com/index.php/post/xw1v/api-request-response-testing-with-behat-v1

EXAMPLE

FeatureContext

class FeatureContext extends MinkContext
{
    private $value;
    private $username;
    private $password;

    /**
     * @When /^I get a value "([^"]*)"$/
     */
    public function iGetValue($value)
    {
        $this->value = $value;
    }

    /**
     * @When /^I have the username "([^"]*)"$/
     */
    public function iHaveUsername($username)
    {
        $this->username = $username;
    }

    /**
     * @When /^I have the password "([^"]*)"$/
     */
    public function iHavePassword($password)
    {
        $this->password = $password;
    }

    /**
     * @When /^I send a "([^"]*)" request to "([^"]*)" with form data:$/
     */
    public function iSendRequestToWithFormData($method, $address, PyStringNode $requestPayload)
    {
        // Example is here: http://www.inanzzz.com/index.php/post/ajqn/api-request-response-testing-with-behat-v2-includes-json-xml-html-and-cli
        // @When /^I send a "([^"]*)" request to "([^"]*)"$/
    }

    /**
     * @When /^I have header data:$/
     */
    public function iHaveHeaderData(PyStringNode $requestPayload)
    {
        // Example is here: http://www.inanzzz.com/index.php/post/ajqn/api-request-response-testing-with-behat-v2-includes-json-xml-html-and-cli
    }

    /**
     * @When /^You run this step to do whatever you wanted to do with your steps above$/
     */
    public function iRun()
    {
        echo $this->value . PHP_EOL;
        echo $this->username . PHP_EOL;
        echo $this->password . PHP_EOL;
    }
}

Gherkin

  Scenario: I test things
    When I get a value "69"
    Then I have the username "hello"
    And I have the password "world"
    And I send a "POST" request to "www.google.com" with form data:
    """
    {
      "key1": "value1",
      "key2": "value2"
    }
    """
    And I have header data:
    """
    {
      "key1": "value1",
      "key2": "value2"
    }
    """
    Then You run this step to do whatever you wanted to do with your steps above

Result

Feature: Whatever

  Scenario: I test things
    When I get a value "69"
    Then I have the username "hello"
    And I have the password "world"
    And I send a "POST" request to "www.google.com" with form data:
      """
      {
        "key1": "value1",
        "key2": "value2"
      }
      """
    And I have header data:
      """
      {
        "key1": "value1",
        "key2": "value2"
      }
      """
69
hello
world
    Then You run this step to do whatever you wanted to do with your steps above

1 scenario (1 passed)
6 steps (6 passed)
0m0.857s
BentCoder
  • 12,257
  • 22
  • 93
  • 165