Passed
Pull Request — 1.x (#3)
by Kevin
01:46
created

PantherBrowserTest::browser()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
namespace Zenstruck\Browser\Tests;
4
5
use Symfony\Component\Panther\PantherTestCase;
6
use Symfony\Component\VarDumper\VarDumper;
7
use Zenstruck\Browser\PantherBrowser;
8
9
/**
10
 * @author Kevin Bond <[email protected]>
11
 *
12
 * @group panther
13
 */
14
final class PantherBrowserTest extends PantherTestCase
15
{
16
    use BrowserTests;
17
18
    /**
19
     * @test
20
     */
21
    public function can_take_screenshot(): void
22
    {
23
        $file = __DIR__.'/../var/browser/screenshots/screen.png';
24
25
        if (\file_exists($file)) {
26
            \unlink($file);
27
        }
28
29
        $this->browser()
30
            ->visit('/page1')
31
            ->takeScreenshot('screen.png')
32
        ;
33
34
        $this->assertFileExists($file);
35
36
        \unlink($file);
37
    }
38
39
    /**
40
     * @test
41
     */
42
    public function can_wait(): void
43
    {
44
        $this->browser()
45
            ->visit('/javascript')
46
            ->assertNotSeeIn('#timeout-box', 'Contents of timeout box')
47
            ->wait(600)
48
            ->assertSeeIn('#timeout-box', 'Contents of timeout box')
49
        ;
50
    }
51
52
    /**
53
     * @test
54
     */
55
    public function can_wait_until_visible_and_not_visible(): void
56
    {
57
        $this->browser()
58
            ->visit('/javascript')
59
            ->assertNotSeeIn('#timeout-box', 'Contents of timeout box')
60
            ->waitUntilVisible('#timeout-box')
61
            ->assertSeeIn('#timeout-box', 'Contents of timeout box')
62
            ->assertNotSeeIn('#show-box', 'Contents of show box')
63
            ->click('show')
64
            ->waitUntilVisible('#show-box')
65
            ->assertSeeIn('#show-box', 'Contents of show box')
66
            ->assertSeeIn('#hide-box', 'Contents of hide box')
67
            ->click('hide')
68
            ->waitUntilNotVisible('#hide-box')
69
            ->assertNotSeeIn('#hide-box', 'Contents of hide box')
70
            ->assertSeeIn('#toggle-box', 'Contents of toggle box')
71
            ->click('toggle')
72
            ->waitUntilNotVisible('#toggle-box')
73
            ->assertNotSeeIn('#toggle-box', 'Contents of toggle box')
74
            ->click('toggle')
75
            ->waitUntilVisible('#toggle-box')
76
            ->assertSeeIn('#toggle-box', 'Contents of toggle box')
77
        ;
78
    }
79
80
    /**
81
     * @test
82
     */
83
    public function can_wait_until_see_in_and_not_see_in(): void
84
    {
85
        $this->browser()
86
            ->visit('/javascript')
87
            ->assertNotSeeIn('#timeout-box', 'Contents of timeout box')
88
            ->waitUntilSeeIn('#timeout-box', 'timeout')
89
            ->assertSeeIn('#timeout-box', 'Contents of timeout box')
90
            ->assertNotSeeIn('#show-box', 'Contents of show box')
91
            ->click('show')
92
            ->waitUntilSeeIn('#show-box', 'show')
93
            ->assertSeeIn('#show-box', 'Contents of show box')
94
            ->assertSeeIn('#hide-box', 'Contents of hide box')
95
            ->click('hide')
96
            ->waitUntilNotSeeIn('#hide-box', 'hide')
97
            ->assertNotSeeIn('#hide-box', 'Contents of hide box')
98
            ->assertNotSeeIn('#output', 'some text')
99
            ->fillField('input', 'some text')
100
            ->click('submit')
101
            ->waitUntilSeeIn('#output', 'some text')
102
            ->assertSeeIn('#output', 'some text')
103
            ->click('clear')
104
            ->waitUntilNotSeeIn('#output', 'some text')
105
            ->assertNotSeeIn('#output', 'some text')
106
        ;
107
    }
108
109
    /**
110
     * @test
111
     */
112
    public function can_check_if_element_is_visible_and_not_visible(): void
113
    {
114
        $this->browser()
115
            ->visit('/javascript')
116
            ->assertVisible('#hide-box')
117
            ->assertNotVisible('#show-box')
118
            ->assertNotVisible('#invalid-element')
119
        ;
120
    }
121
122
    /**
123
     * @test
124
     */
125
    public function can_save_console_log(): void
126
    {
127
        $file = __DIR__.'/../var/browser/console-logs/console.log';
128
129
        if (\file_exists($file)) {
130
            \unlink($file);
131
        }
132
133
        $this->browser()
134
            ->visit('/javascript')
135
            ->click('log')
136
            ->saveConsoleLog('console.log')
137
        ;
138
139
        $this->assertFileExists($file);
140
141
        $contents = \file_get_contents($file);
142
143
        $this->assertStringContainsString('        "level": "SEVERE",', $contents);
144
        $this->assertStringContainsString('error!', $contents);
145
146
        \unlink($file);
147
    }
148
149
    /**
150
     * @test
151
     */
152
    public function can_dump_console_log(): void
153
    {
154
        $dumpedValues[] = null;
0 ignored issues
show
Comprehensibility Best Practice introduced by
$dumpedValues was never initialized. Although not strictly required by PHP, it is generally a good practice to add $dumpedValues = array(); before regardless.
Loading history...
155
156
        VarDumper::setHandler(function($var) use (&$dumpedValues) {
157
            $dumpedValues[] = $var;
158
        });
159
160
        $this->browser()
161
            ->visit('/javascript')
162
            ->click('log')
163
            ->dumpConsoleLog()
164
        ;
165
166
        VarDumper::setHandler();
167
168
        // a null value is added to the beginning
169
        $dumpedValues = \array_values(\array_filter($dumpedValues));
170
171
        $this->assertSame('SEVERE', $dumpedValues[0][0]['level']);
172
        $this->assertStringContainsString('error!', $dumpedValues[0][0]['message']);
173
    }
174
175
    protected function browser(): PantherBrowser
176
    {
177
        return $this->pantherBrowser();
178
    }
179
}
180