Code/PHP

[php] Laravel Dusk 사용법

codens 2020. 8. 23. 14:09

라라벨 더스크 사용 방법

 

https://laravel.com/docs/6.x/dusk




* 설치

composer require --dev laravel/dusk

        - /tests/Browser 폴더 생성됨

 

php artisan dusk:install

        - chrome driver가 설치됨



* 테스트 실행

php artisan dusk

        - /tests/Browser 폴더에 있는 테스트 파일로 테스트 실행



//-------------------------------------------

* 테스트 실행시 실제 브라우저 창 보기

    - \tests\DuskTestCase.php 파일 수정

        '--disable-gpu', '--headless', 라인 주석 처리

 

$options = (new ChromeOptions)->addArguments([

            //'--disable-gpu',

            //'--headless',



//----------------------------------------

* 크롬 확장프로그램 실행 시키기

    - 기본적으로 Chrome Automation Extension 확장만 실행된다

        - \tests\DuskTestCase.php 파일 수정

$options = (new ChromeOptions)->addArguments([ 

    // 다음 행 추가

    'load-extension=' . [크롬확장 경로],

 

    크롬확장 경로 예) C:\\Users\\userID\\AppData\\Local\\Google\\Chrome\\User Data\\Profile 2\\Extensions\\cjpalhdlnbpafiamejdnhcphjbkeiagm\\1.29.2_0



* 디버깅 창 자동으로 열기

    - $options = (new ChromeOptions)->addArguments([ 에 다음을 추가

 

    '--auto-open-devtools-for-tabs',




//----------------------------------------

* 실행후 브라우저 창 유지하기(안닫기)

    ->pause(100000);



//----------------------------------------

* 마우스 더블 클릭하기

    - Dusk의 doubleClick()은 클릭할 노드를 지정할수 없다

    - Dusk 소스 수정

    \vendor\laravel\dusk\src\Concerns\InteractsWithMouse.php 파일 수정

 

public function doubleClick($selector = null)

    {

        //(new WebDriverActions($this->driver))->doubleClick()->perform();

        if (is_null($selector)) {

            (new WebDriverActions($this->driver))->doubleClick()->perform();

        } else {

            (new WebDriverActions($this->driver))->doubleClick(

                $this->resolver->findOrFail($selector)

            )->perform();

        }

 

        return $this;

    }

 

 

//----------------------------------------

* 사용자 함수 추가(브라우저 매크로)

    - 서비스 프로바이더 추가

php artisan make:provider DuskServiceProvider

 

    - boot() 함수에 매크로 추가

class DuskServiceProvider extends ServiceProvider

{   

    public function boot()

    {

        Browser::macro('scrollToElement', function ($element = null) {

            $this->script("$('html, body').animate({ scrollTop: $('$element').offset().top }, 0);");

 

            return $this;

        });

    }



    - 사용

$browser->visit('/pay')

        ->scrollToElement('#credit-card-details')



    - config\app.php 파일 수정

'providers' => [

    App\Providers\DuskServiceProvider::class, // 추가

 


   //-------------------------

    - 이전 설정 삭제

        - undefined method 에러를 고칠수 있다.

php artisan config:clear

php artisan cache:clear

composer dumpautoload

        

 

반응형