|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +use Pest\Browser\Playwright\Element; |
| 6 | + |
| 7 | +describe('Frame', function () { |
| 8 | + beforeEach(function () { |
| 9 | + $this->page = $this->page(playgroundUrl('/test/selector-tests')); |
| 10 | + }); |
| 11 | + |
| 12 | + describe('getByTestId', function () { |
| 13 | + it('finds an element by test ID', function () { |
| 14 | + $element = $this->page->getByTestId('profile-section'); |
| 15 | + |
| 16 | + expect($element)->toBeInstanceOf(Element::class); |
| 17 | + expect($element->isVisible())->toBeTrue(); |
| 18 | + }); |
| 19 | + |
| 20 | + it('finds a nested element by test ID', function () { |
| 21 | + $element = $this->page->getByTestId('user-email'); |
| 22 | + |
| 23 | + expect($element)->toBeInstanceOf(Element::class); |
| 24 | + expect($element->isVisible())->toBeTrue(); |
| 25 | + }); |
| 26 | + |
| 27 | + it('returns null for non-existent test ID', function () { |
| 28 | + $element = $this->page->getByTestId('non-existent-id'); |
| 29 | + |
| 30 | + expect($element)->toBeNull(); |
| 31 | + }); |
| 32 | + }); |
| 33 | + |
| 34 | + describe('getByRole', function () { |
| 35 | + it('finds an element by role with name option', function () { |
| 36 | + $element = $this->page->getByRole('button', ['name' => 'Save']); |
| 37 | + |
| 38 | + expect($element)->toBeInstanceOf(Element::class); |
| 39 | + expect($element->isVisible())->toBeTrue(); |
| 40 | + }); |
| 41 | + |
| 42 | + it('finds a checkbox by role with name option', function () { |
| 43 | + $element = $this->page->getByRole('checkbox', ['name' => 'Remember Me']); |
| 44 | + |
| 45 | + expect($element)->toBeInstanceOf(Element::class); |
| 46 | + }); |
| 47 | + |
| 48 | + it('returns null for non-existent role', function () { |
| 49 | + $element = $this->page->getByRole('tab', ['name' => 'Non-existent']); |
| 50 | + |
| 51 | + expect($element)->toBeNull(); |
| 52 | + }); |
| 53 | + }); |
| 54 | + |
| 55 | + describe('getByLabel', function () { |
| 56 | + it('finds an input element by its associated label', function () { |
| 57 | + $element = $this->page->getByLabel('Username'); |
| 58 | + |
| 59 | + expect($element)->toBeInstanceOf(Element::class); |
| 60 | + expect($element->getAttribute('value'))->toBe('johndoe'); |
| 61 | + }); |
| 62 | + |
| 63 | + it('finds a password input by its label', function () { |
| 64 | + $element = $this->page->getByLabel('Password'); |
| 65 | + |
| 66 | + expect($element)->toBeInstanceOf(Element::class); |
| 67 | + expect($element->getAttribute('type'))->toBe('password'); |
| 68 | + }); |
| 69 | + |
| 70 | + it('returns null for non-existent label', function () { |
| 71 | + $element = $this->page->getByLabel('Non-existent Label'); |
| 72 | + |
| 73 | + expect($element)->toBeNull(); |
| 74 | + }); |
| 75 | + }); |
| 76 | + |
| 77 | + describe('getByPlaceholder', function () { |
| 78 | + it('finds an input element by placeholder text', function () { |
| 79 | + $element = $this->page->getByPlaceholder('Search...'); |
| 80 | + |
| 81 | + expect($element)->toBeInstanceOf(Element::class); |
| 82 | + expect($element->getAttribute('type'))->toBe('text'); |
| 83 | + }); |
| 84 | + |
| 85 | + it('finds a textarea by placeholder text', function () { |
| 86 | + $element = $this->page->getByPlaceholder('Enter your comments here'); |
| 87 | + |
| 88 | + expect($element)->toBeInstanceOf(Element::class); |
| 89 | + expect($element->isVisible())->toBeTrue(); |
| 90 | + }); |
| 91 | + |
| 92 | + it('returns null for non-existent placeholder', function () { |
| 93 | + $element = $this->page->getByPlaceholder('Non-existent Placeholder'); |
| 94 | + |
| 95 | + expect($element)->toBeNull(); |
| 96 | + }); |
| 97 | + |
| 98 | + it('finds an element with exact matching', function () { |
| 99 | + $element = $this->page->getByPlaceholder('Search...', true); |
| 100 | + |
| 101 | + expect($element)->toBeInstanceOf(Element::class); |
| 102 | + }); |
| 103 | + }); |
| 104 | + |
| 105 | + describe('getByText', function () { |
| 106 | + it('finds an element by its text content', function () { |
| 107 | + $element = $this->page->getByText('This is a simple paragraph'); |
| 108 | + |
| 109 | + expect($element)->toBeInstanceOf(Element::class); |
| 110 | + expect($element->isVisible())->toBeTrue(); |
| 111 | + }); |
| 112 | + |
| 113 | + it('finds a button by its text content', function () { |
| 114 | + $element = $this->page->getByText('Click Me Button'); |
| 115 | + |
| 116 | + expect($element)->toBeInstanceOf(Element::class); |
| 117 | + }); |
| 118 | + |
| 119 | + it('returns null for non-existent text', function () { |
| 120 | + $element = $this->page->getByText('Non-existent Text Content'); |
| 121 | + |
| 122 | + expect($element)->toBeNull(); |
| 123 | + }); |
| 124 | + |
| 125 | + it('finds an element with exact matching', function () { |
| 126 | + $element = $this->page->getByText('This is a special span element', true); |
| 127 | + |
| 128 | + expect($element)->toBeInstanceOf(Element::class); |
| 129 | + }); |
| 130 | + |
| 131 | + it('finds partial text without exact matching', function () { |
| 132 | + $element = $this->page->getByText('special span'); |
| 133 | + |
| 134 | + expect($element)->toBeInstanceOf(Element::class); |
| 135 | + }); |
| 136 | + }); |
| 137 | + |
| 138 | + describe('getByAltText', function () { |
| 139 | + it('finds an image by its alt text', function () { |
| 140 | + $element = $this->page->getByAltText('Pest Logo'); |
| 141 | + |
| 142 | + expect($element)->toBeInstanceOf(Element::class); |
| 143 | + expect($element->isVisible())->toBeTrue(); |
| 144 | + }); |
| 145 | + |
| 146 | + it('finds another image by its alt text', function () { |
| 147 | + $element = $this->page->getByAltText('Another Image'); |
| 148 | + |
| 149 | + expect($element)->toBeInstanceOf(Element::class); |
| 150 | + }); |
| 151 | + |
| 152 | + it('returns null for non-existent alt text', function () { |
| 153 | + $element = $this->page->getByAltText('Non-existent Alt Text'); |
| 154 | + |
| 155 | + expect($element)->toBeNull(); |
| 156 | + }); |
| 157 | + |
| 158 | + it('finds an element with exact matching', function () { |
| 159 | + $element = $this->page->getByAltText('Profile Picture', true); |
| 160 | + |
| 161 | + expect($element)->toBeInstanceOf(Element::class); |
| 162 | + }); |
| 163 | + }); |
| 164 | + |
| 165 | + describe('getByTitle', function () { |
| 166 | + it('finds an element by its title attribute', function () { |
| 167 | + $element = $this->page->getByTitle('Info Button'); |
| 168 | + |
| 169 | + expect($element)->toBeInstanceOf(Element::class); |
| 170 | + expect($element->isVisible())->toBeTrue(); |
| 171 | + }); |
| 172 | + |
| 173 | + it('finds a link by its title attribute', function () { |
| 174 | + $element = $this->page->getByTitle('Help Link'); |
| 175 | + |
| 176 | + expect($element)->toBeInstanceOf(Element::class); |
| 177 | + }); |
| 178 | + |
| 179 | + it('returns null for non-existent title', function () { |
| 180 | + $element = $this->page->getByTitle('Non-existent Title'); |
| 181 | + |
| 182 | + expect($element)->toBeNull(); |
| 183 | + }); |
| 184 | + |
| 185 | + it('finds an element with exact matching', function () { |
| 186 | + $element = $this->page->getByTitle('User\'s Name', true); |
| 187 | + |
| 188 | + expect($element)->toBeInstanceOf(Element::class); |
| 189 | + }); |
| 190 | + }); |
| 191 | + |
| 192 | + describe('combining selectors', function () { |
| 193 | + it('can find elements using multiple methods in sequence', function () { |
| 194 | + // First get the profile section by testId |
| 195 | + $profileSection = $this->page->getByTestId('user-profile'); |
| 196 | + expect($profileSection)->toBeInstanceOf(Element::class); |
| 197 | + |
| 198 | + // Then find a button element with role and aria-label within it |
| 199 | + $button = $this->page->getByRole('button', ['name' => 'Edit Profile']); |
| 200 | + expect($button)->toBeInstanceOf(Element::class); |
| 201 | + |
| 202 | + // Verify it has the right content |
| 203 | + expect($button->isVisible())->toBeTrue(); |
| 204 | + }); |
| 205 | + }); |
| 206 | +}); |
0 commit comments