DuskTestCase.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php
  2. namespace Tests;
  3. use Facebook\WebDriver\Chrome\ChromeOptions;
  4. use Facebook\WebDriver\Remote\DesiredCapabilities;
  5. use Facebook\WebDriver\Remote\RemoteWebDriver;
  6. use Laravel\Dusk\TestCase as BaseTestCase;
  7. abstract class DuskTestCase extends BaseTestCase
  8. {
  9. use CreatesApplication;
  10. /**
  11. * Prepare for Dusk test execution.
  12. *
  13. * @beforeClass
  14. * @return void
  15. */
  16. public static function prepare()
  17. {
  18. // if (! static::runningInSail()) {
  19. // static::startChromeDriver();
  20. // }
  21. }
  22. /**
  23. * Create the RemoteWebDriver instance.
  24. *
  25. * @return \Facebook\WebDriver\Remote\RemoteWebDriver
  26. */
  27. protected function driver()
  28. {
  29. $options = (new ChromeOptions)->addArguments(collect([
  30. $this->shouldStartMaximized() ? '--start-maximized' : '--window-size=1920,1080',
  31. ])->unless($this->hasHeadlessDisabled(), function ($items) {
  32. return $items->merge([
  33. '--disable-gpu',
  34. '--headless',
  35. ]);
  36. })->all());
  37. return RemoteWebDriver::create(
  38. $_ENV['DUSK_DRIVER_URL'] ?? 'http://localhost:9515',
  39. DesiredCapabilities::chrome()->setCapability(
  40. ChromeOptions::CAPABILITY, $options
  41. )
  42. );
  43. }
  44. /**
  45. * Determine whether the Dusk command has disabled headless mode.
  46. *
  47. * @return bool
  48. */
  49. protected function hasHeadlessDisabled()
  50. {
  51. return isset($_SERVER['DUSK_HEADLESS_DISABLED']) ||
  52. isset($_ENV['DUSK_HEADLESS_DISABLED']);
  53. }
  54. /**
  55. * Determine if the browser window should start maximized.
  56. *
  57. * @return bool
  58. */
  59. protected function shouldStartMaximized()
  60. {
  61. return isset($_SERVER['DUSK_START_MAXIMIZED']) ||
  62. isset($_ENV['DUSK_START_MAXIMIZED']);
  63. }
  64. }