LoginSignup
0
1

More than 3 years have passed since last update.

Laravel Duskをローカル内のSelenium経由で使ってみる

Posted at

Laravel Duskとは?

Laravel Dusk 6.x Laravel

ブラウザ自動操作 + ブラウザ用テストAPIをLaravel内のコードで実行できるテスト用ライブラリ
スクリーンショットも取れるらしい <- 今回はやらない

事前にいるもの (前提)

  • MacOS (自分がMacOSで動かしたので)
  • Homebrew
  • Laravel(v6.*)

セットアップ

chromedriverのインストール

brew install chromedriver

Seleniumのインストール

brew install selenium-server-standalone

### .env の修正
APP_URLの行を以下のように変更

.env
APP_URL=http://localhost:8000

パーミッション変更

$ chmod 755 vendor/laravel/dusk/bin/*

Laravel Dusk のインストール

$ composer require --dev laravel/dusk
$ php artisan dusk:install

tests/DuskTestCase.phpを以下のように変更する

tests/DuskTestCase.php
<?php

namespace Tests;

use Facebook\WebDriver\Chrome\ChromeOptions;
use Facebook\WebDriver\Remote\DesiredCapabilities;
use Facebook\WebDriver\Remote\RemoteWebDriver;
use Laravel\Dusk\TestCase as BaseTestCase;

abstract class DuskTestCase extends BaseTestCase
{
    use CreatesApplication;

    /**
     * Prepare for Dusk test execution.
     *
     * @beforeClass
     * @return void
     */
    public static function prepare()
    {
        // static::startChromeDriver();
    }

    /**
     * Create the RemoteWebDriver instance.
     *
     * @return \Facebook\WebDriver\Remote\RemoteWebDriver
     */
    protected function driver()
    {
        $options = (new ChromeOptions)->addArguments([
            '--disable-gpu',
            '--headless',
            '--window-size=1920,1080',
        ]);

        return RemoteWebDriver::create(
            'http://localhost:4444/wd/hub',DesiredCapabilities::chrome()->setCapability(
                ChromeOptions::CAPABILITY, $options
            )
        );
    }
}

テストを実行してみる

テストを書いてみる

初期設定のままだとただテスト成功するだけで面白くないので、
明らかに失敗するであろうメソッドtestChallengeExample()tests/Browser/ExampleTest.phpに追加してみる

tests/Browser/ExampleTest.php
class ExampleTest extends DuskTestCase {
    ~~~
    public function testChallengeExample()
    {
        $this->browse(function (Browser $browser) {
            $browser->visit('/')
                ->assertSee('CakePHP');
        });
    }
}

selenium-server を起動

$ selenium-server

アプリケーションを起動

$ php artisan serve

Dusk実行

$ php artisan dusk
PHPUnit 9.5.4 by Sebastian Bergmann and contributors.

.F                                                                  2 / 2 (100%)

Time: 00:01.820, Memory: 18.00 MB

There was 1 failure:

1) Tests\Browser\ExampleTest::testChallengeExample
Did not see expected text [CakePHP] within element [body].
Failed asserting that false is true.

/Users/tk_zawa/laravel-app/vendor/laravel/dusk/src/Concerns/MakesAssertions.php:181
/Users/tk_zawa/laravel-app/vendor/laravel/dusk/src/Concerns/MakesAssertions.php:152
/Users/tk_zawa/laravel-app/tests/Browser/ExampleTest.php:28
/Users/tk_zawa/laravel-app/vendor/laravel/dusk/src/Concerns/ProvidesBrowser.php:68
/Users/tk_zawa/laravel-app/tests/Browser/ExampleTest.php:29

FAILURES!
Tests: 2, Assertions: 2, Failures: 1.

テストが無事実行 & (テスト内容として)失敗しましたね!

初回起動時...

スクリーンショット 2021-04-14 23.49.06.png

と出るので、一旦キャンセルを押す。

システム環境設定のセキュリティとプライバシー/一般を開いておく。

もう一度php artisan duskを実行。
そうすると、

スクリーンショット 2021-04-14 23.49.06.png

と共に、
システム環境設定の画面にこのまま許可ボタンが表示される。

スクリーンショット 2021-04-14 23.49.32.png

このまま許可を押し、アラートウィンドウはキャンセルを押す。

更にもう一度php artisan duskを実行。

スクリーンショット 2021-04-14 23.49.41.png

と表示される為、開くを押す。(これで完了)

追記

2021/04/15時点:

Laravel Dusk v6.15で要求されるchromedriverのバージョンはv90で、現時点でのChrome(ブラウザ)のバージョンはv89。
v89のChrome(ブラウザ)が入った状態では、Macに直接v90以上のchromedriverをインストールしても、
Laravel Duskを実行するとブラウザ版のドライバ(v89)が認識されて詰んでしまう為、Seleniumを経由することで回避
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1