laravellaravel-10laravel-dusk

Laravel Dusk - How to click to each items of a list?


I've a page with a list of links.

I could access these links using class selector '.promotional-item-link'

How can I click to loop throught all the links, click to each item and test if there is a specific [identical for all] string on all of these page?

How can I


Solution

  • The following code works:

    public function testBasicEx(): void
    {
        
        $this->browse(function (Browser $browser) {
    
            $links=$browser->visit('/')->elements('.promotional-item-link');
         
            $linkstext=[];
    
            foreach ($links as $key=>$elem) {
                array_push($linkstext,$elem->getText());
            }
    
            foreach ($links as $key=>$elem) {
                $browser->visit('/')->clickLink($linkstext[$key])->assertSee('ciao');
            }
       
        });
    }
    

    The fist foreach seems unnecessary, but extracting the link text directly in the second foreach failed after the first iteration (i do not know why)