How to Scroll Browser Page - Laravel Dusk (Browser Tests)

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

[1]: https://laravel.com/docs/5.4/dusk , and I need to force click an element that does not appear before scrolling the browser page. How to determine in the twilight test to click the unsee element or scroll the 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)
                ;
        });
    }
}
+6
source share
3 answers

Based on answer from @james

you can execute scripts, but they cannot be fettered. That way, you can simply scroll to a click.

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;
    });
}
+6
source

Now you can use the method script:

$browser->script('window.scrollTo(0, 500);');

+4

, , :

$browser->driver->executeScript('window.scrollTo(0, 500);');
+3

Source: https://habr.com/ru/post/1015933/


All Articles