|
| 1 | +import { beforeEach, describe, expect, it } from 'vitest'; |
| 2 | +import { getXpathsByPoint } from '../../src/extractor/locator'; |
| 3 | + |
| 4 | +// Mock DOM environment for testing |
| 5 | +class MockElement { |
| 6 | + nodeName: string; |
| 7 | + nodeType: number; |
| 8 | + namespaceURI?: string; |
| 9 | + parentNode: MockElement | null; |
| 10 | + previousElementSibling: MockElement | null; |
| 11 | + textContent: string; |
| 12 | + |
| 13 | + constructor( |
| 14 | + nodeName: string, |
| 15 | + textContent = '', |
| 16 | + namespaceURI?: string, |
| 17 | + parentNode: MockElement | null = null, |
| 18 | + ) { |
| 19 | + this.nodeName = nodeName; |
| 20 | + this.nodeType = 1; // ELEMENT_NODE |
| 21 | + this.namespaceURI = namespaceURI; |
| 22 | + this.textContent = textContent; |
| 23 | + this.parentNode = parentNode; |
| 24 | + this.previousElementSibling = null; |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +// Mock SVG element that extends SVGElement |
| 29 | +class MockSVGElement extends MockElement { |
| 30 | + constructor( |
| 31 | + nodeName: string, |
| 32 | + textContent = '', |
| 33 | + parentNode: MockElement | null = null, |
| 34 | + ) { |
| 35 | + super(nodeName, textContent, 'http://www.w3.org/2000/svg', parentNode); |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +// Mock global objects needed by locator functions |
| 40 | +const setupMockDOM = () => { |
| 41 | + global.Node = { |
| 42 | + ELEMENT_NODE: 1, |
| 43 | + TEXT_NODE: 3, |
| 44 | + } as any; |
| 45 | + |
| 46 | + // Mock SVGElement for SVG handling |
| 47 | + global.SVGElement = MockSVGElement as any; |
| 48 | + |
| 49 | + global.document = { |
| 50 | + documentElement: new MockElement('html'), |
| 51 | + body: new MockElement('body'), |
| 52 | + elementFromPoint: () => null, |
| 53 | + } as any; |
| 54 | + |
| 55 | + global.window = {} as any; |
| 56 | + global.HTMLElement = MockElement as any; |
| 57 | +}; |
| 58 | + |
| 59 | +describe('locator - multiple SVG icons', () => { |
| 60 | + beforeEach(() => { |
| 61 | + setupMockDOM(); |
| 62 | + }); |
| 63 | + |
| 64 | + it('should distinguish between multiple SVG icons in the same parent (user issue)', () => { |
| 65 | + // Simulate the user's scenario: td[34] with multiple svg children |
| 66 | + // The user has: td[34]/svg[1], td[34]/svg[2], td[34]/svg[3], td[34]/svg[4] |
| 67 | + // Clicking on svg[4] should return xpath ending with /svg[4], not just /td[34] |
| 68 | + |
| 69 | + global.document.elementFromPoint = (x: number, y: number) => { |
| 70 | + if (x === 100 && y === 100) { |
| 71 | + // Simulate clicking on the 4th SVG icon (the edit icon) |
| 72 | + const tr = new MockElement('tr'); |
| 73 | + const td = new MockElement('td'); |
| 74 | + |
| 75 | + // Create 4 SVG icons (like in the user's table) |
| 76 | + const svg1 = new MockSVGElement('svg', ''); |
| 77 | + const svg2 = new MockSVGElement('svg', ''); |
| 78 | + const svg3 = new MockSVGElement('svg', ''); |
| 79 | + const svg4 = new MockSVGElement('svg', ''); // The edit icon |
| 80 | + |
| 81 | + // Create internal path elements for each SVG |
| 82 | + const path4 = new MockSVGElement('path', ''); |
| 83 | + |
| 84 | + // Set up parent chain |
| 85 | + tr.parentNode = global.document.body as any; |
| 86 | + td.parentNode = tr; |
| 87 | + svg1.parentNode = td; |
| 88 | + svg2.parentNode = td; |
| 89 | + svg3.parentNode = td; |
| 90 | + svg4.parentNode = td; |
| 91 | + path4.parentNode = svg4; |
| 92 | + |
| 93 | + // Set up sibling chain |
| 94 | + svg1.previousElementSibling = null; |
| 95 | + svg2.previousElementSibling = svg1; |
| 96 | + svg3.previousElementSibling = svg2; |
| 97 | + svg4.previousElementSibling = svg3; |
| 98 | + |
| 99 | + // Return the path inside svg4 (simulating clicking on the edit icon) |
| 100 | + return path4 as any; |
| 101 | + } |
| 102 | + return null; |
| 103 | + }; |
| 104 | + |
| 105 | + const point = { left: 100, top: 100 }; |
| 106 | + const xpaths = getXpathsByPoint(point, true); |
| 107 | + |
| 108 | + expect(xpaths).toBeDefined(); |
| 109 | + expect(xpaths).toHaveLength(1); |
| 110 | + |
| 111 | + // Should include svg[4] to distinguish from other SVG icons |
| 112 | + expect(xpaths?.[0]).toMatch(/svg\[4\]/); |
| 113 | + |
| 114 | + // Should not include the internal path element |
| 115 | + expect(xpaths?.[0]).not.toMatch(/path/); |
| 116 | + |
| 117 | + // Should include the td and svg with proper indices |
| 118 | + expect(xpaths?.[0]).toMatch(/\/td\[1\]\/svg\[4\]$/); |
| 119 | + |
| 120 | + console.log('XPath for svg[4]:', xpaths?.[0]); |
| 121 | + }); |
| 122 | + |
| 123 | + it('should distinguish between different SVG icons in the same cell', () => { |
| 124 | + // Test that we can generate different xpaths for different SVG icons |
| 125 | + global.document.elementFromPoint = (x: number, y: number) => { |
| 126 | + const tr = new MockElement('tr'); |
| 127 | + const td = new MockElement('td'); |
| 128 | + |
| 129 | + const svg1 = new MockSVGElement('svg', ''); |
| 130 | + const svg2 = new MockSVGElement('svg', ''); |
| 131 | + const svg3 = new MockSVGElement('svg', ''); |
| 132 | + |
| 133 | + tr.parentNode = global.document.body as any; |
| 134 | + td.parentNode = tr; |
| 135 | + svg1.parentNode = td; |
| 136 | + svg2.parentNode = td; |
| 137 | + svg3.parentNode = td; |
| 138 | + |
| 139 | + svg1.previousElementSibling = null; |
| 140 | + svg2.previousElementSibling = svg1; |
| 141 | + svg3.previousElementSibling = svg2; |
| 142 | + |
| 143 | + // Return different SVG based on coordinates |
| 144 | + if (x === 100) return svg1 as any; |
| 145 | + if (x === 200) return svg2 as any; |
| 146 | + if (x === 300) return svg3 as any; |
| 147 | + return null; |
| 148 | + }; |
| 149 | + |
| 150 | + const xpath1 = getXpathsByPoint({ left: 100, top: 100 }, true); |
| 151 | + const xpath2 = getXpathsByPoint({ left: 200, top: 100 }, true); |
| 152 | + const xpath3 = getXpathsByPoint({ left: 300, top: 100 }, true); |
| 153 | + |
| 154 | + // All xpaths should be different |
| 155 | + expect(xpath1?.[0]).not.toBe(xpath2?.[0]); |
| 156 | + expect(xpath2?.[0]).not.toBe(xpath3?.[0]); |
| 157 | + expect(xpath1?.[0]).not.toBe(xpath3?.[0]); |
| 158 | + |
| 159 | + // Each should have the correct index |
| 160 | + expect(xpath1?.[0]).toMatch(/svg\[1\]$/); |
| 161 | + expect(xpath2?.[0]).toMatch(/svg\[2\]$/); |
| 162 | + expect(xpath3?.[0]).toMatch(/svg\[3\]$/); |
| 163 | + |
| 164 | + console.log('XPath for svg[1]:', xpath1?.[0]); |
| 165 | + console.log('XPath for svg[2]:', xpath2?.[0]); |
| 166 | + console.log('XPath for svg[3]:', xpath3?.[0]); |
| 167 | + }); |
| 168 | +}); |
0 commit comments