| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Zenstruck\Browser\Tests; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use PHPUnit\Framework\AssertionFailedError; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Symfony\Component\Panther\PantherTestCase; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 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_use_panther_browser_as_typehint(): void | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  |         $this->browser() | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  |             ->use(function(PantherBrowser $browser) { | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  |                 $browser->visit('/page1'); | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |             }) | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |             ->assertOn('/page1') | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |         ; | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |      * @test | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |     public function can_take_screenshot(): void | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |         self::catchFileContents(__DIR__.'/../var/browser/screenshots/screen.png', function() { | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |             $this->browser() | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |                 ->visit('/page1') | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |                 ->takeScreenshot('screen.png') | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |             ; | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |         }); | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |      * @test | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |     public function can_wait(): void | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |         $this->browser() | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |             ->visit('/javascript') | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |             ->assertNotSeeIn('#timeout-box', 'Contents of timeout box') | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |             ->wait(600) | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |             ->assertSeeIn('#timeout-box', 'Contents of timeout box') | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |         ; | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |      * @test | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |     public function can_wait_until_visible_and_not_visible(): void | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |         $this->browser() | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |             ->visit('/javascript') | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |             ->assertNotSeeIn('#timeout-box', 'Contents of timeout box') | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |             ->waitUntilVisible('#timeout-box') | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |             ->assertSeeIn('#timeout-box', 'Contents of timeout box') | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  |             ->assertNotSeeIn('#show-box', 'Contents of show box') | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  |             ->click('show') | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  |             ->waitUntilVisible('#show-box') | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  |             ->assertSeeIn('#show-box', 'Contents of show box') | 
            
                                                                        
                            
            
                                    
            
            
                | 71 |  |  |             ->assertSeeIn('#hide-box', 'Contents of hide box') | 
            
                                                                        
                            
            
                                    
            
            
                | 72 |  |  |             ->click('hide') | 
            
                                                                        
                            
            
                                    
            
            
                | 73 |  |  |             ->waitUntilNotVisible('#hide-box') | 
            
                                                                        
                            
            
                                    
            
            
                | 74 |  |  |             ->assertNotSeeIn('#hide-box', 'Contents of hide box') | 
            
                                                                        
                            
            
                                    
            
            
                | 75 |  |  |             ->assertSeeIn('#toggle-box', 'Contents of toggle box') | 
            
                                                                        
                            
            
                                    
            
            
                | 76 |  |  |             ->click('toggle') | 
            
                                                                        
                            
            
                                    
            
            
                | 77 |  |  |             ->waitUntilNotVisible('#toggle-box') | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  |             ->assertNotSeeIn('#toggle-box', 'Contents of toggle box') | 
            
                                                                        
                            
            
                                    
            
            
                | 79 |  |  |             ->click('toggle') | 
            
                                                                        
                            
            
                                    
            
            
                | 80 |  |  |             ->waitUntilVisible('#toggle-box') | 
            
                                                                        
                            
            
                                    
            
            
                | 81 |  |  |             ->assertSeeIn('#toggle-box', 'Contents of toggle box') | 
            
                                                                        
                            
            
                                    
            
            
                | 82 |  |  |         ; | 
            
                                                                        
                            
            
                                    
            
            
                | 83 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 84 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 85 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 86 |  |  |      * @test | 
            
                                                                        
                            
            
                                    
            
            
                | 87 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 88 |  |  |     public function can_wait_until_see_in_and_not_see_in(): void | 
            
                                                                        
                            
            
                                    
            
            
                | 89 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 90 |  |  |         $this->browser() | 
            
                                                                        
                            
            
                                    
            
            
                | 91 |  |  |             ->visit('/javascript') | 
            
                                                                        
                            
            
                                    
            
            
                | 92 |  |  |             ->assertNotSeeIn('#timeout-box', 'Contents of timeout box') | 
            
                                                                        
                            
            
                                    
            
            
                | 93 |  |  |             ->waitUntilSeeIn('#timeout-box', 'timeout') | 
            
                                                                        
                            
            
                                    
            
            
                | 94 |  |  |             ->assertSeeIn('#timeout-box', 'Contents of timeout box') | 
            
                                                                        
                            
            
                                    
            
            
                | 95 |  |  |             ->assertNotSeeIn('#show-box', 'Contents of show box') | 
            
                                                                        
                            
            
                                    
            
            
                | 96 |  |  |             ->click('show') | 
            
                                                                        
                            
            
                                    
            
            
                | 97 |  |  |             ->waitUntilSeeIn('#show-box', 'show') | 
            
                                                                        
                            
            
                                    
            
            
                | 98 |  |  |             ->assertSeeIn('#show-box', 'Contents of show box') | 
            
                                                                        
                            
            
                                    
            
            
                | 99 |  |  |             ->assertSeeIn('#hide-box', 'Contents of hide box') | 
            
                                                                        
                            
            
                                    
            
            
                | 100 |  |  |             ->click('hide') | 
            
                                                                        
                            
            
                                    
            
            
                | 101 |  |  |             ->waitUntilNotSeeIn('#hide-box', 'hide') | 
            
                                                                        
                            
            
                                    
            
            
                | 102 |  |  |             ->assertNotSeeIn('#hide-box', 'Contents of hide box') | 
            
                                                                        
                            
            
                                    
            
            
                | 103 |  |  |             ->assertNotSeeIn('#output', 'some text') | 
            
                                                                        
                            
            
                                    
            
            
                | 104 |  |  |             ->fillField('input', 'some text') | 
            
                                                                        
                            
            
                                    
            
            
                | 105 |  |  |             ->click('submit') | 
            
                                                                        
                            
            
                                    
            
            
                | 106 |  |  |             ->waitUntilSeeIn('#output', 'some text') | 
            
                                                                        
                            
            
                                    
            
            
                | 107 |  |  |             ->assertSeeIn('#output', 'some text') | 
            
                                                                        
                            
            
                                    
            
            
                | 108 |  |  |             ->click('clear') | 
            
                                                                        
                            
            
                                    
            
            
                | 109 |  |  |             ->waitUntilNotSeeIn('#output', 'some text') | 
            
                                                                        
                            
            
                                    
            
            
                | 110 |  |  |             ->assertNotSeeIn('#output', 'some text') | 
            
                                                                        
                            
            
                                    
            
            
                | 111 |  |  |         ; | 
            
                                                                        
                            
            
                                    
            
            
                | 112 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 113 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 114 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 115 |  |  |      * @test | 
            
                                                                        
                            
            
                                    
            
            
                | 116 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 117 |  |  |     public function can_check_if_element_is_visible_and_not_visible(): void | 
            
                                                                        
                            
            
                                    
            
            
                | 118 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 119 |  |  |         $this->browser() | 
            
                                                                        
                            
            
                                    
            
            
                | 120 |  |  |             ->visit('/javascript') | 
            
                                                                        
                            
            
                                    
            
            
                | 121 |  |  |             ->assertVisible('#hide-box') | 
            
                                                                        
                            
            
                                    
            
            
                | 122 |  |  |             ->assertNotVisible('#show-box') | 
            
                                                                        
                            
            
                                    
            
            
                | 123 |  |  |             ->assertNotVisible('#invalid-element') | 
            
                                                                        
                            
            
                                    
            
            
                | 124 |  |  |         ; | 
            
                                                                        
                            
            
                                    
            
            
                | 125 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 126 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 127 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 128 |  |  |      * @test | 
            
                                                                        
                            
            
                                    
            
            
                | 129 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 130 |  |  |     public function can_save_console_log(): void | 
            
                                                                        
                            
            
                                    
            
            
                | 131 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 132 |  |  |         $contents = self::catchFileContents(__DIR__.'/../var/browser/console-logs/console.log', function() { | 
            
                                                                        
                            
            
                                    
            
            
                | 133 |  |  |             $this->browser() | 
            
                                                                        
                            
            
                                    
            
            
                | 134 |  |  |                 ->visit('/javascript') | 
            
                                                                        
                            
            
                                    
            
            
                | 135 |  |  |                 ->click('log error') | 
            
                                                                        
                            
            
                                    
            
            
                | 136 |  |  |                 ->saveConsoleLog('console.log') | 
            
                                                                        
                            
            
                                    
            
            
                | 137 |  |  |             ; | 
            
                                                                        
                            
            
                                    
            
            
                | 138 |  |  |         }); | 
            
                                                                        
                            
            
                                    
            
            
                | 139 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 140 |  |  |         $this->assertStringContainsString('        "level": "SEVERE",', $contents); | 
            
                                                                        
                            
            
                                    
            
            
                | 141 |  |  |         $this->assertStringContainsString('console.error message', $contents); | 
            
                                                                        
                            
            
                                    
            
            
                | 142 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 143 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 144 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 145 |  |  |      * @test | 
            
                                                                        
                            
            
                                    
            
            
                | 146 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 147 |  |  |     public function can_dump_console_log_with_console_error(): void | 
            
                                                                        
                            
            
                                    
            
            
                | 148 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 149 |  |  |         $output = self::catchVarDumperOutput(function() { | 
            
                                                                        
                            
            
                                    
            
            
                | 150 |  |  |             $this->browser() | 
            
                                                                        
                            
            
                                    
            
            
                | 151 |  |  |                 ->visit('/javascript') | 
            
                                                                        
                            
            
                                    
            
            
                | 152 |  |  |                 ->click('log error') | 
            
                                                                        
                            
            
                                    
            
            
                | 153 |  |  |                 ->dumpConsoleLog() | 
            
                                                                        
                            
            
                                    
            
            
                | 154 |  |  |             ; | 
            
                                                                        
                            
            
                                    
            
            
                | 155 |  |  |         }); | 
            
                                                                        
                            
            
                                    
            
            
                | 156 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 157 |  |  |         $this->assertStringContainsString('console.error message', \json_encode($output, \JSON_THROW_ON_ERROR)); | 
            
                                                                        
                            
            
                                    
            
            
                | 158 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 159 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 160 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 161 |  |  |      * @test | 
            
                                                                        
                            
            
                                    
            
            
                | 162 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |     public function can_dump_console_log_with_throw_error(): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |         $output = self::catchVarDumperOutput(function() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |             $this->browser() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |                 ->visit('/javascript') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |                 ->click('throw error') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |                 ->dumpConsoleLog() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |             ; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  |         }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 173 |  |  |         $this->assertStringContainsString('Error: error object message', \json_encode($output, \JSON_THROW_ON_ERROR)); | 
            
                                                                        
                                                                
            
                                    
            
            
                | 174 |  |  |     } | 
            
                                                                        
                                                                
            
                                    
            
            
                | 175 |  |  |  | 
            
                                                                        
                                                                
            
                                    
            
            
                | 176 |  |  |     /** | 
            
                                                                        
                                                                
            
                                    
            
            
                | 177 |  |  |      * @test | 
            
                                                                        
                                                                
            
                                    
            
            
                | 178 |  |  |      */ | 
            
                                                                        
                                                                
            
                                    
            
            
                | 179 |  |  |     public function cannot_follow_invisible_link(): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 |  |  |         $this->expectException(AssertionFailedError::class); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 182 |  |  |         $this->expectErrorMessage('Link "invisible link" is not visible.'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 184 |  |  |         $this->browser() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 185 |  |  |             ->visit('/javascript') | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 186 |  |  |             ->follow('invisible link') | 
            
                                                                        
                                                                
            
                                    
            
            
                | 187 |  |  |         ; | 
            
                                                                        
                                                                
            
                                    
            
            
                | 188 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 189 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 190 |  |  |     protected function browser(): PantherBrowser | 
            
                                                                                                            
                            
            
                                    
            
            
                | 191 |  |  |     { | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 192 |  |  |         return $this->pantherBrowser(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 193 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 194 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 195 |  |  |  |