Search code examples
phplaravellaravel-dusk

How to scroll down browser page - Laravel Dusk (Browser Tests)


I'm preparing tests using [Browser Tests (Laravel Dusk)][1]

[1]: https://laravel.com/docs/5.4/dusk and I need force click to element which is not see before scroll down browser page. How can define in dusk test to click unsee element or scroll browser page ?

class SliderTest extends DuskTestCase
{
    /**
     * A Dusk test example.
     *
     * @return void
     */
    public function testExample()
    {
        $this->browse(function ($browser) {
            $browser
                    ->visit('http://localhost:8000/admin/login')                    
                    ->click('label[for=test_1]')
                    ->pause(500)
                ;
        });
    }
}

Solution

  • Based on the answer from @james

    you can execute scripts, but these cannot be chained. So you can just execute the scroll before the click occurs.

    public function testExample()
    {
        $this->browse(function ($browser) {
            $browser
                    ->visit('http://localhost:8000/admin/login')
                    ->driver->executeScript('window.scrollTo(0, 500);'); 
                    // can't chain methods after this
            $browser                    
                    ->click('label[for=test_1]')
                    ->pause(500) //you can keep chaining here;
        });
    }