Fogarasi Norbert
Fogarasi Norbert

Reputation: 672

Global CSS selectors for Laravel Dusk tests

I am using Laravel Dusk to write Selenium tests for my project. I have a class called PrivilegeLevelsTest in my tests\Browser folder.

I am using in that class a lot of complex selectors like ->type('.gen-search-table[data-uri="table/userSearch"] input', $this->lettersToSearch)

I've seen in the documentation that these selectors can be declared at the Page class, in the siteElements method, but these declarations can only be seen from the Pages folder. Since I don't want to put my test there, I am asking if there is an alternative method to see these declarations from the outside?

My Page.php looks like this:

<?php

namespace Tests\Browser\Pages;

use Laravel\Dusk\Page as BasePage;

abstract class Page extends BasePage
{
 /**
 * Get the global element shortcuts for the site.
 *
 * @return array
 */
public static function siteElements()
{
    return [
        '@element' => '#selector',
        '@search' => '.gen-search-table[data-uri="table/userSearch"] input'
    ];
}
}  

And I want to use @search like this:

     public function testAdminCanListUsers()
     {
     $this->browse(function(Browser $browser) {
        $browser->loginAs($this->user)
            ->visit('/profile')
            ->pause(3000)
            ->type('@search', $this->lettersToSearch)
            ->pause(3000)
            ->press('Show All')
            ->assertSee($this->userToSearch->user_name);
        });
     }

Upvotes: 0

Views: 720

Answers (1)

Jonas Staudenmeir
Jonas Staudenmeir

Reputation: 25906

You have to create your own Page class:

class MyPage extends Page {

}

Then use it with on():

$browser->loginAs($this->user)
    ->visit('/profile')
    ->pause(3000)
    ->on(new MyPage)
    ->type('@search', $this->lettersToSearch)
    ->pause(3000)
    ->press('Show All')
    ->assertSee($this->userToSearch->user_name);

Upvotes: 1

Related Questions