Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to click on a button in PHPUnit (Symfony2)

Hi, I'm writing a functional test and I want to know how to perform a simple click on a button, I have a hidden form that is shown after the button click

I tried doing a normal click like that :

$button  = $crawler->filter('button:contains("Add")');
$crawler = $client->click($button);

but it seems the click() function take a Link Object not a Crawler Object.

how can I do something like that ?

like image 377
Yassine Elhamraoui Avatar asked Feb 10 '23 15:02

Yassine Elhamraoui


2 Answers

I assume you use JS to show your hidden form. This will not work since the crawler doesn't support JS, you better look up CasperJs or some other crawler if you want to test the click and visibility of your form.

Symfony2 Functional Testing - Click on elements with jQuery interaction

Otherwise if testing the submit of the form is what you want to achieve, then you can use :

$form = $crawler->filter('button#idofyourbutton')->form(array(
            'firstname' => 'Blabla',
            'lastname' => 'Blabla',
            'address' => 'BlablaBlablaBlablaBlabla',
            'zipcode' => '302404',
            'phone' => '30030130269'
        ),'POST');

        $client->submit($form);
like image 86
Nawfal Serrar Avatar answered Feb 13 '23 05:02

Nawfal Serrar


From the docs it says to convert to a link object you would do the following

$button = $crawler
    ->filter('button:contains("Add")') // find all buttons with the text "Add"
    ->eq(0) // select the first button in the list
    ->link() // and click it
;

And then you can click it like before..

$crawler = $client->click($button);

I haven't used this though so I'm not sure if it would work with a button.

like image 45
qooplmao Avatar answered Feb 13 '23 04:02

qooplmao