Passed
Push — 1.x ( 1354ab...4188f0 )
by Kevin
02:03
created

PantherBrowser::cookies()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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