Framework: Testing: click method doesn't work with inner HTML

Created on 16 Nov 2015  路  4Comments  路  Source: laravel/framework

Hello!

I have an application that I want to unit test but the click() method is not working.

Here is the code of the link I want the Test to click:

<a href="//localhost:3000/events/49" class="btn btn-primary btn-xs" data-toggle="tooltip" title="" data-original-title="View">
    <i class="fa fa-eye"></i>
</a>

And here is the code in my test:

$this
    ->actingAs(User::find(1))
    ->visit(route('events.index'))
    ->click('<i class="fa fa-eye"></i>')
    ->seePageIs(route('events.show', $event->event_type))
    ->see($event->type->event);

The error I get back is as follows:

Symfony\Component\CssSelector\Exception\SyntaxErrorException: Expected selector, but <delimiter "#" at 1> found.

/home/vagrant/metrics/vendor/symfony/css-selector/Exception/SyntaxErrorException.php:34
/home/vagrant/metrics/vendor/symfony/css-selector/Parser/Parser.php:281
/home/vagrant/metrics/vendor/symfony/css-selector/Parser/Parser.php:140
/home/vagrant/metrics/vendor/symfony/css-selector/Parser/Parser.php:116
/home/vagrant/metrics/vendor/symfony/css-selector/Parser/Parser.php:51
/home/vagrant/metrics/vendor/symfony/css-selector/XPath/Translator.php:297
/home/vagrant/metrics/vendor/symfony/css-selector/XPath/Translator.php:123
/home/vagrant/metrics/vendor/symfony/css-selector/CssSelector.php:94
/home/vagrant/metrics/vendor/symfony/dom-crawler/Crawler.php:658
/home/vagrant/metrics/vendor/laravel/framework/src/Illuminate/Foundation/Testing/InteractsWithPages.php:756
/home/vagrant/metrics/vendor/laravel/framework/src/Illuminate/Foundation/Testing/InteractsWithPages.php:563
/home/vagrant/metrics/tests/EventsControllerTest.php:31

My application is running Laravel 5.1.24 and PHPUnit 4.8.18

Most helpful comment

The click() method searches for a <a> link that either contains a text, an id= attribute or a name= attribute that matches the string you pass as a parameter.

So, consider this piece of HTML:

<a id="go-to-foobar" name="link-to-foobar">
   <span class="neat-styling">Let's go to Foobar land!</span>
</a>

You have three ways to click the link:

->click("go-to-foobar");
->click("link-to-foobar");
->click("Let's go to Foobar land!");

All 4 comments

The click() method searches for a <a> link that either contains a text, an id= attribute or a name= attribute that matches the string you pass as a parameter.

So, consider this piece of HTML:

<a id="go-to-foobar" name="link-to-foobar">
   <span class="neat-styling">Let's go to Foobar land!</span>
</a>

You have three ways to click the link:

->click("go-to-foobar");
->click("link-to-foobar");
->click("Let's go to Foobar land!");

So I have to give the button an ID or put some text between it? I can't do any of these options as these buttons are in a table cell on my page and repeated for each row in the table.

If this is the only way to click the button then I think this code needs to be revised in the framework to allow for the same button being looped onto the page (for example, specifying the first button that appears with a certain class)

I ran into the same issue. I put an id inside the table row a tag, it didn't work for me.
Hello World

Was this page helpful?
0 / 5 - 0 ratings

Related issues

shopblocks picture shopblocks  路  3Comments

YannPl picture YannPl  路  3Comments

SachinAgarwal1337 picture SachinAgarwal1337  路  3Comments

RomainSauvaire picture RomainSauvaire  路  3Comments

PhiloNL picture PhiloNL  路  3Comments