Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to click link icon with laravel dusk?

If I have a link:

<a href="/somewhere">Click Me</a>

I know I can clickLink based on its text.

public function testCanClickLink()
{
    $this->browse(function ($browser) {
        $browser->visit('/welcome')
                ->clickLink('Click Me');
    });
}

But how can I click an icon link?

<a href="/somewhere">
    <i class="fa fa-plus" aria-hidden="true"></i>
</a>
like image 307
Jeff Puckett Avatar asked Feb 26 '17 16:02

Jeff Puckett


People also ask

What is dusk in Laravel?

Laravel Dusk provides an expressive, easy-to-use browser automation and testing API. By default, Dusk does not require you to install JDK or Selenium on your machine. Instead, Dusk uses a standalone ChromeDriver installation. However, you are free to utilize any other Selenium compatible driver you wish.

How does the clicklink method work with dusk?

The clickLink method will click the link that has the given display text: {note} This method interacts with jQuery. If jQuery is not available on the page, Dusk will automatically inject it into the page so it is available for the test's duration.

What is elementnotfound exception in Laravel Dusk?

If you try to interact with elements on an Iframe within your Laravel Dusk you might come across ElementNotFound Exception. This is because Iframe page source different from your main page source.

How to use dusk to navigate to a new page?

Once you have generated the new page and defined the url inside the url () method of the page. Here is how you can visit the page use Tests\Browser\Pages\Checkout; $browser->visit (new Checkout); With this dusk will navigate to the url defined in the url () method and will also run the assert () method if you have defined in the Page class.


2 Answers

You can target the href like this:

->click('a[href="/somewhere"]')

like image 163
Andrew Bibby Avatar answered Oct 26 '22 22:10

Andrew Bibby


This is a bit hacky, but it's what I've come up with as a workaround.

  1. Put an id selector on the link.

    <a id="link-click-me" href="/somewhere">
        <i class="fa fa-plus" aria-hidden="true"></i>
    </a>
    
  2. Assert it's visible.

  3. Get the href attribute.
  4. Visit it.
  5. Assert path is correct.

    public function testCanClickLink()
    {
        $this->browse(function ($browser) {
            $browser->visit('/welcome')
                    ->assertVisible('#link-click-me')
                    ->visit(
                        $browser->attribute('#link-click-me', 'href')
                    )
                    ->assertPathIs('/somewhere');
        });
    }
    
like image 36
Jeff Puckett Avatar answered Oct 26 '22 23:10

Jeff Puckett