DuskTestCase.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. $browser->resolver->prefix = 'html';
  54. if ($this->login) {
  55. $this->login($browser);
  56. }
  57. return $browser;
  58. }
  59. /**
  60. * Create the RemoteWebDriver instance.
  61. *
  62. * @return \Facebook\WebDriver\Remote\RemoteWebDriver
  63. */
  64. protected function driver()
  65. {
  66. $options = (new ChromeOptions)->addArguments([
  67. '--disable-gpu',
  68. '--headless',
  69. '--window-size=1920,1080',
  70. ]);
  71. return RemoteWebDriver::create(
  72. 'http://localhost:9515', DesiredCapabilities::chrome()->setCapability(
  73. ChromeOptions::CAPABILITY_W3C, $options
  74. )
  75. );
  76. }
  77. /**
  78. * Build the process to run the Chromedriver.
  79. *
  80. * @param array $arguments
  81. * @return \Symfony\Component\Process\Process
  82. *
  83. * @throws \RuntimeException
  84. */
  85. protected static function buildChromeProcess(array $arguments = [])
  86. {
  87. return (new ChromeProcess(static::$chromeDriver))->toProcess($arguments);
  88. }
  89. }