Skip to content

Commit 30e02d5

Browse files
committed
feat: add tests for handling non-existent elements in various locator methods
1 parent d13735e commit 30e02d5

11 files changed

Lines changed: 161 additions & 11 deletions

File tree

playground/resources/views/test-pages/element-tests.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@
138138

139139
<div class="form-group">
140140
<label for="comments">Comments</label>
141-
<textarea id="comments" name="comments" placeholder="Enter your comments here"></textarea>
141+
<textarea id="comments" name="comments" data-testid="textarea-input" placeholder="Enter your comments here"></textarea>
142142
</div>
143143

144144
<div class="form-group">

tests/Browser/Playwright/Locator/BoundingBoxTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,12 @@
3838
expect($boundingBox['width'])->toBeGreaterThan(0);
3939
expect($boundingBox['height'])->toBeGreaterThan(0);
4040
});
41+
42+
it('returns null when boundingBox element is not found', function (): void {
43+
$page = page()->goto('/test/element-tests');
44+
$locator = $page->locator('.non-existent-element');
45+
46+
$boundingBox = $locator->boundingBox();
47+
48+
expect($boundingBox)->toBeNull();
49+
});

tests/Browser/Playwright/Locator/ContentFrameTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,11 @@
2323

2424
expect($frame)->toBeNull();
2525
});
26+
27+
it('throws RuntimeException when contentFrame element is not found', function (): void {
28+
$page = page()->goto('/test/element-tests');
29+
$locator = $page->locator('.non-existent-element');
30+
31+
expect(fn() => $locator->contentFrame())
32+
->toThrow(RuntimeException::class, 'Element not found');
33+
});

tests/Browser/Playwright/Locator/FilterTest.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,3 +80,43 @@
8080

8181
expect($filteredButtons)->toBeInstanceOf(Locator::class);
8282
});
83+
84+
it('handles non-string hasText in filter options', function (): void {
85+
$page = page()->goto('/test/element-tests');
86+
$buttons = $page->locator('button');
87+
88+
// Test with non-string hasText (this should be ignored based on the code)
89+
$filteredButtons = $buttons->filter(['hasText' => 123]);
90+
91+
expect($filteredButtons)->toBeInstanceOf(Locator::class);
92+
});
93+
94+
it('handles non-string hasNotText in filter options', function (): void {
95+
$page = page()->goto('/test/element-tests');
96+
$buttons = $page->locator('button');
97+
98+
// Test with non-string hasNotText (this should be ignored based on the code)
99+
$filteredButtons = $buttons->filter(['hasNotText' => 123]);
100+
101+
expect($filteredButtons)->toBeInstanceOf(Locator::class);
102+
});
103+
104+
it('handles non-locator has in filter options', function (): void {
105+
$page = page()->goto('/test/element-tests');
106+
$buttons = $page->locator('button');
107+
108+
// Test with non-locator has (this should be ignored based on the code)
109+
$filteredButtons = $buttons->filter(['has' => 'not-a-locator']);
110+
111+
expect($filteredButtons)->toBeInstanceOf(Locator::class);
112+
});
113+
114+
it('handles non-locator hasNot in filter options', function (): void {
115+
$page = page()->goto('/test/element-tests');
116+
$buttons = $page->locator('button');
117+
118+
// Test with non-locator hasNot (this should be ignored based on the code)
119+
$filteredButtons = $buttons->filter(['hasNot' => 'not-a-locator']);
120+
121+
expect($filteredButtons)->toBeInstanceOf(Locator::class);
122+
});

tests/Browser/Playwright/Locator/FrameAndWaitTest.php

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -68,20 +68,18 @@
6868
expect($button->isEnabled())->toBeTrue();
6969
});
7070

