TestCase::enableNetworkLogging()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
rs 9.6333
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace BeyondCode\DuskDashboard\Testing;
4
5
use BeyondCode\DuskDashboard\BrowserActionCollector;
6
use BeyondCode\DuskDashboard\Console\StartDashboardCommand;
7
use BeyondCode\DuskDashboard\Dusk\Browser;
8
use Closure;
9
use Facebook\WebDriver\Chrome\ChromeOptions;
10
use Facebook\WebDriver\Remote\DesiredCapabilities;
11
use GuzzleHttp\Client;
12
use GuzzleHttp\RequestOptions;
13
use Laravel\Dusk\TestCase as BaseTestCase;
14
use Throwable;
15
16
abstract class TestCase extends BaseTestCase
17
{
18
    /**
19
     * Create a new Browser instance.
20
     *
21
     * @param  \Facebook\WebDriver\Remote\RemoteWebDriver  $driver
22
     * @return \BeyondCode\DuskDashboard\Dusk\Browser
23
     */
24
    protected function newBrowser($driver)
25
    {
26
        return new Browser($driver);
27
    }
28
29
    /**
30
     * Create the browser instances needed for the given callback.
31
     *
32
     * @param  \Closure  $callback
33
     * @return array
34
     * @throws \ReflectionException
35
     */
36
    protected function createBrowsersFor(Closure $callback)
37
    {
38
        $browsers = parent::createBrowsersFor($callback);
39
40
        foreach ($browsers as $browser) {
41
            $browser->setActionCollector(new BrowserActionCollector($this->getTestName()));
42
        }
43
44
        static::$browsers = $browsers;
45
46
        return static::$browsers;
47
    }
48
49
    protected function getTestName()
50
    {
51
        return class_basename(static::class).'::'.$this->getName();
52
    }
53
54
    protected function enableNetworkLogging(DesiredCapabilities $capabilities): DesiredCapabilities
55
    {
56
        $chromeOptions = $capabilities->getCapability(ChromeOptions::CAPABILITY);
0 ignored issues
show
Deprecated Code introduced by
The constant Facebook\WebDriver\Chrom...romeOptions::CAPABILITY has been deprecated.

This class constant has been deprecated.

Loading history...
57
58
        $perfLoggingPrefs = new \stdClass();
59
        $perfLoggingPrefs->enableNetwork = true;
60
61
        $chromeOptions->setExperimentalOption('perfLoggingPrefs', $perfLoggingPrefs);
62
63
        $capabilities->setCapability(ChromeOptions::CAPABILITY, $chromeOptions);
0 ignored issues
show
Deprecated Code introduced by
The constant Facebook\WebDriver\Chrom...romeOptions::CAPABILITY has been deprecated.

This class constant has been deprecated.

Loading history...
64
65
        $loggingPrefs = new \stdClass();
66
        $loggingPrefs->browser = 'ALL';
67
        $loggingPrefs->performance = 'ALL';
68
69
        $capabilities->setCapability('loggingPrefs', $loggingPrefs);
70
71
        return $capabilities;
72
    }
73
74
    protected function onNotSuccessfulTest(Throwable $t): void
75
    {
76
        try {
77
            (new Client())->post('http://127.0.0.1:'.StartDashboardCommand::PORT.'/events', [
78
                RequestOptions::JSON => [
79
                    'channel' => 'dusk-dashboard',
80
                    'name' => 'dusk-failure',
81
                    'data' => [
82
                        'message' => $t->getMessage(),
83
                    ],
84
                ],
85
            ]);
86
        } catch (\Exception $e) {
87
            // Dashboard is offline
88
        } finally {
89
            parent::onNotSuccessfulTest($t);
90
        }
91
    }
92
}
93