jedrek cienicki
jedrek cienicki

Reputation: 153

Behat Pass a value from a test step

I'm trying to make assertion that the random text entered in one field appears on next page (confirmation)

I do it like this

When I fill in "edit-title" with random value of length "8"

/**
     * Fills in form field with specified id|name|label|value with random string
     * Example: And I fill in "bwayne" with random value of length "length"
     *
     * @When /^(?:|I )fill in "(?P<field>(?:[^"]|\\")*)" with random value of length "(?P<length>(?:[^"]|\\")*)"$/
     */
    public function fillFieldWithRandomValue($field, $length)
    {
        $field = $this->fixStepArgument($field);
        $value = $this->generateRandomString($length);
        $this->getSession()->getPage()->fillField($field, $value);
    }

Than I want to make assertion - something like this:

Then I should see text matching "<RANDOM VALUE ENTERED IN THE PREVIOUS STEP>"

is it possible?

UPDATE:

But how would it look like with setters and getters if i want to use a generateRandomString method multiple times and then get the values of this methods one after another? DO I have to make variables and functions for every test step? like this:

When I fill in "x" with random value of length "8"
And I fill in "y" with random value of length "12"
And I go to other page
Then I should see text matching "VALUE ENTERED TO X"
And I should see text matching "VALUE ENTERED TO Y"

Upvotes: 0

Views: 1391

Answers (2)

lauda
lauda

Reputation: 4173

Since you will will call the generateRandomString method in multiple places then you should also have a method for getting this value like getRandomString like setters and getters.

My recommendation would be to have a class with related methods that handle all the data and not saving in variable in every place you will use data, generate+save and read from the same place anywhere you need.

Tip: You could be more flexible about the step definition and have a default length for the random string in case one one not provided.

High level example:

class Data
{

    public static $data = array();

    public static function generateRandomString($length = null, $name = null)
    {
        if ($name = null) {
            $name = 'random';
        };
        if ($length = null) {
            $length = 8;
        };
        // generate string like  $string =
        return self::$data[$name] = $string;
    }

    public static function getString($name = null)
    {
        if ($name = null) {
            $name = 'random';
        };
        // exception handling
        if (array_key_exists($name, self::$data) === false) {
            return null;
        }

        return self::$data[$name];
    }

}

In context:

/**
 * @Then /^I fill in "x" with random value as (.*?)( and length (\d+))?$/
 */
public function iFillInWithRandomValue($selector, $name, $length = null){
    $string = Data::generateRandomString($length, $name);

    // fill method
}

/**
 * @Then /^I should see text matching "first name"$/
 */
public function iShouldSeeTextMatching($variableName){
    $string = Data::getString($variableName);

    // assert/check method
}

This is high level example, you might need to do some adjustments.

If you have the validation in the same class then you can also have all these in the same class, meaning generateRandomString and getString in the same class with the steps.

Upvotes: 1

Igor Lantushenko
Igor Lantushenko

Reputation: 1791

You can create a property and set it in the previous step. And use it in the next one, but assert it if it has value. Also it would be nice and readable to define that property with proper visibility type

/**
 * @var string
 */
private randomString;

/**
 * Fills in form field with specified id|name|label|value with random string
 * Example: And I fill in "bwayne" with random value of length "length"
 *
 * @When /^(?:|I )fill in "(?P<field>(?:[^"]|\\")*)" with random value of length "(?P<length>(?:[^"]|\\")*)"$/
 */
public function fillFieldWithRandomValue($field, $length)
{
    $field = $this->fixStepArgument($field);
    $this->randomString = $this->generateRandomString($length);
    $this->getSession()->getPage()->fillField($field, $this->randomString);
}

/**
 *
 * @Then /^(?:|I )should see that page contains random generated text$/
 */
public function assertPageContainsRandomGeneratedText()
{
    //Assertion from phpunit
    $this->assertNotNull($this->randomString);

    $this->assertPageContainsText($this->randomString);
}

NOTE: Depending on your behat setup - assertion from phpunit might not work.

Upvotes: 1

Related Questions