|
| 1 | +/** |
| 2 | + * Copyright (c) Microsoft Corporation. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +import { test, expect } from './fixtures'; |
| 18 | + |
| 19 | +test('should return aria snapshot diff', async ({ client, server }) => { |
| 20 | + server.setContent('/', ` |
| 21 | + <button>Button 1</button> |
| 22 | + <button>Button 2</button> |
| 23 | + <ul id=filler></ul> |
| 24 | + <script> |
| 25 | + const filler = document.getElementById('filler'); |
| 26 | + for (let i = 0; i < 100; i++) { |
| 27 | + const li = document.createElement('li'); |
| 28 | + li.textContent = 'Filler ' + i; |
| 29 | + filler.appendChild(li); |
| 30 | + } |
| 31 | + const [button1, button2] = document.querySelectorAll('button'); |
| 32 | + button1.addEventListener('click', () => { |
| 33 | + const span = document.createElement('span'); |
| 34 | + span.textContent = 'new text'; |
| 35 | + button2.appendChild(span); |
| 36 | + button1.focus(); // without manual focus, webkit focuses body |
| 37 | + }); |
| 38 | + button2.focus(); |
| 39 | + button2.addEventListener('click', () => { |
| 40 | + button2.focus(); // without manual focus, webkit focuses body |
| 41 | + }); |
| 42 | + </script> |
| 43 | + `, 'text/html'); |
| 44 | + |
| 45 | + const listitems = new Array(100).fill(0).map((_, i) => `\n - listitem [ref=e${5 + i}]: Filler ${i}`).join(''); |
| 46 | + |
| 47 | + expect(await client.callTool({ |
| 48 | + name: 'browser_navigate', |
| 49 | + arguments: { |
| 50 | + url: server.PREFIX, |
| 51 | + }, |
| 52 | + })).toHaveResponse({ |
| 53 | + pageState: expect.stringContaining(` |
| 54 | + - button "Button 1" [ref=e2] |
| 55 | + - button "Button 2" [active] [ref=e3] |
| 56 | + - list [ref=e4]:${listitems}`), |
| 57 | + }); |
| 58 | + |
| 59 | + expect(await client.callTool({ |
| 60 | + name: 'browser_click', |
| 61 | + arguments: { |
| 62 | + element: 'Button 2', |
| 63 | + ref: 'e3', |
| 64 | + }, |
| 65 | + })).toHaveResponse({ |
| 66 | + pageState: expect.stringContaining(`Page Snapshot: |
| 67 | +\`\`\`yaml |
| 68 | +- ref=e1 [unchanged] |
| 69 | +\`\`\``), |
| 70 | + }); |
| 71 | + |
| 72 | + expect(await client.callTool({ |
| 73 | + name: 'browser_click', |
| 74 | + arguments: { |
| 75 | + element: 'Button 1', |
| 76 | + ref: 'e2', |
| 77 | + }, |
| 78 | + })).toHaveResponse({ |
| 79 | + pageState: expect.stringContaining(`Page Snapshot: |
| 80 | +\`\`\`yaml |
| 81 | +- generic [ref=e1]: |
| 82 | + - button "Button 1" [active] [ref=e2] |
| 83 | + - button "Button 2new text" [ref=e105] |
| 84 | + - ref=e4 [unchanged] |
| 85 | +\`\`\``), |
| 86 | + }); |
| 87 | + |
| 88 | + // browser_snapshot forces a full snapshot. |
| 89 | + expect(await client.callTool({ |
| 90 | + name: 'browser_snapshot', |
| 91 | + })).toHaveResponse({ |
| 92 | + pageState: expect.stringContaining(`Page Snapshot: |
| 93 | +\`\`\`yaml |
| 94 | +- generic [ref=e1]: |
| 95 | + - button "Button 1" [active] [ref=e2] |
| 96 | + - button "Button 2new text" [ref=e105] |
| 97 | + - list [ref=e4]:${listitems} |
| 98 | +\`\`\``), |
| 99 | + }); |
| 100 | +}); |
| 101 | + |
| 102 | +test('should reset aria snapshot diff upon navigation', async ({ client, server }) => { |
| 103 | + server.setContent('/before', ` |
| 104 | + <button>Button 1</button> |
| 105 | + <button>Button 2</button> |
| 106 | + <ul id=filler></ul> |
| 107 | + <script> |
| 108 | + const filler = document.getElementById('filler'); |
| 109 | + for (let i = 0; i < 100; i++) { |
| 110 | + const li = document.createElement('li'); |
| 111 | + li.textContent = 'Filler ' + i; |
| 112 | + filler.appendChild(li); |
| 113 | + } |
| 114 | + document.querySelector('button').addEventListener('click', () => { |
| 115 | + window.location.href = '/after'; |
| 116 | + }); |
| 117 | + document.querySelectorAll('button')[1].focus(); |
| 118 | + </script> |
| 119 | + `, 'text/html'); |
| 120 | + |
| 121 | + server.setContent('/after', ` |
| 122 | + <button>Button 1</button> |
| 123 | + <button>Button 2</button> |
| 124 | + <ul id=filler></ul> |
| 125 | + <script> |
| 126 | + const filler = document.getElementById('filler'); |
| 127 | + for (let i = 0; i < 100; i++) { |
| 128 | + const li = document.createElement('li'); |
| 129 | + li.textContent = 'Filler ' + i; |
| 130 | + filler.appendChild(li); |
| 131 | + } |
| 132 | + document.querySelectorAll('button')[1].focus(); |
| 133 | + </script> |
| 134 | + `, 'text/html'); |
| 135 | + |
| 136 | + expect(await client.callTool({ |
| 137 | + name: 'browser_navigate', |
| 138 | + arguments: { |
| 139 | + url: server.PREFIX + '/before', |
| 140 | + }, |
| 141 | + })).toHaveResponse({ |
| 142 | + pageState: expect.stringContaining(` |
| 143 | + - button "Button 1" [ref=e2] |
| 144 | + - button "Button 2" [active] [ref=e3]`), |
| 145 | + }); |
| 146 | + |
| 147 | + expect(await client.callTool({ |
| 148 | + name: 'browser_click', |
| 149 | + arguments: { |
| 150 | + element: 'Button 1', |
| 151 | + ref: 'e2', |
| 152 | + }, |
| 153 | + })).toHaveResponse({ |
| 154 | + pageState: expect.stringContaining(` |
| 155 | + - button "Button 1" [ref=e2] |
| 156 | + - button "Button 2" [active] [ref=e3]`), |
| 157 | + }); |
| 158 | +}); |
0 commit comments