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

51 views Asked by At

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

  • extract items based on a class
  • loop through each one to do a ->visit and an ->assert ?
1

There are 1 answers

0
suxgri On BEST ANSWER

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)