DuskTestCase.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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\TestCase as BaseTestCase;
  8. abstract class DuskTestCase extends BaseTestCase
  9. {
  10. use CreatesApplication, BrowserExtension, InteractsWithDatabase;
  11. /**
  12. * @var Administrator
  13. */
  14. protected $user;
  15. /**
  16. * @var bool
  17. */
  18. protected $login = true;
  19. public function login(Browser $browser)
  20. {
  21. $browser->loginAs($this->getUser(), 'admin');
  22. }
  23. public function setUp(): void
  24. {
  25. parent::setUp();
  26. $this->extendBrowser();
  27. $this->boot();
  28. }
  29. public function tearDown(): void
  30. {
  31. $this->destory();
  32. parent::tearDown();
  33. }
  34. /**
  35. * Prepare for Dusk test execution.
  36. *
  37. * @beforeClass
  38. * @return void
  39. */
  40. public static function prepare()
  41. {
  42. static::startChromeDriver();
  43. }
  44. /**
  45. * @param \Facebook\WebDriver\Remote\RemoteWebDriver $driver
  46. *
  47. * @return \Laravel\Dusk\Browser
  48. */
  49. protected function newBrowser($driver)
  50. {
  51. $browser = (new Browser($driver))->resize(1566, 1080);
  52. $browser->resolver->prefix = 'html';
  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. }