-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidate.go
More file actions
196 lines (169 loc) · 5.67 KB
/
validate.go
File metadata and controls
196 lines (169 loc) · 5.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package w3pilot
import (
"context"
"encoding/json"
"fmt"
)
// SelectorValidation represents the validation result for a single selector.
type SelectorValidation struct {
Selector string `json:"selector"`
Found bool `json:"found"`
Count int `json:"count"`
Visible bool `json:"visible"`
Enabled bool `json:"enabled,omitempty"`
TagName string `json:"tag_name,omitempty"`
Suggestions []string `json:"suggestions,omitempty"`
}
// ValidateSelectors checks multiple selectors and returns validation results.
// This helps AI agents verify selectors before attempting interactions.
func (p *Pilot) ValidateSelectors(ctx context.Context, selectors []string) ([]SelectorValidation, error) {
if p.closed {
return nil, ErrConnectionClosed
}
if len(selectors) == 0 {
return []SelectorValidation{}, nil
}
browsingCtx, err := p.getContext(ctx)
if err != nil {
return nil, err
}
// Build selector list as JSON for the script
selectorsJSON, err := json.Marshal(selectors)
if err != nil {
return nil, fmt.Errorf("failed to encode selectors: %w", err)
}
script := fmt.Sprintf(`
(function() {
const selectors = %s;
const results = [];
function isVisible(el) {
if (!el) return false;
const rect = el.getBoundingClientRect();
const style = window.getComputedStyle(el);
return rect.width > 0 && rect.height > 0 &&
style.visibility !== 'hidden' &&
style.display !== 'none' &&
style.opacity !== '0';
}
function isEnabled(el) {
if (!el) return false;
return !el.disabled && !el.getAttribute('aria-disabled');
}
function findSuggestions(selector) {
const suggestions = [];
// Extract the base name from the selector
let baseName = selector;
if (baseName.startsWith('#') || baseName.startsWith('.')) {
baseName = baseName.substring(1);
}
// Remove attribute selectors
baseName = baseName.replace(/\[.*\]/g, '');
// Remove pseudo-selectors
baseName = baseName.replace(/:.*$/g, '');
if (!baseName) return suggestions;
// Try ID variations
['#' + baseName, '#' + baseName + '-btn', '#' + baseName + 'Btn', '#' + baseName + '-button'].forEach(sel => {
try { if (document.querySelector(sel)) suggestions.push(sel); } catch {}
});
// Try class variations
['.' + baseName, '.' + baseName + '-btn', '.' + baseName + '-button', '.' + baseName.toLowerCase()].forEach(sel => {
try { if (document.querySelector(sel)) suggestions.push(sel); } catch {}
});
// Try data-testid
try {
const testId = document.querySelector('[data-testid="' + baseName + '"]');
if (testId) suggestions.push('[data-testid="' + baseName + '"]');
} catch {}
// Find elements with similar text content
const lowerBase = baseName.toLowerCase();
document.querySelectorAll('button, a, input[type="submit"], [role="button"]').forEach(el => {
const text = (el.textContent || el.value || '').toLowerCase();
if (text.includes(lowerBase) && suggestions.length < 5) {
if (el.id) {
const idSel = '#' + el.id;
if (!suggestions.includes(idSel)) suggestions.push(idSel);
} else if (el.className && typeof el.className === 'string') {
const classSel = '.' + el.className.split(' ')[0];
if (!suggestions.includes(classSel)) suggestions.push(classSel);
}
}
});
// Remove the original selector from suggestions
return [...new Set(suggestions)].filter(s => s !== selector).slice(0, 5);
}
for (const selector of selectors) {
let found = false;
let count = 0;
let visible = false;
let enabled = false;
let tagName = '';
let suggestions = [];
try {
const elements = document.querySelectorAll(selector);
count = elements.length;
found = count > 0;
if (found && elements[0]) {
const el = elements[0];
visible = isVisible(el);
enabled = isEnabled(el);
tagName = el.tagName.toLowerCase();
} else {
suggestions = findSuggestions(selector);
}
} catch (e) {
// Invalid selector syntax
found = false;
suggestions = ['Invalid selector syntax: ' + e.message];
}
results.push({
selector: selector,
found: found,
count: count,
visible: visible,
enabled: enabled,
tag_name: tagName,
suggestions: suggestions
});
}
return JSON.stringify(results);
})()
`, string(selectorsJSON))
// Execute via Evaluate
rawResult, err := p.client.Send(ctx, "script.callFunction", map[string]interface{}{
"functionDeclaration": "() => { " + script + " }",
"target": map[string]interface{}{"context": browsingCtx},
"arguments": []interface{}{},
"awaitPromise": true,
"resultOwnership": "root",
})
if err != nil {
return nil, fmt.Errorf("validation script failed: %w", err)
}
// Parse the BiDi response
var resp struct {
Result struct {
Type string `json:"type"`
Value string `json:"value"`
} `json:"result"`
}
if err := json.Unmarshal(rawResult, &resp); err != nil {
return nil, fmt.Errorf("failed to parse validation response: %w", err)
}
// Parse the validation results
var results []SelectorValidation
if err := json.Unmarshal([]byte(resp.Result.Value), &results); err != nil {
return nil, fmt.Errorf("failed to parse validation data: %w", err)
}
return results, nil
}
// ValidateSelector validates a single selector.
func (p *Pilot) ValidateSelector(ctx context.Context, selector string) (*SelectorValidation, error) {
results, err := p.ValidateSelectors(ctx, []string{selector})
if err != nil {
return nil, err
}
if len(results) == 0 {
return nil, fmt.Errorf("no validation result")
}
return &results[0], nil
}