amazon linux2(vagrant) + Laravel 7.xでDuskを使うまでの手順

Laravelでseleniumを使おうと思ったら、duskというパッケージがあるらしいので、duskをインストールして使っていきます。環境は、vagrant、amazon linux2, laravel 7.2.1です。

### 1.duskインストール
$ php composer.phar require –dev laravel/dusk
– laravel/dusk v5.10.0 requires ext-zip * -> the requested PHP extension zip is missing from your system.

$ sudo yum install php-pecl-zip.x86_64
$ php composer.phar require –dev laravel/dusk

$ php artisan dusk:install
testsディレクトリにBrowserディレクトリが作られる

.env

APP_URL=http://192.168.33.10:8000

### 2.duskテスト
$ php artisan dusk:make LoginTest

/tests/Browser/LoginTest.php

public function test_can_login_successfull(){
        $this->browse(function (Browser $browser) {
            $browser->visit('/login')
                ->type('email', 'foo@hoge.com')
                ->type('password', 'hogehoge')
                ->press('Login')
                ->assertSee('this is subscriber');
        });
    }
$ php artisan dusk
1) Tests\Browser\ExampleTest::testBasicExample
Facebook\WebDriver\Exception\WebDriverCurlException: Curl error thrown for http POST to /session with params: {"capabilities":{"firstMatch":[{"browserName":"chrome","goog:chromeOptions":{"binary":"","args":["--disable-gpu","--headless","--window-size=1920,1080"]}}]},"desiredCapabilities":{"browserName":"chrome","platform":"ANY","chromeOptions":{"binary":"","args":["--disable-gpu","--headless","--window-size=1920,1080"]}}}

Failed to connect to localhost port 9515: Connection refused

ん? ChromeDriverが入っていない?

### 3.ChromeDriver インストール
$ ./vendor/laravel/dusk/bin/chromedriver-linux –verbose
./vendor/laravel/dusk/bin/chromedriver-linux: error while loading shared libraries: libX11.so.6: cannot open shared object file: No such file or directory
$ sudo yum install -y libX11
$ ./vendor/laravel/dusk/bin/chromedriver-linux –v
ChromeDriver 80.0.3987.106 (f68069574609230cf9b635cd784cfb1bf81bb53a-refs/branch-heads/3987@{#882})

$ php artisan dusk
1) Tests\Browser\ExampleTest::testBasicExample
Facebook\WebDriver\Exception\UnknownErrorException: unknown error: cannot find Chrome binary

なに、今度はChrome binary?

### 4.Chrome install
$ curl https://intoli.com/install-google-chrome.sh | bash
$ php artisan dusk

1) Tests\Browser\LoginTest::test_can_login_successfull
Facebook\WebDriver\Exception\NoSuchElementException: no such element: Unable to locate element: {"method":"css selector","selector":"body textarea[name='email']"}
  (Session info: headless chrome=80.0.3987.149)

どういうこと? 

### 5.visitでパスを直接指定

public function test_can_login_successfull(){
        $this->browse(function (Browser $browser) {
            $browser->visit('http://192.168.33.10:8000/login')
                // ->dump();
                ->type('email', 'foo@hoge.com')
                ->type('password', 'hogehoge')
                ->press('Login')
                ->assertSee('this is subscriber');
        });

    }
$ php artisan dusk
PHPUnit 8.5.2 by Sebastian Bergmann and contributors.

.                                                                   1 / 1 (100%)

Time: 2.18 seconds, Memory: 18.00 MB

OK (1 test, 1 assertion)

上手くいきました。半日かかりましたが、seleniumが何やってるかは理解できました。

laravel-websockets

Laravel WebScokets
Git hub: laravel-websockets
Official Document: laravel-websockets

– Drop-in Pusher replacement, SSL support, Laravel Echo support and a debug dashboard are just some of its features.

### How to use
https://docs.beyondco.de/laravel-websockets/1.0/getting-started/introduction.html

### package install
$ php composer.phar require beyondcode/laravel-websockets

### publish migration file
$ php artisan vendor:publish –provider=”BeyondCode\LaravelWebSockets\WebSocketsServiceProvider” –tag=”migrations”
Copied File [/vendor/beyondcode/laravel-websockets/database/migrations/create_websockets_statistics_entries_table.php.stub] To [/database/migrations/2020_02_07_062321_create_websockets_statistics_entries_table.php]

$ php artisan migrate
+——————————-+
| Tables_in_chat |
+——————————-+
| failed_jobs |
| messages |
| migrations |
| password_resets |
| users |
| websockets_statistics_entries |
+——————————-+

mysql> describe websockets_statistics_entries;
+————————-+——————+——+—–+———+—————-+
| Field | Type | Null | Key | Default | Extra |
+————————-+——————+——+—–+———+—————-+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| app_id | varchar(255) | NO | | NULL | |
| peak_connection_count | int(11) | NO | | NULL | |
| websocket_message_count | int(11) | NO | | NULL | |
| api_message_count | int(11) | NO | | NULL | |
| created_at | timestamp | YES | | NULL | |
| updated_at | timestamp | YES | | NULL | |
+————————-+——————+——+—–+———+—————-+
※アクセス情報が保存される

### Websocket Configuration File
$ php artisan vendor:publish –provider=”BeyondCode\LaravelWebSockets\WebSocketsServiceProvider” –tag=”config”
Copied File [/vendor/beyondcode/laravel-websockets/config/websockets.php] To [/config/websockets.php]

### config/broadcasting.php

'pusher' => [
            'driver' => 'pusher',
            'key' => env('PUSHER_APP_KEY'),
            'secret' => env('PUSHER_APP_SECRET'),
            'app_id' => env('PUSHER_APP_ID'),
            'options' => [
                'cluster' => env('PUSHER_APP_CLUSTER'),
                'encrypted' => true,
                'host' => '192.168.33.10', // ご自身のホスト名
                'port' => 6001,
                'scheme' => 'http' // or https
            ],
        ],

### .env

PUSHER_APP_ID=1
PUSHER_APP_KEY=laravelWebsockets
PUSHER_APP_SECRET=laravelWebsockets
PUSHER_APP_CLUSTER=ap1

$ php artisan serve –host 192.168.33.10 –port 8000
$ php artisan websockets:serve
Starting the WebSocket server on port 6001…

app.js:28574 WebSocket connection to ‘wss://192.168.33.10/app/laravelWebsockets?protocol=7&client=js&version=5.0.3&flash=false’ failed: Error in connection establishment: net::ERR_CONNECTION_REFUSED

何故だ。。。