Passed
Push — 1.x ( 86ffba...69ed14 )
by Kevin
01:56
created

PantherBrowser::dumpCurrentState()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 6
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\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
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 assertVisible(string $selector): self
55
    {
56
        return $this->wrapMinkExpectation(function() use ($selector) {
57
            $element = $this->webAssert()->elementExists('css', $selector);
58
59
            PHPUnit::assertTrue($element->isVisible());
60
        });
61
    }
62
63
    /**
64
     * @return static
65
     */
66
    final public function assertNotVisible(string $selector): self
67
    {
68
        $element = $this->documentElement()->find('css', $selector);
69
70
        if (!$element) {
71
            PHPUnit::assertTrue(true);
72
73
            return $this;
74
        }
75
76
        PHPUnit::assertFalse($element->isVisible());
77
78
        return $this;
79
    }
80
81
    /**
82
     * @return static
83
     */
84
    final public function wait(int $milliseconds): self
85
    {
86
        \usleep($milliseconds * 1000);
87
88
        return $this;
89
    }
90
91
    /**
92
     * @return static
93
     */
94
    final public function waitUntilVisible(string $selector): self
95
    {
96
        $this->client->waitForVisibility($selector);
97
98
        return $this;
99
    }
100
101
    /**
102
     * @return static
103
     */
104
    final public function waitUntilNotVisible(string $selector): self
105
    {
106
        $this->client->waitForInvisibility($selector);
107
108
        return $this;
109
    }
110
111
    /**
112
     * @return static
113
     */
114
    final public function waitUntilSeeIn(string $selector, string $expected): self
115
    {
116
        $this->client->waitForElementToContain($selector, $expected);
117
118
        return $this;
119
    }
120
121
    /**
122
     * @return static
123
     */
124
    final public function waitUntilNotSeeIn(string $selector, string $expected): self
125
    {
126
        $this->client->waitForElementToNotContain($selector, $expected);
127
128
        return $this;
129
    }
130
131
    /**
132
     * @return static
133
     */
134
    final public function inspect(): self
135
    {
136
        if (!($_SERVER['PANTHER_NO_HEADLESS'] ?? false)) {
137
            throw new \RuntimeException('The "PANTHER_NO_HEADLESS" env variable must be set to inspect.');
138
        }
139
140
        \fwrite(STDIN, "\n\nInspecting the browser.\n\nPress enter to continue...");
141
        \fgets(STDIN);
142
143
        return $this;
144
    }
145
146
    /**
147
     * @return static
148
     */
149
    final public function takeScreenshot(string $filename): self
150
    {
151
        if ($this->screenshotDir) {
152
            $filename = \sprintf('%s/%s', \rtrim($this->screenshotDir, '/'), \ltrim($filename, '/'));
153
        }
154
155
        $this->client->takeScreenshot($filename);
156
157
        return $this;
158
    }
159
160
    final public function saveConsoleLog(string $filename): self
161
    {
162
        if ($this->consoleLogDir) {
163
            $filename = \sprintf('%s/%s', \rtrim($this->consoleLogDir, '/'), \ltrim($filename, '/'));
164
        }
165
166
        $log = $this->client->manage()->getLog('browser');
167
        $log = \json_encode($log, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_THROW_ON_ERROR);
168
169
        (new Filesystem())->dumpFile($filename, $log);
170
171
        return $this;
172
    }
173
174
    final public function dumpConsoleLog(): self
175
    {
176
        VarDumper::dump($this->client->manage()->getLog('browser'));
177
178
        return $this;
179
    }
180
181
    final public function ddConsoleLog(): void
182
    {
183
        $this->dumpConsoleLog();
184
        $this->die();
185
    }
186
187
    final public function ddScreenshot(string $filename = 'screenshot.png'): void
188
    {
189
        $this->takeScreenshot($filename);
190
191
        // todo show real filename
192
        echo "\n\nScreenshot saved as \"{$filename}\".\n\n";
193
194
        $this->die();
195
    }
196
197
    /**
198
     * @internal
199
     */
200
    final public function dumpCurrentState(string $filename): void
201
    {
202
        parent::dumpCurrentState($filename);
203
204
        $this->takeScreenshot("{$filename}.png");
205
        $this->saveConsoleLog("{$filename}.log");
206
    }
207
208
    protected function response(): PantherResponse
209
    {
210
        return new PantherResponse($this->minkSession());
211
    }
212
213
    protected function die(): void
214
    {
215
        $this->client->quit();
216
        parent::die();
217
    }
218
}
219