Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle multiple elements with the same class in laravel dusk

In my dusk test I want to firstly add and then delete news. Each news has .delete-news class but on the screen I have multiple elements. Each .delete-news class has in it's path data attribute with it's id data-newsid="id". Now the browser does not know which delete-news class it should to click. How should I manage that?

Probably I should take delete-news class with the biggest data-newsid attribute. But I don't know how I should check it.

Currently I'm deleting it like this:

public function testRemoveNews() {
    $this->browse(function ($browser) {
        $browser->visit('/')
                ->press('.delete_news')
                ->press('Yes')
                ->waitForText('News has been deleted!')
                ->press('OK')
                ->assertDontSee('Title of the news');
    });
}
like image 793
Martin Avatar asked Nov 02 '25 01:11

Martin


1 Answers

If you sorting your news by 'id' desc, maybe you should try

->press('.delete-news:first') // or :nth-child(1)

or add a dusk attribute to first news element, like dusk="last-news", and call it with:

->press('@last-news')
like image 152
Daniel Avatar answered Nov 03 '25 17:11

Daniel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!