Yassine Elhamraoui
Yassine Elhamraoui

Reputation: 155

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 ?

Upvotes: 4

Views: 9985

Answers (2)

Nawfal Serrar
Nawfal Serrar

Reputation: 2263

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);

Upvotes: 6

qooplmao
qooplmao

Reputation: 17759

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.

Upvotes: 2

Related Questions