Passed
Pull Request — 1.x (#9)
by Kevin
01:44
created

PantherBrowserTest::can_wait()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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