Laravel 5.1 PHPUnit - press() gives me 'Unreachable field ""' Laravel 5.1 PHPUnit - press() gives me 'Unreachable field ""' laravel laravel

Laravel 5.1 PHPUnit - press() gives me 'Unreachable field ""'


I'm not sure what the exact cause of your problem is but it could be a couple of things.I would have commented but I don't have enough reputation yet.

Firstly, have you run your plain html trough a validator? If it gives you any syntax errors try to fix them and run the unit test again. (You can try this one https://validator.w3.org/, just copy and paste your html)

Another cause may be that you have some javascript that modifies your DOM and Laravel testing can't handle that.

And lastly, you can try to set the 'value' attribute of your <button> to "Create"

<button class="btn btn-lg btn-primary" type="submit" name="submit" value="Create">Create</button>

Or change the <button> to an <input> with type attribute "submit" and value to "Create"

<input type="submit" class="btn btn-lg btn-primary" name="submit" value="Create" />


From experience, unreachable field errors are normally caused by the buttons being outside of the <form> element, i've got a similair scenario where another section of the page is passed to the controller via $request but because it's not within the same immediate form element as everything else, PHPUnit can't assosciate it with the form.

It could be worth changing the method from press() to click() which just looks for something it can click on in general rather than attempting to match a submit button to the form.

As mentioned above as well, PHPUnit can't test JavaScript by itself (if you're using Javascript of course), you need something like Selenium to do that.

Without the markup it's quite hard to say, could you post the markup of your form?


The previous suggestions do not cover the business need of having the two submit buttons with same value.

What we need to do is, override the default Laravel XPath expression.

Instead of finding the first instance of the second submit button with the text, we'll tell Laravel to find the element with specific XPath of ours (the second submit button).

    $this->visit(route('user.login'));    // replace `login-btn-2` with the id of your second button    $button = $this->crawler->filterXPath('//*[@id="login-btn-2"]');     $form = $button->form($inputs = ['email' => $user->email, 'password' => $password,]);    $this->makeRequestUsingForm($form);    $this->see(route('user.loggedin.dashboard'));

Extra credit If you're new to XPath, you can right click on the inspected source code on Chrome browser. The option you want is Copy > Copy as XPath