フリーランス 技術調査ブログ

フリーランス/エンジニア Ruby Python Nodejs Vuejs React Dockerなどの調査技術調査の備忘録

SeleniumでLaravelDuskテストを実行する

はじめに

LaravelDusk パッケージ インストール

  • Laravelのバージョンによってインストールできるバージョンが異なるため、公式サイトを参考にインストールコマンドを実行する
  • memory_limitを指定しないとメモリーリークで落ちるため、指定しております。
php -d memory_limit=-1 /usr/local/bin/composer require --dev laravel/dusk:"^2.0"

※ご自身の環境にあったインストールコマンドをサイトで確認する readouble.com

  • インストールの結果、すごいwarningが発生しているが気にしない。
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)

Package operations: 2 installs, 0 updates, 0 removals
As there is no 'unzip' command installed zip files are being unpacked using the PHP zip extension.
This may cause invalid reports of corrupted archives. Besides, any UNIX permissions (e.g. executable) defined in the archives will be lost.
Installing 'unzip' may remediate them.
  - Installing facebook/webdriver (1.7.1): Downloading (100%)         
  - Installing laravel/dusk (v2.0.14): Downloading (100%)         
facebook/webdriver suggests installing ext-SimpleXML (For Firefox profile creation)
Package davejamesmiller/laravel-breadcrumbs is abandoned, you should avoid using it. Use diglactic/laravel-breadcrumbs instead.
Package fzaninotto/faker is abandoned, you should avoid using it. No replacement was suggested.
Package jakub-onderka/php-console-color is abandoned, you should avoid using it. Use php-parallel-lint/php-console-color instead.
Package jakub-onderka/php-console-highlighter is abandoned, you should avoid using it. Use php-parallel-lint/php-console-highlighter instead.
Package mtdowling/cron-expression is abandoned, you should avoid using it. Use dragonmantank/cron-expression instead.
Package phpunit/php-token-stream is abandoned, you should avoid using it. No replacement was suggested.
Package phpunit/phpunit-mock-objects is abandoned, you should avoid using it. No replacement was suggested.
Package facebook/webdriver is abandoned, you should avoid using it. Use php-webdriver/webdriver instead.
Writing lock file
Generating optimized autoload files
Deprecation Notice: Class Faker\Provider\uk_Ua\Payment located in ./vendor/fzaninotto/faker/src/Faker/Provider/uk_UA/Payment.php does not comply with psr-4 autoloading standard. It will not autoload anymore in Composer v2.0. in phar:///usr/local/bin/composer/src/Composer/Autoload/ClassMapGenerator.php:201
Deprecation Notice: Class Egulias\EmailValidator\Exception\ExpectedQPair located in ./vendor/egulias/email-validator/EmailValidator/Exception/ExpectingQPair.php does not comply with psr-4 autoloading standard. It will not autoload anymore in Composer v2.0. in phar:///usr/local/bin/composer/src/Composer/Autoload/ClassMapGenerator.php:201
Deprecation Notice: Class App\Admin\Extensions\Form\mailmagazineLp located in ./app/Admin/Extensions/Form/mailmagazinelp.php does not comply with psr-4 autoloading standard. It will not autoload anymore in Composer v2.0. in phar:///usr/local/bin/composer/src/Composer/Autoload/ClassMapGenerator.php:201
Deprecation Notice: Class App\Console\Commands\App\MamabiyoriAppArticleChangeOrder located in ./app/Console/Commands/app/MamabiyoriAppArticleChangeOrder.php does not comply with psr-4 autoloading standard. It will not autoload anymore in Composer v2.0. in phar:///usr/local/bin/composer/src/Composer/Autoload/ClassMapGenerator.php:201
Deprecation Notice: Class App\Entities\MystoryCategory located in ./app/Entities/MystoryCatgory.php does not comply with psr-4 autoloading standard. It will not autoload anymore in Composer v2.0. in phar:///usr/local/bin/composer/src/Composer/Autoload/ClassMapGenerator.php:201
> Illuminate\Foundation\ComposerScripts::postUpdate
> php artisan optimize

LaravelDuskのインストール

  • 次にphp artisan dusk:installコマンドを実行してテストに必要なフォルダやファイルを作成する
# php artisan dusk:install 
Dusk scaffolding installed successfully.
  • testsフォルダが作成される。
/tests/Browser/
/tests/DuskTestCase.php

LaravelDuskの設定

<?php

namespace Tests;

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

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()
    {
        return RemoteWebDriver::create(
            'http://<dockerのselenium hubのコンテナ名>:4444/wd/hub', DesiredCapabilities::chrome()
        );
    }
}
  • 下記のコンテナ名に誤りあると場合
 'http://<dockerのselenium hubのコンテナ名>:4444/wd/hub', DesiredCapabilities::chrome()
  • 下記のエラーが発生するので、コンテナ名の指定は確認して実行する必要がる
There was 1 error:

1) Tests\Browser\ExampleTest::testBasicExample
Facebook\WebDriver\Exception\WebDriverCurlException: Curl error thrown for http POST to /session with params: {"desiredCapabilities":{"browserName":"chrome","platform":"ANY","chromeOptions":{"w3c":false,"binary":""}}}

テストを実行する

  • 下記のコマンドを実行すると「E」と表示されるだけで何も返事がありませんでした。
  • 設定がうまく行っていないようだ!
php artisan dusk

色々と試行錯誤をして

  • Selenium Grid経由からのテストは断念して下記のようにdockerの設定を変更して検証したところ、うまく動作しました。
  selenium:
    image: selenium/standalone-chrome-debug
    ports:
      - 4444:4444
      - 5900:5900
    container_name: selenium-container
    depends_on:
      - nginx
    privileged: true

再度動作確認

VNCでdockerのstandalone-chrome-debugにログインする f:id:PX-WING:20201204220232p:plain パスワードを「secret」として入力してログインする f:id:PX-WING:20201204220201p:plain docker内の画面が表示される f:id:PX-WING:20201204220244p:plain

再度、テストコマンドを実行する

php artisan dusk

standalone-chrome-debugの仮想環境でchromが立ち上がり、テストが実施される f:id:PX-WING:20201204220249p:plain

-最終的に下記が出力されてテストが通ったことが確認できる。

# php artisan dusk 
PHPUnit 5.7.22 by Sebastian Bergmann and contributors.

.                                                                   1 / 1 (100%)

Time: 22.54 seconds, Memory: 18.00MB

OK (1 test, 1 assertion)