Passed
Push — 1.x ( e8b990...e91c3c )
by Kevin
02:04
created

PantherBrowserTest   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 152
Duplicated Lines 0 %

Importance

Changes 23
Bugs 1 Features 9
Metric Value
eloc 77
c 23
b 1
f 9
dl 0
loc 152
rs 10
wmc 9

9 Methods

Rating   Name   Duplication   Size   Complexity  
A can_take_screenshot() 0 6 1
A can_check_if_element_is_visible_and_not_visible() 0 7 1
A can_wait_until_visible_and_not_visible() 0 22 1
A can_wait() 0 7 1
A can_wait_until_see_in_and_not_see_in() 0 23 1
A can_dump_console_log_with_throw_error() 0 11 1
A can_dump_console_log_with_console_error() 0 11 1
A can_save_console_log() 0 12 1
A browser() 0 3 1
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_throw_error(): void
150
    {
151
        $output = self::catchVarDumperOutput(function() {
152
            $this->browser()
153
                ->visit('/javascript')
154
                ->click('throw error')
155
                ->dumpConsoleLog()
156
            ;
157
        });
158
159
        $this->assertStringContainsString('Error: error object message', \json_encode($output, JSON_THROW_ON_ERROR));
160
    }
161
162
    protected function browser(): PantherBrowser
163
    {
164
        return $this->pantherBrowser();
165
    }
166
}
167