4

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 ?

2 Answers2

6

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);
Community
  • 1
  • 1
Nawfal Serrar
  • 2,213
  • 1
  • 14
  • 22
  • yep that's what i want to test thanks for the reply. but i keep getting : the current node list is empty whent i try to select the submit input – Yassine Elhamraoui Mar 20 '15 at 09:47
  • var_dump the $crawler->html() after you made the request to see if the page is correct. then if the form have a button tag with the id, the selector 'button#yourbuttonid' should work (the button should be inside the form) – Nawfal Serrar Mar 20 '15 at 09:59
  • yep It wasn't the page i was hoping for, I think I understand now the problem when I try to acces my route i get redirected to login form. thanks a lot you really helped, i'll try do the complete scenario now from the login form to the end. – Yassine Elhamraoui Mar 20 '15 at 10:15
  • just a last question, I'm working in the windows console when i var_dum the html like you suggested it only displays a few lines from the html page then it display ..., well in some pages i have the same structure so i don't know if i was really redirected or not. My question is there some way that i can force the var_dump to display the whole html page ? – Yassine Elhamraoui Mar 20 '15 at 11:24
  • hum you can try var_export($crawler->html(), true) or print_r($crawler->html(), true) , never tested it so i am not sure why it doesn't return the whole html but try this it might help. – Nawfal Serrar Mar 20 '15 at 18:55
  • btw @YassineElhamraoui here is how to dump the whole html from a crawler $html = $crawler->getNode(0)->ownerDocument->saveHTML(); the html function return only the first node.. looking at the html() function of the crawler it was easy to understand why lol never looked into it.. they should have given it another name – Nawfal Serrar Apr 08 '15 at 02:32
2

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.

qooplmao
  • 17,622
  • 2
  • 44
  • 69
  • thanks @Qoop for the reply, I just tested your code and I think it's not working I get the following error : _LogicException: Unable to click on a "button" tag._ it seems though that I can acces the hidden form without clicking the add button so I don't know if it's a good idea not to test the functionning of that button or just skip it – Yassine Elhamraoui Mar 20 '15 at 11:33
  • Is it part of a form that submits or is it just a random button? If it's a form then it might be better to select the button and then select the form that it is a part of and submit that. Alternatively it might make sense to change the button to an anchor that you can listen to, or would reload the page with a `$_GET` if you had javascript disabled/unavailable. – qooplmao Mar 20 '15 at 11:36
  • 1
    No the form is actualy hidden so it's not part of a form, it's a button that shows the form then a have the actual submit button of the form – Yassine Elhamraoui Mar 20 '15 at 11:51
  • Would your system still be able to work with reloading the page and showing the form if the user had javascript disabled or turned off? – qooplmao Mar 20 '15 at 12:24