【问题标题】:Laravel Dusk screenshotLaravel 黄昏截图
【发布时间】:2023-04-01 06:58:01
【问题描述】:

我正在使用 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')都继续写。

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

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

您有相关的指导或文档吗?因为我什么都找不到……

【问题讨论】:

    标签: laravel laravel-dusk


    【解决方案1】:

    在我的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;
    }
    

    您应该能够使用所需的正确尺寸对其进行配置。

    【讨论】:

    • 一切正常,你知道我的路径问题吗?
    • 保存路径在哪里?我只使用过默认位置
    • 默认情况下它存储在应用程序的根目录中。不是吗?反正我以后会想办法的
    • 在我的例子中,截图存储在./tests/Browser/screenshots
    【解决方案2】:

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

    所以你的 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
            )
        );
    
    }
    

    【讨论】:

      【解决方案3】:

      关于路径问题,您可以在测试用例类的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,否则会被默认覆盖。

      【讨论】:

        猜你喜欢
        • 2018-02-23
        • 1970-01-01
        • 1970-01-01
        • 2017-08-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-01-13
        相关资源
        最近更新 更多