-
-
Notifications
You must be signed in to change notification settings - Fork 112
/
Copy pathDuskTestCase.php
112 lines (95 loc) · 3.12 KB
/
DuskTestCase.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
<?php
namespace Tests;
use Facebook\WebDriver\Chrome\ChromeOptions;
use Facebook\WebDriver\Remote\DesiredCapabilities;
use Facebook\WebDriver\Remote\RemoteWebDriver;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Http;
use Laravel\Dusk\TestCase as BaseTestCase;
use PHPUnit\Framework\Attributes\BeforeClass;
use Spatie\Snapshots\MatchesSnapshots;
abstract class DuskTestCase extends BaseTestCase
{
use CreatesApplication;
use MatchesSnapshots;
/**
* Create a new Browser instance.
*
* @param \Facebook\WebDriver\Remote\RemoteWebDriver $driver
* @return \Tests\Browser
*/
protected function newBrowser($driver)
{
return new Browser($driver);
}
public static function tableUrls()
{
return [
['/table/users/spatie/'],
['/table/users/splade/'],
['/table/users/spatieWrapped/'],
['/table/users/spladeWrapped/'],
];
}
public static function booleanDataset()
{
return [[false], [true]];
}
protected function configureDownloadPath($browser, $path)
{
(new Filesystem)->ensureDirectoryExists($path);
(new Filesystem)->cleanDirectory($path);
$url = $browser->driver->getCommandExecutor()->getAddressOfRemoteServer();
$uri = '/session/' . $browser->driver->getSessionID() . '/chromium/send_command';
Http::post($url . $uri, [
'cmd' => 'Page.setDownloadBehavior',
'params' => ['behavior' => 'allow', 'downloadPath' => $path],
]);
$this->beforeApplicationDestroyed(fn () => (new Filesystem)->cleanDirectory($path));
}
/**
* Prepare for Dusk test execution.
*
* @return void
*/
#[BeforeClass]
public static function prepare()
{
if (!static::runningInSail()) {
static::startChromeDriver(['--port=9515']);
}
}
public function assertScreenshotSnapshot($name): self
{
$names = Arr::wrap($name);
foreach ($names as $name) {
$filePath = sprintf('%s/%s.png', rtrim(Browser::$storeScreenshotsAt, '/'), $name);
// $this->assertMatchesFileSnapshot($filePath);
}
return $this;
}
/**
* Create the RemoteWebDriver instance.
*
* @return \Facebook\WebDriver\Remote\RemoteWebDriver
*/
protected function driver()
{
$options = (new ChromeOptions)->addArguments(collect([
$this->shouldStartMaximized() ? '--start-maximized' : '--window-size=1920,1080',
'--disable-search-engine-choice-screen',
])->unless($this->hasHeadlessDisabled(), function ($items) {
return $items->merge([
'--disable-gpu',
'--headless=new',
]);
})->all());
return RemoteWebDriver::create(
$_ENV['DUSK_DRIVER_URL'] ?? 'http://localhost:9515',
DesiredCapabilities::chrome()->setCapability(
ChromeOptions::CAPABILITY, $options
)->setCapability('goog:loggingPrefs', ['browser' => 'ALL'])
);
}
}