5

I was writing a test with Laravel Dusk to make sure there are, let's say, 10 articles on the index page.

Here is some HTML code:

<h1>A list of articles</h1>
<ul>
    @foreach($articles as $article)
        <li class="article-item"> {{ $article->title }}</li>
    @endforeach
</ul>

I was thinking that in my test file I could firstly select all li.article-item then assert that there are 10 such elements.

The code might be:

$this->browse(function ($browser) {
    $browser->visit('/articles')
        ->assertSee('A list of articles');
    // no such method
    $elements = $browser->findElements('.article-item');

    $this->assertCount(10, $elements);

});

But in fact, it looks like Laravel Dusk doesn't support choosing multiple elements at once. So in this case how may I structure my test? Any chance that within Dusk I use WebDriver API to select those elements?

Thank you!

Carter
  • 1,230
  • 13
  • 24

1 Answers1

3

It turns out that a $browser instance stores the driver it is using, which means in a test method I can actually choose HTML element via WebDriver API.

So a possible solution could be

/** @test */
public function it_displays_a_list_of_articles()
{
    $this->browse(function ($browser) {
        $browser->visit('/articles');
        $elements = $browser->driver->findElements(WebDriverBy::className('article-item'));
        $this->assertCount(10, $elements);
    });
}
Carter
  • 1,230
  • 13
  • 24