Passed
Push — 1.x ( de413f...885d6a )
by Kevin
01:48
created

PantherBrowserTest::html_head_assertions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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