Laravel Dusk 截图
Laravel Dusk screenshot
我正在使用 laravel 5.6
和 Dusk
进行 运行 一些测试。
我总是这样截屏
...
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')
。
我想为将要编写的每个测试定义 大小 和 路径。
我想我应该在 tests/DesukTestCase.php
中定义一些函数,但我什至不知道如何检索驱动程序等等。
你有关于这方面的指导或文档吗?因为我找不到任何东西...
在我的 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;
}
您应该能够使用您需要的正确尺寸配置它。
您只需在$options
中添加'--window-size=1920,1080'
。这将为所有 Dusk 测试应用 1920x1080 屏幕分辨率。随意调整到您想要的 window 大小。
因此您的 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
)
);
}
关于路径问题,您可以在测试用例class.
的setUp
方法中用Browser::$storeScreenshotsAt
设置
protected function setUp()
{
parent::setUp();
Browser::$storeScreenshotsAt = '/path/to/your/screenshots';
}
Browser::$storeScreenshotsAt
的默认位置设置在 setUp
method of the grand parent test case class 中。
所以,一定要在调用parent::setUp()
后设置Browser::$storeScreenshotsAt
,否则会被默认覆盖
我正在使用 laravel 5.6
和 Dusk
进行 运行 一些测试。
我总是这样截屏
...
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')
。
我想为将要编写的每个测试定义 大小 和 路径。
我想我应该在 tests/DesukTestCase.php
中定义一些函数,但我什至不知道如何检索驱动程序等等。
你有关于这方面的指导或文档吗?因为我找不到任何东西...
在我的 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;
}
您应该能够使用您需要的正确尺寸配置它。
您只需在$options
中添加'--window-size=1920,1080'
。这将为所有 Dusk 测试应用 1920x1080 屏幕分辨率。随意调整到您想要的 window 大小。
因此您的 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
)
);
}
关于路径问题,您可以在测试用例class.
的setUp
方法中用Browser::$storeScreenshotsAt
设置
protected function setUp()
{
parent::setUp();
Browser::$storeScreenshotsAt = '/path/to/your/screenshots';
}
Browser::$storeScreenshotsAt
的默认位置设置在 setUp
method of the grand parent test case class 中。
所以,一定要在调用parent::setUp()
后设置Browser::$storeScreenshotsAt
,否则会被默认覆盖