Laravel Dusk屏幕截图

时间:2018-07-31 12:41:46

标签: laravel laravel-dusk

我正在使用laravel 5.6Dusk来运行一些测试。

我总是像这样拍摄我的屏幕截图

...
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'));
        });
    }
}

但是随着我的测试越来越多地使用,我不想每次都->resize(X,Y)base_path('bla/blab/bla')继续写。

我想为将要编写的每个测试定义 size path

我想我应该在tests/DesukTestCase.php中定义一些函数,但是我什至不知道如何获取驱动程序等等。

您有关于此的一些指导或文档吗?因为我什么都找不到...

3 个答案:

答案 0 :(得分:3)

在我的DuskTestCase文件中,我的driver()函数包含以下内容。

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;
}

您应该能够根据需要配置正确的尺寸。

答案 1 :(得分:0)

您只需要在'--window-size=1920,1080'中添加$options。这会将1920x1080屏幕分辨率应用于您的所有Dusk测试。随意调整为所需的任何窗口大小。

因此您的DuskTestCase.php文件应如下所示:

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 :(得分:0)

关于路径问题,您可以在测试用例类的Browser::$storeScreenshotsAt方法中使用setUp进行设置。

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

setUp method of the grand parent test case class中设置了Browser::$storeScreenshotsAt的默认位置。 因此,请确保在调用Browser::$storeScreenshotsAt之后设置parent::setUp(),否则默认情况下它将被覆盖。