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

拜拜、爱过 提交于 2021-02-07 20:31:42

问题


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');
    });
}

回答1:


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')


来源:https://stackoverflow.com/questions/44283669/how-to-handle-multiple-elements-with-the-same-class-in-laravel-dusk

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!