如何以编程方式从 php artisan 命令调用 dusk 测试

How to programmatically call a dusk test from a php artisan command

我需要通过 artisan 命令 运行 我的 Laravel Dusk 测试之一,以便它每天处理。我已经在我的命令中尝试了 $this->call('dusk');,但是 运行 是 dusk 的所有测试并且不允许我添加组或过滤器。我只需要 运行 进行 1 次测试。如何添加过滤器?

$this->call('dusk', [ '--group' => 'communication_tests' ]);

$this->call('dusk', [ '--filter' => 'tests\Browser\myTestFile::myTestMethod' ]);

不起作用,它会忽略传入的选项。关于如何完成此操作有什么想法吗?

1st 创建工作 Laravel Dusk 测试。使用 php artisan dusk 对其进行测试并确保其正常工作。

2nd 在名为 Dusk 的 app\Commands 文件夹中创建您自己的命令以覆盖 laravel 的原生 Dusk 命令并将其签名设为 'dusk'。让它扩展 Laravel\Dusk\Console\DuskCommand 并将下面的代码写入其句柄方法(有关此代码的其他版本,请参阅 https://github.com/laravel/dusk/issues/371)。我编辑了我的以删除 $this->option('without-tty') ? 3 : 2 三元语句,所以它只是为我读取 2,因为该选项不存在或不需要我的 laravel.[=19= 版本]

3rd 将新的 class 添加到内核,这样当您调用 php artisan dusk 时 class 被识别。

4th 创建新命令以编程方式 运行 您的 dusk 测试使用@taytus 添加的最后一行。

5th 也将新 class 添加到内核中。

下面是我的文件 运行...

我的 laravel dusk 测试 (tests\Browser\CommunicationsTest.php) (第 1 步)

<?php

namespace Tests\Browser;

use Tests\DuskTestCase;
use Laravel\Dusk\Browser;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use App\Models\User;

class CommunicationsTest extends DuskTestCase
{
    /**
     * A Dusk test example.
     * @group all_communication_tests
     * @return void
     */
    public function test_that_all_coms_work()
    {
        // Test Text
        $this->browse(function (Browser $browser) {

            $browser->resize(1920, 1080);
            $browser->loginAs(7)
                ->visit('/events/types/appointments')
                ->assertSee('Automated Messages')
                ->click('.text_message0')
                ->pause(1000)
                ->click('.send-eng-text-btn')
                ->pause(1000)
                ->type('phone', env('TESTING_DEVELOPER_PHONE'))
                ->click('.send-text')
                ->pause(5000)
                ->assertSee('Your test text has been sent.');

            // Test Email
            $browser->visit('/events/types/appointments')
                ->assertSee('Automated Messages')
                ->click('.email0')
                ->assertSee('Automated Messages')
                ->driver->executeScript('window.scrollTo(595, 1063);');
            $browser->click('.send-eng-email-btn')
                ->pause(2000)
                ->click('.send-email')
                ->pause(10000)
                ->assertSee('Your test email has been sent.');

            // Test Call
            $browser->visit('/audio/testcall')
                ->assertSee('Test Call')
                ->type('phone', env('TESTING_DEVELOPER_PHONE'))
                ->press('Call')
                ->pause(3000)
                ->assertSee('Test Call Queued');
        });
    }
}

我的覆盖 dusk 命令(app\Console\Commands\DuskCommand.php)(第 2 步)

<?php

namespace App\Console\Commands;

use Laravel\Dusk\Console\DuskCommand as VendorDuskCommand;
use Symfony\Component\Process\ProcessBuilder;

class DuskCommand extends VendorDuskCommand
{

    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'dusk';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Run Tests on our system... by extending the Laravel Vendor DuskCommand.';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $this->purgeScreenshots();

        $this->purgeConsoleLogs();

        $options=array(); 
        // This line checks if it is a direct call or if has been called from Artisan (we assume is Artisan, more checks can be added) 
        if($_SERVER['argv'][1]!='dusk'){ 
            $filter=$this->input->getParameterOption('--filter'); 
            // $filter returns 0 if has not been set up 
            if($filter){
                $options[]='--filter'; 
                $options[]=$filter; 
                // note: --path is a custom key, check how I use it in Commands\CommunicationsTest.php 
                $options[]=$this->input->getParameterOption('--path'); 
            } 
        }else{ 
            $options = array_slice($_SERVER['argv'], 2); 
        }

        return $this->withDuskEnvironment(function () use ($options) {
            $process = (new ProcessBuilder())
                ->setTimeout(null)
                ->setPrefix($this->binary())
                ->setArguments($this->phpunitArguments($options))
                ->getProcess();

            try {
                $process->setTty(true);
            } catch (RuntimeException $e) {
                $this->output->writeln('Warning: '.$e->getMessage());
            }

            return $process->run(function ($type, $line) {
                $this->output->write($line);
            });
        });
    }

}

现在将该新命令添加到内核,以便在您调用它时进行注册并覆盖 native/vendor Dusk 命令的功能。 (第 3 步)

/**
     * The Artisan commands provided by your application.
     *
     * @var array
     */
    protected $commands = [
         // .... preceding commands....
         Commands\DuskCommand::class,
   ];

现在创建将以编程方式调用 dusk 的命令 - 这是我的命令 (第 4 步)

<?php 

namespace App\Console\Commands;

use DB;
use Illuminate\Console\Command;

class TestCommunications extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'test:communications';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Test Email, Text, and Phone Calls to make sure they\'re operational.';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {

       $response = $this->call('dusk',['--filter'=>'test_that_all_coms_work','--path'=>'tests/Browser/CommunicationsTest.php']);

    }
}

并在内核中注册它(第 5 步)...

/**
     * The Artisan commands provided by your application.
     *
     * @var array
     */
    protected $commands = [
         .... preceding commands....
         Commands\DuskCommand::class,
         Commands\TestCommunications::class,
   ];

现在 运行 php artisan dusk 它应该会触发扩展 Dusk 命令并且工作正常。然后调用替换我的 TestCommunications.php 文件的新 php artisan 命令,它应该 运行 dusk 完美……假设您的测试当然有效。如果您有任何问题或我遗漏了什么,请告诉我。

记住 Dusk 只能在本地环境中工作...这不是你 want/should/natively 可以在生产环境中实现的东西