Passed
Push — 1.x ( 13d0d8...e7706b )
by Kevin
01:47
created

PantherBrowser::savedArtifacts()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 5
rs 10
1
<?php
2
3
namespace Zenstruck\Browser;
4
5
use PHPUnit\Framework\Assert as PHPUnit;
0 ignored issues
show
Bug introduced by
The type PHPUnit\Framework\Assert was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use Symfony\Component\Filesystem\Filesystem;
7
use Symfony\Component\Panther\Client;
8
use Symfony\Component\VarDumper\VarDumper;
9
use Zenstruck\Browser;
10
use Zenstruck\Browser\Extension\Html;
11
use Zenstruck\Browser\Mink\PantherDriver;
12
use Zenstruck\Browser\Response\PantherResponse;
13
14
/**
15
 * @author Kevin Bond <[email protected]>
16
 *
17
 * @experimental in 1.0
18
 */
19
class PantherBrowser extends Browser
20
{
21
    use Html;
22
23
    private Client $client;
24
    private ?string $screenshotDir = null;
25
    private ?string $consoleLogDir = null;
26
    private array $savedScreenshots = [];
27
    private array $savedConsoleLogs = [];
28
29
    final public function __construct(Client $client)
30
    {
31
        parent::__construct(new PantherDriver($this->client = $client));
32
    }
33
34
    final public function client(): Client
35
    {
36
        return $this->client;
37
    }
38
39
    final public function setScreenshotDir(string $dir): self
40
    {
41
        $this->screenshotDir = $dir;
42
43
        return $this;
44
    }
45
46
    final public function setConsoleLogDir(string $dir): self
47
    {
48
        $this->consoleLogDir = $dir;
49
50
        return $this;
51
    }
52
53
    /**
54
     * @return static
55
     */
56
    final public function assertVisible(string $selector): self
57
    {
58
        return $this->wrapMinkExpectation(function() use ($selector) {
59
            $element = $this->webAssert()->elementExists('css', $selector);
60
61
            PHPUnit::assertTrue($element->isVisible());
62
        });
63
    }
64
65
    /**
66
     * @return static
67
     */
68
    final public function assertNotVisible(string $selector): self
69
    {
70
        $element = $this->documentElement()->find('css', $selector);
71
72
        if (!$element) {
73
            PHPUnit::assertTrue(true);
74
75
            return $this;
76
        }
77
78
        PHPUnit::assertFalse($element->isVisible());
79
80
        return $this;
81
    }
82
83
    /**
84
     * @return static
85
     */
86
    final public function wait(int $milliseconds): self
87
    {
88
        \usleep($milliseconds * 1000);
89
90
        return $this;
91
    }
92
93
    /**
94
     * @return static
95
     */
96
    final public function waitUntilVisible(string $selector): self
97
    {
98
        $this->client->waitForVisibility($selector);
99
100
        return $this;
101
    }
102
103
    /**
104
     * @return static
105
     */
106
    final public function waitUntilNotVisible(string $selector): self
107
    {
108
        $this->client->waitForInvisibility($selector);
109
110
        return $this;
111
    }
112
113
    /**
114
     * @return static
115
     */
116
    final public function waitUntilSeeIn(string $selector, string $expected): self
117
    {
118
        $this->client->waitForElementToContain($selector, $expected);
119
120
        return $this;
121
    }
122
123
    /**
124
     * @return static
125
     */
126
    final public function waitUntilNotSeeIn(string $selector, string $expected): self
127
    {
128
        $this->client->waitForElementToNotContain($selector, $expected);
129
130
        return $this;
131
    }
132
133
    /**
134
     * @return static
135
     */
136
    final public function inspect(): self
137
    {
138
        if (!($_SERVER['PANTHER_NO_HEADLESS'] ?? false)) {
139
            throw new \RuntimeException('The "PANTHER_NO_HEADLESS" env variable must be set to inspect.');
140
        }
141
142
        \fwrite(STDIN, "\n\nInspecting the browser.\n\nPress enter to continue...");
143
        \fgets(STDIN);
144
145
        return $this;
146
    }
147
148
    /**
149
     * @return static
150
     */
151
    final public function takeScreenshot(string $filename): self
152
    {
153
        if ($this->screenshotDir) {
154
            $filename = \sprintf('%s/%s', \rtrim($this->screenshotDir, '/'), \ltrim($filename, '/'));
155
        }
156
157
        $this->client->takeScreenshot($this->savedScreenshots[] = $filename);
158
159
        return $this;
160
    }
161
162
    final public function saveConsoleLog(string $filename): self
163
    {
164
        if ($this->consoleLogDir) {
165
            $filename = \sprintf('%s/%s', \rtrim($this->consoleLogDir, '/'), \ltrim($filename, '/'));
166
        }
167
168
        $log = $this->client->manage()->getLog('browser');
169
        $log = \json_encode($log, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_THROW_ON_ERROR);
170
171
        (new Filesystem())->dumpFile($this->savedConsoleLogs[] = $filename, $log);
172
173
        return $this;
174
    }
175
176
    final public function dumpConsoleLog(): self
177
    {
178
        VarDumper::dump($this->client->manage()->getLog('browser'));
179
180
        return $this;
181
    }
182
183
    final public function ddConsoleLog(): void
184
    {
185
        $this->dumpConsoleLog();
186
        $this->die();
187
    }
188
189
    final public function ddScreenshot(string $filename = 'screenshot.png'): void
190
    {
191
        $this->takeScreenshot($filename);
192
193
        echo \sprintf("\n\nScreenshot saved as \"%s\".\n\n", \end($this->savedScreenshots));
194
195
        $this->die();
196
    }
197
198
    /**
199
     * @internal
200
     */
201
    final public function dumpCurrentState(string $filename): void
202
    {
203
        parent::dumpCurrentState($filename);
204
205
        $this->takeScreenshot("{$filename}.png");
206
        $this->saveConsoleLog("{$filename}.log");
207
    }
208
209
    /**
210
     * @internal
211
     */
212
    public function savedArtifacts(): array
213
    {
214
        return \array_merge(
215
            parent::savedArtifacts(),
216
            ['Saved Console Logs' => $this->savedConsoleLogs, 'Saved Screenshots' => $this->savedScreenshots]
217
        );
218
    }
219
220
    protected function response(): PantherResponse
221
    {
222
        return new PantherResponse($this->minkSession());
223
    }
224
225
    protected function die(): void
226
    {
227
        $this->client->quit();
228
        parent::die();
229
    }
230
}
231