Passed
Pull Request — 1.x (#39)
by Kevin
04:31 queued 02:42
created

PantherBrowser::waitUntilNotVisible()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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