Search code examples
phplaravelbehatmink

Behat\Laravel can't find submit


I'am trying to test my Laravel app by Behat. There is a homepage with fields for name, email and "Register" button. I'm trying to press button in behat-test, but I have an error Fatal error: Call to a member function press() on null (Behat\Testwork\Call\Exception\FatalThrowableError)

My HTML:

<html>
    <head>
    </head>
    <title>Registration</title>
    <body>
        <form action="/thanks" name="register">
            <p align="center"><font size="4"><b>Please, enter your name and e-mail</b></font></p>
            <p align="center"><input name="name" type="text" value="name"></p>
            <p align="center"><input name="email" type="text" value="e-mail"></p>
            <p align="center"><input name="registerButton" type="submit" value="Register"></p>
            <p>&nbsp;</p>
        </form>
    </body>
</html>

My FeatureContext function:

/**
 * @When I press the :submit
 */

public function iPressTheSubmit($submit) {
    $element=$this->getSession()->getPage()->findButton($submit);     
    $element->click();
}

Scenario:

Scenario: Register Test
Given I am on the homepage
When I press the "registerButton"
Then I should be on "/thanks"

Output:

Scenario: Register Test # features/regpage.feature:4 Given I am on the homepage # FeatureContext::iAmOnHomepage() When I press the "registerButton" submit # FeatureContext::iPressTheSubmit() Fatal error: Call to a member function press() on null (Behat\Testwork\Call\Exception\FatalThrowableError) Then I should be on "/thanks" # FeatureContext::assertPageAddress() --- Failed scenarios: features/regpage.feature:4 1 scenario (1 failed) 3 steps (1 passed, 1 failed, 1 skipped)

behat.yml: default: extensions: Laracasts\Behat: # env_path: .env.behat Behat\MinkExtension: default_session: laravel base_url: localhost:8000 laravel: ~


Solution

  • I fixed it with 2 steps: 1. Setting APP_KEY parameters in .env and .env.behat files 2. Fixing FeatureContent file like this:

     /**
     * @When I press the :arg1
     */
    public function iPressThe($arg1)
    {
        $element=$this->getSession()->getPage()->find('named', array('id_or_name', $arg1));
        $element->press();
    }