Laravel Dusk 如何在执行测试时显示浏览器
Laravel Dusk how to show the browser while executing tests
我正在编写一些测试,我想看看 Dusk 是否正确填写了输入字段,但 Dusk 在 运行 测试时不显示浏览器,有没有办法强制它这样做?
在 tests\DuskTestCase.php
文件 driver()
函数中禁用无头模式:
$options = (new ChromeOptions)->addArguments([
//'--disable-gpu',
//'--headless'
]);
在 tests/DuskTestCase.php
文件的顶部附近,添加:
use Facebook\WebDriver\Chrome\ChromeOptions;
在同一文件中,将整个 driver()
函数替换为:
/**
* Create the RemoteWebDriver instance.
*
* @return \Facebook\WebDriver\Remote\RemoteWebDriver
*/
protected function driver() {
$options = (new ChromeOptions)->addArguments([
//'--disable-gpu',
//'--headless'//
]);
return RemoteWebDriver::create(
'http://localhost:9515', DesiredCapabilities::chrome()->setCapability(
ChromeOptions::CAPABILITY, $options
)
);
}
更新(2021 年):
您可以使用 2 种方法禁用 headless:
方法 1:将此添加到您的 .env
DUSK_HEADLESS_DISABLED=true
方法 2:如果您不需要为所有测试显示浏览器,请将此添加到您的特殊测试用例
protected function hasHeadlessDisabled(): bool
{
return true;
}
顺便说一句,我不知道为什么文档中没有提到这些。以上方法是我自己从DuskTestCase.php.
中找到的
我正在编写一些测试,我想看看 Dusk 是否正确填写了输入字段,但 Dusk 在 运行 测试时不显示浏览器,有没有办法强制它这样做?
在 tests\DuskTestCase.php
文件 driver()
函数中禁用无头模式:
$options = (new ChromeOptions)->addArguments([
//'--disable-gpu',
//'--headless'
]);
在 tests/DuskTestCase.php
文件的顶部附近,添加:
use Facebook\WebDriver\Chrome\ChromeOptions;
在同一文件中,将整个 driver()
函数替换为:
/**
* Create the RemoteWebDriver instance.
*
* @return \Facebook\WebDriver\Remote\RemoteWebDriver
*/
protected function driver() {
$options = (new ChromeOptions)->addArguments([
//'--disable-gpu',
//'--headless'//
]);
return RemoteWebDriver::create(
'http://localhost:9515', DesiredCapabilities::chrome()->setCapability(
ChromeOptions::CAPABILITY, $options
)
);
}
更新(2021 年):
您可以使用 2 种方法禁用 headless:
方法 1:将此添加到您的 .env
DUSK_HEADLESS_DISABLED=true
方法 2:如果您不需要为所有测试显示浏览器,请将此添加到您的特殊测试用例
protected function hasHeadlessDisabled(): bool
{
return true;
}
顺便说一句,我不知道为什么文档中没有提到这些。以上方法是我自己从DuskTestCase.php.
中找到的