0

Trying to create a functional test using PHP with Behat and I want a random string for an email, but I want the string to be random every time a new test runs, but I want to pass the same string created to different tests as a parameter.

So if I generate a 10 digit string, I would like to generate the string and then pass it though other tests as the same 10 digit sequence

I'm new to PHP so I'm not exactly positive how to set it up, but here's what I have so far in my Behat context file

class FeatureContext implements Context {

private $MailPage;
private $registrationpage;


/**
 * Initializes context.
 *
 * Every scenario gets its own context instance.
 * You can also pass arbitrary arguments to the
 * context constructor through behat.yml.
 */
public function __construct(MailPage $mailPage, RegistrationPage $registrationpage)
{
    // Page obects injected directly in constructor with type hints.
    $this->MailPage = $MailPage;
    $this->registrationpage = $registrationpage;
}

/**
 * @BeforeSuite
 */
public static function  generateRandomString() {

    // Generate random string for Email implementation.
    $randomString = bin2hex(openssl_random_pseudo_bytes(10));
    return $randomString;

}


/**
 * @Given /^I register a temporary email address $/
 */
public function iRegisterATemporaryEmailAddress()
{
    $this->MailPage->grabNewEmail(self::generateRandomEmail());
}

/**
 * @Given /^I register a Developer account$/
 */
public function iRegisterADeveloperAccount()
{

    $this->registrationpage->fillInFormDetails(self::generateRandomEmail());
}

The Problem I run into is that with the parameter as it is, It generates a different string every time it's called, but I only want it to generate it once for the entire suite. Any ideas?

1
  • Not completely sure I’m understanding the big picture, but you could assign $this->randomString in the constructor, and then simply reference the randomString property instead of the generateRandomString method Commented Mar 4, 2018 at 1:35

1 Answer 1

1

1- Call your method from the constructor.

2- Save the generated value in a variable using this

private $randomString ;
public function __construct(MailPage $mailPage, RegistrationPage $registrationpage)
{
   //your code
   $this->randomString = $this->generateRandomString();

}

3- To use this variable you can call it inside your class methods like this $this->randomString.

Classes which have a constructor method call this method on each newly-created object, so it is suitable for any initialization that the object may need before it is used.

Sign up to request clarification or add additional context in comments.

1 Comment

That worked perfectly, just recently moved from Java to php so learning how to handle the differences give me a complete brain fart sometimes. Thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.