71-
it('can wait for selector to appear relative to locator', function (): void {
71+
it('throws RuntimeException when waitForElementState element is not found', function (): void {
7272
$page = page()->goto('/test/element-tests');
73-
$container = $page->getByTestId('profile-section');
73+
$locator = $page->locator('.non-existent-element');
7474

75-
$childLocator = $container->waitForSelector('h2');
76-
77-
expect($childLocator)->toBeInstanceOf(Locator::class);
75+
expect(fn() => $locator->waitForElementState('visible'))
76+
->toThrow(RuntimeException::class, 'Element not found');
7877
});
7978

80-
it('returns null when waitForSelector times out', function (): void {
79+
it('throws RuntimeException when waitForState element is not found', function (): void {
8180
$page = page()->goto('/test/element-tests');
82-
$container = $page->getByTestId('profile-section');
83-
84-
$result = $container->waitForSelector('.definitely-non-existent-element-12345', ['timeout' => 100]);
81+
$locator = $page->locator('.non-existent-element');
8582

86-
expect($result)->toBeNull();
83+
expect(fn() => $locator->waitForState('visible'))
84+
->toThrow(RuntimeException::class, 'Element not found');
8785
});

tests/Browser/Playwright/Locator/InputMethodsTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,19 @@
2626
$checkbox->setChecked(true);
2727
expect($checkbox->isChecked())->toBeTrue();
2828
});
29+
30+
it('throws RuntimeException when selectText element is not found', function (): void {
31+
$page = page()->goto('/test/element-tests');
32+
$locator = $page->locator('.non-existent-element');
33+
34+
expect(fn() => $locator->selectText())
35+
->toThrow(RuntimeException::class, 'Element not found');
36+
});
37+
38+
it('throws RuntimeException when setChecked element is not found', function (): void {
39+
$page = page()->goto('/test/element-tests');
40+
$locator = $page->locator('.non-existent-element');
41+
42+
expect(fn() => $locator->setChecked(true))
43+
->toThrow(RuntimeException::class, 'Element not found');
44+
});
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
it('returns true for editable input elements', function (): void {
6+
$page = page()->goto('/test/element-tests');
7+
$inputLocator = $page->getByTestId('text-input');
8+
9+
expect($inputLocator->isEditable())->toBeTrue();
10+
});
11+
12+
it('returns true for textarea elements', function (): void {
13+
$page = page()->goto('/test/element-tests');
14+
$textareaLocator = $page->getByTestId('textarea-input');
15+
16+
expect($textareaLocator->isEditable())->toBeTrue();
17+
});
18+
19+
it('throws RuntimeException for non-editable elements', function (): void {
20+
$page = page()->goto('/test/element-tests');
21+
$divLocator = $page->getByTestId('profile-section');
22+
23+
expect(fn() => $divLocator->isEditable())
24+
->toThrow(RuntimeException::class, 'Element is not an <input>, <textarea>, <select> or [contenteditable]');
25+
});
26+
27+
it('returns false for disabled input elements', function (): void {
28+
$page = page()->goto('/test/element-tests');
29+
$disabledInputLocator = $page->getByTestId('disabled-input');
30+
31+
expect($disabledInputLocator->isEditable())->toBeFalse();
32+
});
33+
34+
it('returns false for readonly input elements', function (): void {
35+
$page = page()->goto('/test/element-tests');
36+
$readonlyInputLocator = $page->getByTestId('readonly-input');
37+
38+
expect($readonlyInputLocator->isEditable())->toBeFalse();
39+
});

tests/Browser/Playwright/Locator/OwnerFrameTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,11 @@
1414

1515
expect($frame)->toBeNull();
1616
});
17+
18+
it('throws RuntimeException when ownerFrame element is not found', function (): void {
19+
$page = page()->goto('/test/element-tests');
20+
$locator = $page->locator('.non-existent-element');
21+
22+
expect(fn() => $locator->ownerFrame())
23+
->toThrow(RuntimeException::class, 'Element not found');
24+
});

tests/Browser/Playwright/Locator/SelectOptionTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,11 @@
2828

2929
expect($selected)->toBeArray();
3030
});
31+
32+
it('throws RuntimeException when selectOption element is not found', function (): void {
33+
$page = page()->goto('/test/element-tests');
34+
$locator = $page->locator('.non-existent-element');
35+
36+
expect(fn() => $locator->selectOption('value'))
37+
->toThrow(RuntimeException::class, 'Element not found');
38+
});

tests/Browser/Playwright/Locator/TapTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,11 @@
2929

3030
expect($resultElement->textContent())->toBe('Button was clicked!');
3131
});
32+
33+
it('throws RuntimeException when tap element is not found', function (): void {
34+
$page = page(null, ['hasTouch' => true])->goto('/test/element-tests');
35+
$locator = $page->locator('.non-existent-element');
36+
37+
expect(fn() => $locator->tap())
38+
->toThrow(RuntimeException::class, 'Element not found');
39+
});

0 commit comments

Comments
 (0)