DerJacques's avatar

Laravel 5.1: Test unable to locate submit button

Dear Laravel Experts

I'm currently trying to do some very basic testing. More specifically, I'm using Laravel 5.1's testing facilities to test the successfull submission of a form.

Unfortunately, I receive the following error:

InvalidArgumentException: Could not find a form that has submit button [register].

The relevant portion of my form looks like this:

<form role="form" method="POST" action="/auth/register">
    <input type="hidden" name="_token" value="{{ csrf_token() }}">
    <input type="text" name="name" value="{{ old('name') }}">
    <input type="submit" value="register">
</form>

My test looks like this:

$this->visit('/auth/register')
            ->type('Test User', 'name')
            ->press('register');

Any idea, why this error may occur?

I actually tried to copy and paste the "Working With Forms" example from the Laravel documentation, but experience the same error: InvalidArgumentException: Could not find a form that has submit button [register].

I assume that this is related to the Symfony DomCrawler component, but cannot figure out where I'm failing.

Any help is very much appreciated! :-) Thanks.

0 likes
10 replies
lordvater's avatar

Hi, had the same error while developing on two different systems. For me it was the $baseUrl, in the TestCase Class, which was different in both cases.

1 like
DerJacques's avatar

Thank you so much for both your replies! @lordvater 's suggestion did solve my issue. I simply had to change the $baseUrl variable. :-)

I still don't really understand, how this solved the issue, but it definitely did, so thanks a bunch.

ChrisSFR's avatar

I have the same issue with:

<input type="submit" value="Add">

And my test:

$this->visit('/tag/create')
            ->type('Tag1', 'name')
            ->press('Add')
            ->seePageIs('/tag');

I'm using Homestead. I changed my TestCase $baseUrl to everything possible: localhost:8000, localhost:80, test.app, 192.168.10.10 but I still get:

InvalidArgumentException: Could not find a form that has submit button [Add].

Any help?

ChrisSFR's avatar

Ok , my fault... auth middleware was active :P

JuanjoLainez's avatar

So what did you put at $baseUrl? It works if I put my development environment, but I read in a Laracast video that since we are using the integration package, there's no need to set it.

What do you think about this?

glockops's avatar

For me I was using $this->get(...) rather than $this->visit(...) prior to calling the formSubmit method. After updating to $this->visit(...) the form button was found.

MarceloG's avatar

I have a HTML5 button like this:

<button type="submit" class="btn btn-primary" title="Ingresar">Ingresar</button>

The visit method is called, and the page has the element, but is not founded

1 like
codebyjeff's avatar

I'll add my own findings:

The element requires a "name" attribute; the "value" shown in examples doesn't work for me. So:

<input type="submit" name="register" value="register">

lk92's avatar

i have the same issue in my laravel 5.2 application

when try using either press() or fillForm() for filling a form, submitForm for submitting a form but keeps getting this error:

    InvalidArgumentException: Could not find a form that has submit button [Gem]

i have tried to change the $baseUrl variable, and added a name attribute to the button i want to press in my test but no hope so far :(

ale's avatar

Instead.. my mistake was that press() method is CaseSensitive

Please or to participate in this conversation.