1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace LaravelDuskReporter; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Str; |
6
|
|
|
use Laravel\Dusk\Browser; |
7
|
|
|
use LaravelDuskReporter\Generation\ReportFileContract; |
8
|
|
|
|
9
|
|
|
trait HasDuskReporter |
10
|
|
|
{ |
11
|
|
|
public static ?ReportFileContract $duskReporter = null; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Set dusk reporter. |
15
|
|
|
* |
16
|
|
|
* @param ReportFileContract $reporter |
17
|
|
|
* |
18
|
|
|
* @return void |
19
|
|
|
*/ |
20
|
|
|
public static function withDuskReporter(?ReportFileContract $reporter): void |
21
|
|
|
{ |
22
|
|
|
static::$duskReporter = $reporter; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Callback to add text to report file. |
27
|
|
|
* |
28
|
|
|
* @param Browser $browser |
29
|
|
|
* @param \Closure $callback |
30
|
|
|
* |
31
|
|
|
* @return Browser |
32
|
|
|
*/ |
33
|
|
|
public function reportAppend(Browser $browser, \Closure $callback): Browser |
34
|
|
|
{ |
35
|
|
|
if (!is_null(static::$duskReporter)) { |
36
|
|
|
call_user_func($callback, static::$duskReporter, $browser); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
return $browser; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Add to report information about current screen. |
44
|
|
|
* |
45
|
|
|
* @param Browser $browser |
46
|
|
|
* @param string|null $caption |
47
|
|
|
* @param array $options |
48
|
|
|
* |
49
|
|
|
* @return Browser |
50
|
|
|
*/ |
51
|
|
|
public function reportUserSee(Browser $browser, ?string $caption = null, array $options = []): Browser |
52
|
|
|
{ |
53
|
|
|
if (!is_null(static::$duskReporter)) { |
54
|
|
|
if (isset($options['screenshot']) && is_array($options['screenshot'])) { |
55
|
|
|
call_user_func_array([ static::$duskReporter, 'screenshot' ], $options['screenshot']); |
56
|
|
|
} else { |
57
|
|
|
static::$duskReporter->screenshotWithCombineScreen($browser); |
58
|
|
|
} |
59
|
|
|
if ($caption) { |
60
|
|
|
static::$duskReporter->p($caption); |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
return $browser; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Add to report information about current page. |
69
|
|
|
* |
70
|
|
|
* @param Browser $browser |
71
|
|
|
* @param string|null $pageName |
72
|
|
|
* @param array $options |
73
|
|
|
* |
74
|
|
|
* @return Browser |
75
|
|
|
*/ |
76
|
|
|
public function reportUserSeePage(Browser $browser, ?string $pageName = null, array $options = []): Browser |
77
|
|
|
{ |
78
|
|
|
return $this->reportUserSee( |
79
|
|
|
$browser, |
80
|
|
|
trans('dusk-reporter::report.user_see_page', [ |
|
|
|
|
81
|
|
|
'page' => $pageName ?? Str::title(Str::kebab(Str::beforeLast(class_basename(get_class($this)), 'Page'), ' ')), |
|
|
|
|
82
|
|
|
]), |
83
|
|
|
$options |
84
|
|
|
); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|