Laravel 5.1: Find a button based on it's class / id
I have a page where I have several forms. Each forms have a submit button, with the text of Select. So I can't use Laravel's press method during the test, 'cause it accepts the button text.
Do you have any idea how to submit a form based on the submit button's class or id?
If you have multiple forms, why not have a different submit handler (defined by the 'action' attribute of the form tag) for each form?
Alternately, you could handle form submissions with javascript.
This fairly basic jquery snippet will get you most of the way there:
(function ($) {
$(document).ready(function () {
$(':submit').bind('click', function (event) {
event.preventDefault();
var clicked = $(this).attr('class'); // or $(this).attr('id');
// Now you know which was clicked, so
// Do stuff...
});
})(jQuery);
I think you'd be better off setting distinct form actions for each form though, and/or a hidden form element in each form.
@willvincent thank you for your comment. I'm terribly sorry, but my opening posting was really misleading.
I'm using Laravel's built-in helpers to write tests for the different part of my application.
<?php
use Illuminate\Foundation\Testing\DatabaseTransactions;
class AuthTest extends TestCase {
use DatabaseTransactions;
/** @test */
public function it_registers_a_user()
{
$overrides = ['email' => 'foo@example.com'];
$this->register($overrides)
->seeInDatabase('users', $overrides)
->seeInDatabase('user_meta', [
'user_id' => 1,
'meta_key' => 'first_name',
'meta_value' => 'John'
]);
}
}
So, I can't use the built-in helper methods (eg submitForm, press, etc), 'cause they are using the button text as an identifier.