2

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

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

或者

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

不起作用,它忽略了传入的选项。关于如何实现这一点的任何想法?

4

1 回答 1

1

第一次创建你的工作 Laravel Dusk 测试。使用 php artisan Huang 进行测试并确保它正常工作。

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

第三次将你的新类添加到内核中,这样当你调用 php artisan黄昏时,类就会被识别。

第 4步创建新命令,使用 @taytus 添加的最后一行以编程方式运行黄昏测试。

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

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

我的 laravel 黄昏测试 (tests\Browser\CommunicationsTest.php) (STEP 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');
        });
    }
}

我的覆盖黄昏命令 (app\Console\Commands\DuskCommand.php) (STEP 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);
            });
        });
    }

}

现在将该新命令添加到内核中,以便在您调用它时注册并覆盖本机/供应商 DuskCommand 的功能。(第 3 步)

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

现在创建将以编程方式调用黄昏的命令 - 这是我的(第 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,它应该会命中扩展的 DuskCommand 并且可以正常工作。然后调用替换我的 TestCommunications.php 文件的新 php artisan 命令,它应该可以完美地运行黄昏......当然假设你的测试有效。如果您有任何问题或我遗漏了什么,请告诉我。

记住 Dusk 只在本地环境中工作......这不是你想要/应该/本机可以在生产环境中实现的东西

于 2019-02-21T19:23:54.157 回答