DuskTestCase.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. namespace Tests;
  3. use Dcat\Admin\Models\Administrator;
  4. use Facebook\WebDriver\Chrome\ChromeOptions;
  5. use Facebook\WebDriver\Remote\DesiredCapabilities;
  6. use Facebook\WebDriver\Remote\RemoteWebDriver;
  7. use Laravel\Dusk\Browser;
  8. use Laravel\Dusk\TestCase as BaseTestCase;
  9. abstract class DuskTestCase extends BaseTestCase
  10. {
  11. use CreatesApplication, BrowserExtension, InteractsWithDatabase;
  12. /**
  13. * @var Administrator
  14. */
  15. protected $user;
  16. /**
  17. * @var bool
  18. */
  19. protected $login = true;
  20. public function login(Browser $browser)
  21. {
  22. $browser->loginAs($this->getUser(), 'admin');
  23. }
  24. public function setUp(): void
  25. {
  26. parent::setUp();
  27. $this->extendBrowser();
  28. $this->boot();
  29. }
  30. public function tearDown(): void
  31. {
  32. $this->destory();
  33. parent::tearDown();
  34. }
  35. /**
  36. * Prepare for Dusk test execution.
  37. *
  38. * @beforeClass
  39. * @return void
  40. */
  41. public static function prepare()
  42. {
  43. static::startChromeDriver();
  44. }
  45. /**
  46. * @param \Facebook\WebDriver\Remote\RemoteWebDriver $driver
  47. *
  48. * @return \Laravel\Dusk\Browser
  49. */
  50. protected function newBrowser($driver)
  51. {
  52. $browser = parent::newBrowser($driver)->resize(1566, 1080);
  53. if ($this->login) {
  54. $this->login($browser);
  55. }
  56. return $browser;
  57. }
  58. /**
  59. * Create the RemoteWebDriver instance.
  60. *
  61. * @return \Facebook\WebDriver\Remote\RemoteWebDriver
  62. */
  63. protected function driver()
  64. {
  65. $options = (new ChromeOptions)->addArguments([
  66. '--disable-gpu',
  67. '--headless',
  68. '--window-size=1920,1080',
  69. ]);
  70. return RemoteWebDriver::create(
  71. 'http://localhost:9515', DesiredCapabilities::chrome()->setCapability(
  72. ChromeOptions::CAPABILITY_W3C, $options
  73. )
  74. );
  75. }
  76. /**
  77. * Build the process to run the Chromedriver.
  78. *
  79. * @param array $arguments
  80. * @return \Symfony\Component\Process\Process
  81. *
  82. * @throws \RuntimeException
  83. */
  84. protected static function buildChromeProcess(array $arguments = [])
  85. {
  86. return (new ChromeProcess(static::$chromeDriver))->toProcess($arguments);
  87. }
  88. }