Laravel Dusk screenshot

只谈情不闲聊 提交于 2019-12-21 19:36:40

问题


I'm using laravel 5.6 and Dusk for running some tests.

I'm always taking my screenshot like this

...
use Facebook\WebDriver\WebDriverDimension;
...
class LoginTest extends DuskTestCase
{
    public function testLogin()
    {
        $user = User::first();

        $this->browse(function ($browser) use ( $user ) {
            $test = $browser->visit( new Login)
                    ->resize(1920,1080)                    
                    ...                
                    ->driver->takeScreenshot(base_path('tests/Browser/screenshots/testLogin.png'));
        });
    }
}

But as my tests will be more and more used, I don't want to continue to write everytime ->resize(X,Y) and base_path('bla/blab/bla').

I wanted to define the size and path for every tests that will be written.

I guess I should define some function in tests/DesukTestCase.php but I'm not even aware of how I can retrieve the driver and so on.

Have you got some guidance or documentation about this? Because I can't find anything...


回答1:


You only need to add '--window-size=1920,1080' in $options. This will apply a 1920x1080 screen resolution to all your Dusk tests. Feel free to adjust to whatever window size you want.

So your DuskTestCase.php file should look like this:

protected function driver()
{
    $options = (new ChromeOptions())->addArguments([
        '--disable-gpu',
        '--headless',
        '--window-size=1920,1080',
    ]);

    $driver = RemoteWebDriver::create(
        'http://selenium:4444/wd/hub',
        DesiredCapabilities::chrome()->setCapability(
            ChromeOptions::CAPABILITY,
            $options
        )
    );

}



回答2:


In my DuskTestCase file I have the below in my driver() function.

protected function driver()
{
    $options = (new ChromeOptions())->addArguments([
        '--disable-gpu',
        '--headless',
    ]);

    $driver = RemoteWebDriver::create(
        'http://selenium:4444/wd/hub',
        DesiredCapabilities::chrome()->setCapability(
            ChromeOptions::CAPABILITY,
            $options
        )
    );

    $size = new WebDriverDimension(1280, 2000);
    $driver->manage()->window()->setSize($size);

    return $driver;
}

You should just be able to configure it with the right dimensions you need.




回答3:


Regarding the path issue, you can set it with Browser::$storeScreenshotsAt in setUp method of your test case class.

protected function setUp()
{
    parent::setUp();
    Browser::$storeScreenshotsAt = '/path/to/your/screenshots';
}

Default location of Browser::$storeScreenshotsAt is set in setUp method of the grand parent test case class. So, make sure that you set Browser::$storeScreenshotsAt after calling parent::setUp(), otherwise it will be overwritten by the default.



来源:https://stackoverflow.com/questions/51613669/laravel-dusk-screenshot

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!