|
3 | 3 | declare(strict_types=1); |
4 | 4 |
|
5 | 5 | use Pest\Browser\Playwright\JSHandle; |
6 | | -use Pest\Browser\Support\JavaScriptSerializer; |
7 | 6 |
|
8 | 7 | it('can evaluate basic expressions on a JSHandle', function (): void { |
9 | 8 | $page = page('/test/frame-tests'); |
10 | 9 |
|
11 | | - // Create a JSHandle for a DOM element |
12 | 10 | $handle = $page->evaluateHandle('document.querySelector("#test-content")'); |
13 | 11 | expect($handle)->toBeInstanceOf(JSHandle::class); |
14 | 12 |
|
15 | | - // Evaluate a simple property access on the handle |
16 | 13 | $textContent = $handle->evaluate('node => node.textContent'); |
17 | 14 | expect($textContent)->toContain('This is the main content for testing'); |
18 | 15 | }); |
19 | 16 |
|
20 | 17 | it('can evaluate expressions that modify a JSHandle element', function (): void { |
21 | 18 | $page = page('/test/frame-tests'); |
22 | 19 |
|
23 | | - // Create a JSHandle for an input element |
24 | 20 | $handle = $page->evaluateHandle('document.querySelector("#test-input")'); |
25 | 21 | expect($handle)->toBeInstanceOf(JSHandle::class); |
26 | 22 |
|
27 | | - // Set the value of the input |
28 | 23 | $handle->evaluate('input => { input.value = "test value"; return true; }'); |
29 | 24 |
|
30 | | - // Verify the value was changed |
31 | 25 | $value = $page->inputValue('#test-input'); |
32 | 26 | expect($value)->toBe('test value'); |
33 | 27 | }); |
34 | 28 |
|
35 | 29 | it('can pass arguments when evaluating expressions on a JSHandle', function (): void { |
36 | 30 | $page = page('/test/frame-tests'); |
37 | 31 |
|
38 | | - // Create a JSHandle |
39 | 32 | $handle = $page->evaluateHandle('document.querySelector("#test-content")'); |
40 | 33 | expect($handle)->toBeInstanceOf(JSHandle::class); |
41 | 34 |
|
42 | | - // Pass a simple string argument |
43 | 35 | $result = $handle->evaluate('(node, text) => { node.textContent = text; return node.textContent; }', 'Updated content'); |
44 | 36 | expect($result)->toBe('Updated content'); |
45 | 37 |
|
46 | | - // Verify the content was actually changed in the page |
47 | 38 | $content = $page->textContent('#test-content'); |
48 | 39 | expect($content)->toBe('Updated content'); |
49 | 40 | }); |
|
54 | 45 | $handle = $page->evaluateHandle('document.querySelector("h1")'); |
55 | 46 | expect($handle)->toBeInstanceOf(JSHandle::class); |
56 | 47 |
|
57 | | - // Return a string |
58 | 48 | $text = $handle->evaluate('h1 => h1.textContent'); |
59 | 49 | expect($text)->toBeString(); |
60 | 50 | expect($text)->toContain('PESTPHP'); |
61 | 51 |
|
62 | | - // Return a boolean |
63 | 52 | $hasChildren = $handle->evaluate('h1 => h1.hasChildNodes()'); |
64 | 53 | expect($hasChildren)->toBeTrue(); |
65 | 54 |
|
66 | | - // Return a number |
67 | 55 | $childCount = $handle->evaluate('h1 => h1.childNodes.length'); |
68 | 56 | expect($childCount)->toBeGreaterThan(0); |
69 | 57 | }); |
| 58 | + |
| 59 | +it('can get JSON value from JSHandle with objects', function (): void { |
| 60 | + $page = page('/test/frame-tests'); |
| 61 | + |
| 62 | + $handle = $page->evaluateHandle('({name: "test", value: 42, active: true})'); |
| 63 | + expect($handle)->toBeInstanceOf(JSHandle::class); |
| 64 | + |
| 65 | + $jsonValue = $handle->jsonValue(); |
| 66 | + expect($jsonValue)->toBeArray(); |
| 67 | + expect($jsonValue)->toHaveKey('name'); |
| 68 | + expect($jsonValue)->toHaveKey('value'); |
| 69 | + expect($jsonValue['name'])->toBe('test'); |
| 70 | + expect($jsonValue['value'])->toBe(42); |
| 71 | + expect($jsonValue['active'])->toBeTrue(); |
| 72 | +}); |
| 73 | + |
| 74 | +it('can get JSON value from JSHandle with different data types', function (): void { |
| 75 | + $page = page('/test/frame-tests'); |
| 76 | + |
| 77 | + $arrayHandle = $page->evaluateHandle('[1, 2, "test"]'); |
| 78 | + expect($arrayHandle->jsonValue())->toBe([1, 2, 'test']); |
| 79 | + |
| 80 | + $numberHandle = $page->evaluateHandle('42'); |
| 81 | + expect($numberHandle->jsonValue())->toBe(42); |
| 82 | +}); |
| 83 | + |
| 84 | +it('can get JSON value from JSHandle for DOM elements', function (): void { |
| 85 | + $page = page('/test/frame-tests'); |
| 86 | + |
| 87 | + $handle = $page->evaluateHandle('document.querySelector("#test-content")'); |
| 88 | + expect($handle)->toBeInstanceOf(JSHandle::class); |
| 89 | + |
| 90 | + $jsonValue = $handle->jsonValue(); |
| 91 | + expect($jsonValue)->toBeString(); |
| 92 | + expect($jsonValue)->toContain('ref:'); |
| 93 | +}); |
| 94 | + |
| 95 | +it('can convert JSHandle to string representation', function (): void { |
| 96 | + $page = page('/test/frame-tests'); |
| 97 | + |
| 98 | + $stringHandle = $page->evaluateHandle('"hello world"'); |
| 99 | + expect($stringHandle->toString())->toBe('hello world'); |
| 100 | + |
| 101 | + $numberHandle = $page->evaluateHandle('42'); |
| 102 | + expect($numberHandle->toString())->toBe('42'); |
| 103 | +}); |
| 104 | + |
| 105 | +it('can dispose JSHandle', function (): void { |
| 106 | + $page = page('/test/frame-tests'); |
| 107 | + |
| 108 | + $handle = $page->evaluateHandle('document.querySelector("#test-content")'); |
| 109 | + expect($handle)->toBeInstanceOf(JSHandle::class); |
| 110 | + |
| 111 | + $handle->dispose(); |
| 112 | + expect(true)->toBeTrue(); |
| 113 | +}); |
| 114 | + |
| 115 | +it('can evaluate complex expressions with various argument types', function (): void { |
| 116 | + $page = page('/test/frame-tests'); |
| 117 | + |
| 118 | + $handle = $page->evaluateHandle('document'); |
| 119 | + expect($handle)->toBeInstanceOf(JSHandle::class); |
| 120 | + |
| 121 | + expect($handle->evaluate('(doc, arg) => arg === null', null))->toBeTrue(); |
| 122 | + expect($handle->evaluate('(doc, num) => num * 2', 21))->toBe(42); |
| 123 | + expect($handle->evaluate('(doc, arr) => arr.length', [1, 2, 3]))->toBe(3); |
| 124 | + expect($handle->evaluate('(doc, obj) => obj.name + ":" + obj.value', ['name' => 'test', 'value' => 123]))->toBe('test:123'); |
| 125 | +}); |
0 commit comments