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>
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.
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.
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.
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.
You can target the href like this:
->click('a[href="/somewhere"]')
This is a bit hacky, but it's what I've come up with as a workaround.
Put an id selector on the link.
<a id="link-click-me" href="/somewhere">
<i class="fa fa-plus" aria-hidden="true"></i>
</a>
Assert it's visible.
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');
});
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With