|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace BeyondCode\DuskDashboard; |
|
4
|
|
|
|
|
5
|
|
|
use BeyondCode\DuskDashboard\Console\StartDashboardCommand; |
|
6
|
|
|
use BeyondCode\DuskDashboard\Dusk\Browser; |
|
7
|
|
|
use GuzzleHttp\Client; |
|
8
|
|
|
use GuzzleHttp\RequestOptions; |
|
9
|
|
|
|
|
10
|
|
|
class BrowserActionCollector |
|
11
|
|
|
{ |
|
12
|
|
|
/** @var Client */ |
|
13
|
|
|
protected $client; |
|
14
|
|
|
|
|
15
|
|
|
protected $testName; |
|
16
|
|
|
|
|
17
|
|
|
public function __construct($testName) |
|
18
|
|
|
{ |
|
19
|
|
|
$this->testName = $testName; |
|
20
|
|
|
|
|
21
|
|
|
$this->client = new Client(); |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
public function collect(string $action, array $arguments, Browser $browser, string $previousHtml = null) |
|
25
|
|
|
{ |
|
26
|
|
|
$path = parse_url($browser->driver->getCurrentURL(), PHP_URL_PATH) ?? ''; |
|
27
|
|
|
|
|
28
|
|
|
$action = new Action($action, $arguments, $browser->getCurrentPageSource(), $path); |
|
29
|
|
|
|
|
30
|
|
|
$action->setPreviousHtml($previousHtml); |
|
31
|
|
|
|
|
32
|
|
|
$this->pushAction('dusk-event', [ |
|
33
|
|
|
'test' => $this->testName, |
|
34
|
|
|
'path' => $action->getPath(), |
|
35
|
|
|
'name' => $action->getName(), |
|
36
|
|
|
'arguments' => $action->getArguments(), |
|
37
|
|
|
'before' => $action->getPreviousHtml(), |
|
38
|
|
|
'html' => $action->getHtml(), |
|
39
|
|
|
]); |
|
40
|
|
|
|
|
41
|
|
|
$this->processPerformanceLog($browser); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
protected function processPerformanceLog(Browser $browser) |
|
45
|
|
|
{ |
|
46
|
|
|
$logs = collect([]); |
|
47
|
|
|
|
|
48
|
|
|
try { |
|
49
|
|
|
$logs = collect($browser->driver->manage()->getLog('performance')); |
|
50
|
|
|
} catch (\Exception $e) { |
|
51
|
|
|
// performance logging might be disabled. |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
$allowedMethods = [ |
|
55
|
|
|
'Network.requestWillBeSent', |
|
56
|
|
|
'Network.responseReceived', |
|
57
|
|
|
]; |
|
58
|
|
|
|
|
59
|
|
|
$logs |
|
60
|
|
|
->map(function ($log) { |
|
61
|
|
|
return json_decode($log['message']); |
|
62
|
|
|
}) |
|
63
|
|
|
->filter(function ($log) use ($allowedMethods) { |
|
64
|
|
|
$method = data_get($log, 'message.method'); |
|
65
|
|
|
|
|
66
|
|
|
$type = data_get($log, 'message.params.type'); |
|
67
|
|
|
|
|
68
|
|
|
return in_array($method, $allowedMethods) && $type === 'XHR'; |
|
69
|
|
|
})->groupBy(function ($log) { |
|
70
|
|
|
if (data_get($log, 'message.method') === 'Network.requestWillBeSent') { |
|
71
|
|
|
return data_get($log, 'message.requestId'); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
return data_get($log, 'params.requestId'); |
|
75
|
|
|
})->map(function ($log) use ($browser) { |
|
76
|
|
|
$this->pushPerformanceLog($log->toArray(), $browser); |
|
77
|
|
|
}); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
protected function pushAction(string $name, array $payload) |
|
81
|
|
|
{ |
|
82
|
|
|
try { |
|
83
|
|
|
$this->client->post('http://127.0.0.1:'.StartDashboardCommand::PORT.'/events', [ |
|
84
|
|
|
RequestOptions::JSON => [ |
|
85
|
|
|
'channel' => 'dusk-dashboard', |
|
86
|
|
|
'name' => $name, |
|
87
|
|
|
'data' => $payload, |
|
88
|
|
|
], |
|
89
|
|
|
]); |
|
90
|
|
|
} catch (\Exception $e) { |
|
91
|
|
|
// Dusk-Dashboard Server might be turned off. No need to panic! |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
protected function pushPerformanceLog(array $log, Browser $browser) |
|
96
|
|
|
{ |
|
97
|
|
|
$request = $log[0]; |
|
98
|
|
|
$response = $log[1]; |
|
99
|
|
|
|
|
100
|
|
|
$url = parse_url(data_get($request, 'message.params.request.url')); |
|
101
|
|
|
|
|
102
|
|
|
$this->pushAction('dusk-event', [ |
|
103
|
|
|
'test' => $this->testName, |
|
104
|
|
|
'name' => 'XHR', |
|
105
|
|
|
'arguments' => [ |
|
106
|
|
|
data_get($request, 'message.params.request.method').' '. |
|
107
|
|
|
$url['path'].' '. |
|
108
|
|
|
data_get($response, 'message.params.response.status').' '. |
|
109
|
|
|
data_get($response, 'message.params.response.statusText'), |
|
110
|
|
|
], |
|
111
|
|
|
'html' => $browser->getCurrentPageSource(), |
|
112
|
|
|
'logs' => $log, |
|
113
|
|
|
]); |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
|