如何通过php artisan命令以编程方式调用黄昏测试

如何通过php artisan命令以编程方式调用黄昏测试,php,laravel-5,laravel-artisan,laravel-dusk,laravel-dusk2,Php,Laravel 5,Laravel Artisan,Laravel Dusk,Laravel Dusk2,我需要从artisan命令运行我的一个Laravel黄昏测试,以便它每天进行处理。我尝试了$this->呼叫('dash')在我的命令中,但它运行Dask的所有测试,不允许我添加组或过滤器。我只需要运行一个测试。如何添加过滤器 $this->call('dusk', [ '--group' => 'communication_tests' ]); 或 不起作用,它会忽略传入的选项。关于如何做到这一点,有什么想法吗?1st创建您的工作Laravel黄昏测试。使用php artisan

我需要从artisan命令运行我的一个Laravel黄昏测试,以便它每天进行处理。我尝试了
$this->呼叫('dash')
在我的命令中,但它运行Dask的所有测试,不允许我添加组或过滤器。我只需要运行一个测试。如何添加过滤器

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


不起作用,它会忽略传入的选项。关于如何做到这一点,有什么想法吗?

1st创建您的工作Laravel黄昏测试。使用php artisan dusk进行测试,确保它正常工作

2nd在app\Commands文件夹中创建自己的名为dashcommand的命令,以覆盖laravels原生dashcommand,并使其签名为“黄昏”。让它扩展Laravel\Dusk\Console\Dusk命令,并将下面的代码写入其handle方法(有关此代码的其他版本,请参阅)。我编辑了我的以删除
$this->选项('without-tty')?3:2
三元语句,因此它只读取我的
2
,因为该选项不存在,或者我的laravel版本不需要该选项

3rd将新类添加到内核中,以便在调用php artisan dusk时识别该类

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

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

下面是我的文件

My laravel黄昏测试(tests\Browser\CommunicationsTest.php)(步骤1)

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

现在运行
php-artisan-dusk
,它应该可以点击扩展的dusk命令并正常工作。然后调用新的php artisan命令来替换我的TestCommunications.php文件,它应该可以完美地运行黄昏。。。当然,假设你的测试有效。如果你有任何问题或者我遗漏了什么,请告诉我


记住,黄昏只在本地环境中起作用。。。这不是您想要/应该/本机可以在生产环境上实现的功能

BadMethodCallException方法App\Console\Commands\DashCommand::WithDashEnvironment不存在。
$this->call('dusk', [ '--filter' => 'tests\Browser\myTestFile::myTestMethod' ]);
<?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');
        });
    }
}
<?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);
            });
        });
    }

}
/**
     * The Artisan commands provided by your application.
     *
     * @var array
     */
    protected $commands = [
         // .... preceding commands....
         Commands\DuskCommand::class,
   ];
<?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']);

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