selenium-selector-autocorrect 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- selenium_selector_autocorrect-0.1.0/LICENSE +21 -0
- selenium_selector_autocorrect-0.1.0/PKG-INFO +277 -0
- selenium_selector_autocorrect-0.1.0/README.md +237 -0
- selenium_selector_autocorrect-0.1.0/pyproject.toml +138 -0
- selenium_selector_autocorrect-0.1.0/setup.cfg +4 -0
- selenium_selector_autocorrect-0.1.0/src/selenium_selector_autocorrect/__init__.py +54 -0
- selenium_selector_autocorrect-0.1.0/src/selenium_selector_autocorrect/ai_providers.py +123 -0
- selenium_selector_autocorrect-0.1.0/src/selenium_selector_autocorrect/auto_correct.py +254 -0
- selenium_selector_autocorrect-0.1.0/src/selenium_selector_autocorrect/correction_tracker.py +226 -0
- selenium_selector_autocorrect-0.1.0/src/selenium_selector_autocorrect/py.typed +1 -0
- selenium_selector_autocorrect-0.1.0/src/selenium_selector_autocorrect/wait_hook.py +140 -0
- selenium_selector_autocorrect-0.1.0/src/selenium_selector_autocorrect.egg-info/PKG-INFO +277 -0
- selenium_selector_autocorrect-0.1.0/src/selenium_selector_autocorrect.egg-info/SOURCES.txt +14 -0
- selenium_selector_autocorrect-0.1.0/src/selenium_selector_autocorrect.egg-info/dependency_links.txt +1 -0
- selenium_selector_autocorrect-0.1.0/src/selenium_selector_autocorrect.egg-info/requires.txt +10 -0
- selenium_selector_autocorrect-0.1.0/src/selenium_selector_autocorrect.egg-info/top_level.txt +1 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024-2026 Contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,277 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: selenium-selector-autocorrect
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Automatic Selenium selector correction using AI when elements fail to be found
|
|
5
|
+
Author-email: Marty Zhou <marty.zhou@example.com>
|
|
6
|
+
Maintainer-email: Marty Zhou <marty.zhou@example.com>
|
|
7
|
+
License: MIT
|
|
8
|
+
Project-URL: Homepage, https://github.com/MartyZhou/selenium-selector-autocorrect
|
|
9
|
+
Project-URL: Documentation, https://github.com/MartyZhou/selenium-selector-autocorrect#readme
|
|
10
|
+
Project-URL: Repository, https://github.com/MartyZhou/selenium-selector-autocorrect
|
|
11
|
+
Project-URL: Issues, https://github.com/MartyZhou/selenium-selector-autocorrect/issues
|
|
12
|
+
Project-URL: Changelog, https://github.com/MartyZhou/selenium-selector-autocorrect/blob/main/CHANGELOG.md
|
|
13
|
+
Keywords: selenium,testing,automation,ai,web-testing,test-automation,selector,auto-correction
|
|
14
|
+
Classifier: Development Status :: 4 - Beta
|
|
15
|
+
Classifier: Intended Audience :: Developers
|
|
16
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
17
|
+
Classifier: Operating System :: OS Independent
|
|
18
|
+
Classifier: Programming Language :: Python :: 3
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.8
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
21
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
22
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
23
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
24
|
+
Classifier: Topic :: Software Development :: Testing
|
|
25
|
+
Classifier: Topic :: Software Development :: Quality Assurance
|
|
26
|
+
Classifier: Framework :: Pytest
|
|
27
|
+
Requires-Python: >=3.8
|
|
28
|
+
Description-Content-Type: text/markdown
|
|
29
|
+
License-File: LICENSE
|
|
30
|
+
Requires-Dist: selenium>=4.0.0
|
|
31
|
+
Requires-Dist: requests>=2.25.0
|
|
32
|
+
Provides-Extra: dev
|
|
33
|
+
Requires-Dist: pytest>=7.0.0; extra == "dev"
|
|
34
|
+
Requires-Dist: pytest-cov>=4.0.0; extra == "dev"
|
|
35
|
+
Requires-Dist: black>=23.0.0; extra == "dev"
|
|
36
|
+
Requires-Dist: isort>=5.12.0; extra == "dev"
|
|
37
|
+
Requires-Dist: mypy>=1.0.0; extra == "dev"
|
|
38
|
+
Requires-Dist: ruff>=0.1.0; extra == "dev"
|
|
39
|
+
Dynamic: license-file
|
|
40
|
+
|
|
41
|
+
# Selenium Selector AutoCorrect
|
|
42
|
+
|
|
43
|
+
A Python package that automatically corrects Selenium element selectors using AI when they fail, reducing test maintenance and improving test reliability.
|
|
44
|
+
|
|
45
|
+
## Features
|
|
46
|
+
|
|
47
|
+
- **Automatic Selector Correction**: When a WebDriverWait times out, the package uses AI to analyze the page and suggest working alternatives
|
|
48
|
+
- **Local AI Integration**: Uses a local AI service with OpenAI-compatible API
|
|
49
|
+
- **Correction Tracking**: Records all corrections with source file and line information
|
|
50
|
+
- **Optional Auto-Update**: Can automatically update test files with corrected selectors
|
|
51
|
+
- **Zero Code Changes**: Works by hooking into Selenium's WebDriverWait
|
|
52
|
+
|
|
53
|
+
## Installation
|
|
54
|
+
|
|
55
|
+
```bash
|
|
56
|
+
pip install selenium-selector-autocorrect
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
## Quick Start
|
|
60
|
+
|
|
61
|
+
```python
|
|
62
|
+
from selenium import webdriver
|
|
63
|
+
from selenium.webdriver.common.by import By
|
|
64
|
+
from selenium.webdriver.support.wait import WebDriverWait
|
|
65
|
+
from selenium.webdriver.support import expected_conditions as EC
|
|
66
|
+
from selenium_selector_autocorrect import install_auto_correct_hook
|
|
67
|
+
|
|
68
|
+
install_auto_correct_hook()
|
|
69
|
+
|
|
70
|
+
driver = webdriver.Chrome()
|
|
71
|
+
driver.get("https://example.com")
|
|
72
|
+
|
|
73
|
+
element = WebDriverWait(driver, 10).until(
|
|
74
|
+
EC.presence_of_element_located((By.ID, "some-element"))
|
|
75
|
+
)
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
## AI Service Setup
|
|
79
|
+
|
|
80
|
+
This package requires a local AI service with an OpenAI-compatible API. We recommend using **[VS Code Copilot as Service](https://marketplace.visualstudio.com/items?itemName=MartyZhou.vscode-copilot-as-service)**, which exposes GitHub Copilot through a local HTTP server.
|
|
81
|
+
|
|
82
|
+
### Installing VS Code Copilot as Service
|
|
83
|
+
|
|
84
|
+
1. Install from VS Code Marketplace or run:
|
|
85
|
+
```bash
|
|
86
|
+
code --install-extension MartyZhou.vscode-copilot-as-service
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
2. The extension automatically starts a server on `http://localhost:8765`
|
|
90
|
+
|
|
91
|
+
3. Requires an active GitHub Copilot subscription
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
## Configuration
|
|
95
|
+
|
|
96
|
+
Configure via environment variables:
|
|
97
|
+
|
|
98
|
+
- `LOCAL_AI_API_URL`: URL of local AI service (default: `http://localhost:8765`)
|
|
99
|
+
- `SELENIUM_AUTO_CORRECT`: Enable/disable auto-correction (default: `"1"`)
|
|
100
|
+
- `SELENIUM_SUGGEST_BETTER`: Suggest better selectors for found elements (default: `"0"`)
|
|
101
|
+
- `SELENIUM_AUTO_UPDATE_TESTS`: Auto-update test files with corrections (default: `"0"`)
|
|
102
|
+
|
|
103
|
+
### Example
|
|
104
|
+
|
|
105
|
+
```python
|
|
106
|
+
import os
|
|
107
|
+
|
|
108
|
+
os.environ['LOCAL_AI_API_URL'] = 'http://localhost:8765'
|
|
109
|
+
os.environ['SELENIUM_AUTO_CORRECT'] = '1'
|
|
110
|
+
os.environ['SELENIUM_AUTO_UPDATE_TESTS'] = '1' # Enable auto-update
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
## Usage
|
|
114
|
+
|
|
115
|
+
### Basic Usage
|
|
116
|
+
|
|
117
|
+
```python
|
|
118
|
+
from selenium_selector_autocorrect import install_auto_correct_hook
|
|
119
|
+
|
|
120
|
+
install_auto_correct_hook()
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
### Advanced Usage
|
|
124
|
+
|
|
125
|
+
```python
|
|
126
|
+
from selenium_selector_autocorrect import (
|
|
127
|
+
install_auto_correct_hook,
|
|
128
|
+
get_auto_correct,
|
|
129
|
+
get_correction_tracker,
|
|
130
|
+
export_corrections_report
|
|
131
|
+
)
|
|
132
|
+
|
|
133
|
+
install_auto_correct_hook()
|
|
134
|
+
|
|
135
|
+
auto_correct = get_auto_correct()
|
|
136
|
+
auto_correct.enabled = True
|
|
137
|
+
auto_correct.suggest_better_selectors = False
|
|
138
|
+
|
|
139
|
+
# Export corrections report at end of test run
|
|
140
|
+
tracker = get_correction_tracker()
|
|
141
|
+
export_corrections_report("corrections_report.json")
|
|
142
|
+
tracker = get_correction_tracker()
|
|
143
|
+
export_corrections_report("corrections_report.json")
|
|
144
|
+
|
|
145
|
+
print(f"Total corrections: {len(tracker.get_corrections())}")
|
|
146
|
+
print(f"Successful corrections: {len(tracker.get_successful_corrections())}")
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
### Custom AI Provider
|
|
150
|
+
|
|
151
|
+
```python
|
|
152
|
+
from selenium_selector_autocorrect import AIProvider, configure_provider
|
|
153
|
+
|
|
154
|
+
class CustomAIProvider(AIProvider):
|
|
155
|
+
def is_available(self) -> bool:
|
|
156
|
+
return True
|
|
157
|
+
|
|
158
|
+
def suggest_selector(self, system_prompt: str, user_prompt: str):))
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
## How It Works
|
|
162
|
+
|
|
163
|
+
1. **Hook Installation**: Patches `WebDriverWait.until()` to add auto-correction
|
|
164
|
+
2. **Timeout Detection**: When a selector times out, the original exception is caught
|
|
165
|
+
3. **Page Analysis**: JavaScript extracts visible elements and their attributes
|
|
166
|
+
4. **AI Suggestion**: Sends page context to AI provider for selector suggestion
|
|
167
|
+
5. **Verification**: Tests the suggested selector
|
|
168
|
+
6. **Success Handling**: If successful, records the correction and optionally updates the test file
|
|
169
|
+
7. **Fallback**: If correction fails, raises the original TimeoutException
|
|
170
|
+
|
|
171
|
+
## AI Provider Setup
|
|
172
|
+
|
|
173
|
+
### Local AI Service
|
|
174
|
+
|
|
175
|
+
The package requires a local AI service with OpenAI-compatible API:
|
|
176
|
+
|
|
177
|
+
```bash
|
|
178
|
+
POST http://localhost:8765/v1/chat/completions
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
For file auto-updates:
|
|
182
|
+
```bash
|
|
183
|
+
POST http://localhost:8765/v1/workspace/files/read
|
|
184
|
+
POST http://localhost:8765/v1/workspace/files/edit
|
|
185
|
+
## Correction Reports
|
|
186
|
+
|
|
187
|
+
Export correction reports in JSON format:
|
|
188
|
+
|
|
189
|
+
```python
|
|
190
|
+
from selenium_selector_autocorrect import export_corrections_report
|
|
191
|
+
|
|
192
|
+
export_corrections_report("corrections_report.json")
|
|
193
|
+
```
|
|
194
|
+
|
|
195
|
+
Report format:
|
|
196
|
+
```json
|
|
197
|
+
{
|
|
198
|
+
"corrections": [
|
|
199
|
+
{
|
|
200
|
+
"original_by": "id",
|
|
201
|
+
"original_value": "old-selector",
|
|
202
|
+
"corrected_by": "css selector",
|
|
203
|
+
"corrected_value": ".new-selector",
|
|
204
|
+
"success": true,
|
|
205
|
+
"test_file": "/path/to/test.py",
|
|
206
|
+
"test_line": 42,
|
|
207
|
+
"timestamp": "2024-01-31T10:30:00"
|
|
208
|
+
}
|
|
209
|
+
],
|
|
210
|
+
"summary": {
|
|
211
|
+
"total": 10,
|
|
212
|
+
"successful": 8,
|
|
213
|
+
"generated_at": "2024-01-31T10:35:00"
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
```
|
|
217
|
+
|
|
218
|
+
## Best Practices
|
|
219
|
+
|
|
220
|
+
1. **Install Once**: Call `install_auto_correct_hook()` once at test suite startup (e.g., in `conftest.py`)
|
|
221
|
+
2. **Review Corrections**: Regularly review correction reports to identify brittle selectors
|
|
222
|
+
3. **Update Tests**: Use auto-update sparingly and review changes before committing
|
|
223
|
+
4. **Monitor AI Service**: Ensure your AI service is running and responsive
|
|
224
|
+
5. **Use Strong Selectors**: The tool helps with failures but writing robust selectors is still preferred
|
|
225
|
+
|
|
226
|
+
## Requirements
|
|
227
|
+
|
|
228
|
+
- Python >= 3.8
|
|
229
|
+
- selenium >= 4.0.0
|
|
230
|
+
- requests >= 2.25.0
|
|
231
|
+
|
|
232
|
+
## License
|
|
233
|
+
|
|
234
|
+
MITInstall hook once at test suite startup (e.g., in conftest.py)
|
|
235
|
+
2. Review correction reports regularly to identify brittle selectors
|
|
236
|
+
3. Use auto-update sparingly and review changes before committing
|
|
237
|
+
4. Ensure your AI service is running and responsive
|
|
238
|
+
5. Write robust selectors - the tool helps with failures but prevention is better
|
|
239
|
+
|
|
240
|
+
When contributing:
|
|
241
|
+
1. Follow PEP 8 style guidelines
|
|
242
|
+
2. Add tests for new features
|
|
243
|
+
3. Update documentation
|
|
244
|
+
4. No emojis in code or documentation
|
|
245
|
+
|
|
246
|
+
## Troubleshooting
|
|
247
|
+
|
|
248
|
+
### AI Service Not Available
|
|
249
|
+
|
|
250
|
+
Contributions are welcome! Please:
|
|
251
|
+
1. Follow PEP 8 style guidelines
|
|
252
|
+
2. Add tests for new features
|
|
253
|
+
3. Update documentation
|
|
254
|
+
4. Maintain consistency with existing code
|
|
255
|
+
|
|
256
|
+
**Possible causes**:
|
|
257
|
+
- `SELENIUM_AUTO_UPDATE_TESTS` not set to `"1"`
|
|
258
|
+
- Test file path not detected correctly
|
|
259
|
+
- Selector string not found in source file (check quotes)
|
|
260
|
+
|
|
261
|
+
### No Corrections Happening
|
|
262
|
+
Solution: Ensure your local AI service is running on the configured port.
|
|
263
|
+
|
|
264
|
+
### Test File Not Updated
|
|
265
|
+
|
|
266
|
+
Possible causes:
|
|
267
|
+
- `SELENIUM_AUTO_UPDATE_TESTS` not set to "1"
|
|
268
|
+
- Test file path not detected correctly
|
|
269
|
+
- Selector string not found in source file
|
|
270
|
+
|
|
271
|
+
### No Corrections Happening
|
|
272
|
+
|
|
273
|
+
Check:
|
|
274
|
+
1. Hook is installed - look for log message
|
|
275
|
+
2. AI service is available - check `get_auto_correct().is_service_available()`
|
|
276
|
+
3. Auto-correct is enabled - c
|
|
277
|
+
See CHANGELOG.md for version history and changes.
|
|
@@ -0,0 +1,237 @@
|
|
|
1
|
+
# Selenium Selector AutoCorrect
|
|
2
|
+
|
|
3
|
+
A Python package that automatically corrects Selenium element selectors using AI when they fail, reducing test maintenance and improving test reliability.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- **Automatic Selector Correction**: When a WebDriverWait times out, the package uses AI to analyze the page and suggest working alternatives
|
|
8
|
+
- **Local AI Integration**: Uses a local AI service with OpenAI-compatible API
|
|
9
|
+
- **Correction Tracking**: Records all corrections with source file and line information
|
|
10
|
+
- **Optional Auto-Update**: Can automatically update test files with corrected selectors
|
|
11
|
+
- **Zero Code Changes**: Works by hooking into Selenium's WebDriverWait
|
|
12
|
+
|
|
13
|
+
## Installation
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
pip install selenium-selector-autocorrect
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
## Quick Start
|
|
20
|
+
|
|
21
|
+
```python
|
|
22
|
+
from selenium import webdriver
|
|
23
|
+
from selenium.webdriver.common.by import By
|
|
24
|
+
from selenium.webdriver.support.wait import WebDriverWait
|
|
25
|
+
from selenium.webdriver.support import expected_conditions as EC
|
|
26
|
+
from selenium_selector_autocorrect import install_auto_correct_hook
|
|
27
|
+
|
|
28
|
+
install_auto_correct_hook()
|
|
29
|
+
|
|
30
|
+
driver = webdriver.Chrome()
|
|
31
|
+
driver.get("https://example.com")
|
|
32
|
+
|
|
33
|
+
element = WebDriverWait(driver, 10).until(
|
|
34
|
+
EC.presence_of_element_located((By.ID, "some-element"))
|
|
35
|
+
)
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
## AI Service Setup
|
|
39
|
+
|
|
40
|
+
This package requires a local AI service with an OpenAI-compatible API. We recommend using **[VS Code Copilot as Service](https://marketplace.visualstudio.com/items?itemName=MartyZhou.vscode-copilot-as-service)**, which exposes GitHub Copilot through a local HTTP server.
|
|
41
|
+
|
|
42
|
+
### Installing VS Code Copilot as Service
|
|
43
|
+
|
|
44
|
+
1. Install from VS Code Marketplace or run:
|
|
45
|
+
```bash
|
|
46
|
+
code --install-extension MartyZhou.vscode-copilot-as-service
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
2. The extension automatically starts a server on `http://localhost:8765`
|
|
50
|
+
|
|
51
|
+
3. Requires an active GitHub Copilot subscription
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
## Configuration
|
|
55
|
+
|
|
56
|
+
Configure via environment variables:
|
|
57
|
+
|
|
58
|
+
- `LOCAL_AI_API_URL`: URL of local AI service (default: `http://localhost:8765`)
|
|
59
|
+
- `SELENIUM_AUTO_CORRECT`: Enable/disable auto-correction (default: `"1"`)
|
|
60
|
+
- `SELENIUM_SUGGEST_BETTER`: Suggest better selectors for found elements (default: `"0"`)
|
|
61
|
+
- `SELENIUM_AUTO_UPDATE_TESTS`: Auto-update test files with corrections (default: `"0"`)
|
|
62
|
+
|
|
63
|
+
### Example
|
|
64
|
+
|
|
65
|
+
```python
|
|
66
|
+
import os
|
|
67
|
+
|
|
68
|
+
os.environ['LOCAL_AI_API_URL'] = 'http://localhost:8765'
|
|
69
|
+
os.environ['SELENIUM_AUTO_CORRECT'] = '1'
|
|
70
|
+
os.environ['SELENIUM_AUTO_UPDATE_TESTS'] = '1' # Enable auto-update
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
## Usage
|
|
74
|
+
|
|
75
|
+
### Basic Usage
|
|
76
|
+
|
|
77
|
+
```python
|
|
78
|
+
from selenium_selector_autocorrect import install_auto_correct_hook
|
|
79
|
+
|
|
80
|
+
install_auto_correct_hook()
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
### Advanced Usage
|
|
84
|
+
|
|
85
|
+
```python
|
|
86
|
+
from selenium_selector_autocorrect import (
|
|
87
|
+
install_auto_correct_hook,
|
|
88
|
+
get_auto_correct,
|
|
89
|
+
get_correction_tracker,
|
|
90
|
+
export_corrections_report
|
|
91
|
+
)
|
|
92
|
+
|
|
93
|
+
install_auto_correct_hook()
|
|
94
|
+
|
|
95
|
+
auto_correct = get_auto_correct()
|
|
96
|
+
auto_correct.enabled = True
|
|
97
|
+
auto_correct.suggest_better_selectors = False
|
|
98
|
+
|
|
99
|
+
# Export corrections report at end of test run
|
|
100
|
+
tracker = get_correction_tracker()
|
|
101
|
+
export_corrections_report("corrections_report.json")
|
|
102
|
+
tracker = get_correction_tracker()
|
|
103
|
+
export_corrections_report("corrections_report.json")
|
|
104
|
+
|
|
105
|
+
print(f"Total corrections: {len(tracker.get_corrections())}")
|
|
106
|
+
print(f"Successful corrections: {len(tracker.get_successful_corrections())}")
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
### Custom AI Provider
|
|
110
|
+
|
|
111
|
+
```python
|
|
112
|
+
from selenium_selector_autocorrect import AIProvider, configure_provider
|
|
113
|
+
|
|
114
|
+
class CustomAIProvider(AIProvider):
|
|
115
|
+
def is_available(self) -> bool:
|
|
116
|
+
return True
|
|
117
|
+
|
|
118
|
+
def suggest_selector(self, system_prompt: str, user_prompt: str):))
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
## How It Works
|
|
122
|
+
|
|
123
|
+
1. **Hook Installation**: Patches `WebDriverWait.until()` to add auto-correction
|
|
124
|
+
2. **Timeout Detection**: When a selector times out, the original exception is caught
|
|
125
|
+
3. **Page Analysis**: JavaScript extracts visible elements and their attributes
|
|
126
|
+
4. **AI Suggestion**: Sends page context to AI provider for selector suggestion
|
|
127
|
+
5. **Verification**: Tests the suggested selector
|
|
128
|
+
6. **Success Handling**: If successful, records the correction and optionally updates the test file
|
|
129
|
+
7. **Fallback**: If correction fails, raises the original TimeoutException
|
|
130
|
+
|
|
131
|
+
## AI Provider Setup
|
|
132
|
+
|
|
133
|
+
### Local AI Service
|
|
134
|
+
|
|
135
|
+
The package requires a local AI service with OpenAI-compatible API:
|
|
136
|
+
|
|
137
|
+
```bash
|
|
138
|
+
POST http://localhost:8765/v1/chat/completions
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
For file auto-updates:
|
|
142
|
+
```bash
|
|
143
|
+
POST http://localhost:8765/v1/workspace/files/read
|
|
144
|
+
POST http://localhost:8765/v1/workspace/files/edit
|
|
145
|
+
## Correction Reports
|
|
146
|
+
|
|
147
|
+
Export correction reports in JSON format:
|
|
148
|
+
|
|
149
|
+
```python
|
|
150
|
+
from selenium_selector_autocorrect import export_corrections_report
|
|
151
|
+
|
|
152
|
+
export_corrections_report("corrections_report.json")
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
Report format:
|
|
156
|
+
```json
|
|
157
|
+
{
|
|
158
|
+
"corrections": [
|
|
159
|
+
{
|
|
160
|
+
"original_by": "id",
|
|
161
|
+
"original_value": "old-selector",
|
|
162
|
+
"corrected_by": "css selector",
|
|
163
|
+
"corrected_value": ".new-selector",
|
|
164
|
+
"success": true,
|
|
165
|
+
"test_file": "/path/to/test.py",
|
|
166
|
+
"test_line": 42,
|
|
167
|
+
"timestamp": "2024-01-31T10:30:00"
|
|
168
|
+
}
|
|
169
|
+
],
|
|
170
|
+
"summary": {
|
|
171
|
+
"total": 10,
|
|
172
|
+
"successful": 8,
|
|
173
|
+
"generated_at": "2024-01-31T10:35:00"
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
```
|
|
177
|
+
|
|
178
|
+
## Best Practices
|
|
179
|
+
|
|
180
|
+
1. **Install Once**: Call `install_auto_correct_hook()` once at test suite startup (e.g., in `conftest.py`)
|
|
181
|
+
2. **Review Corrections**: Regularly review correction reports to identify brittle selectors
|
|
182
|
+
3. **Update Tests**: Use auto-update sparingly and review changes before committing
|
|
183
|
+
4. **Monitor AI Service**: Ensure your AI service is running and responsive
|
|
184
|
+
5. **Use Strong Selectors**: The tool helps with failures but writing robust selectors is still preferred
|
|
185
|
+
|
|
186
|
+
## Requirements
|
|
187
|
+
|
|
188
|
+
- Python >= 3.8
|
|
189
|
+
- selenium >= 4.0.0
|
|
190
|
+
- requests >= 2.25.0
|
|
191
|
+
|
|
192
|
+
## License
|
|
193
|
+
|
|
194
|
+
MITInstall hook once at test suite startup (e.g., in conftest.py)
|
|
195
|
+
2. Review correction reports regularly to identify brittle selectors
|
|
196
|
+
3. Use auto-update sparingly and review changes before committing
|
|
197
|
+
4. Ensure your AI service is running and responsive
|
|
198
|
+
5. Write robust selectors - the tool helps with failures but prevention is better
|
|
199
|
+
|
|
200
|
+
When contributing:
|
|
201
|
+
1. Follow PEP 8 style guidelines
|
|
202
|
+
2. Add tests for new features
|
|
203
|
+
3. Update documentation
|
|
204
|
+
4. No emojis in code or documentation
|
|
205
|
+
|
|
206
|
+
## Troubleshooting
|
|
207
|
+
|
|
208
|
+
### AI Service Not Available
|
|
209
|
+
|
|
210
|
+
Contributions are welcome! Please:
|
|
211
|
+
1. Follow PEP 8 style guidelines
|
|
212
|
+
2. Add tests for new features
|
|
213
|
+
3. Update documentation
|
|
214
|
+
4. Maintain consistency with existing code
|
|
215
|
+
|
|
216
|
+
**Possible causes**:
|
|
217
|
+
- `SELENIUM_AUTO_UPDATE_TESTS` not set to `"1"`
|
|
218
|
+
- Test file path not detected correctly
|
|
219
|
+
- Selector string not found in source file (check quotes)
|
|
220
|
+
|
|
221
|
+
### No Corrections Happening
|
|
222
|
+
Solution: Ensure your local AI service is running on the configured port.
|
|
223
|
+
|
|
224
|
+
### Test File Not Updated
|
|
225
|
+
|
|
226
|
+
Possible causes:
|
|
227
|
+
- `SELENIUM_AUTO_UPDATE_TESTS` not set to "1"
|
|
228
|
+
- Test file path not detected correctly
|
|
229
|
+
- Selector string not found in source file
|
|
230
|
+
|
|
231
|
+
### No Corrections Happening
|
|
232
|
+
|
|
233
|
+
Check:
|
|
234
|
+
1. Hook is installed - look for log message
|
|
235
|
+
2. AI service is available - check `get_auto_correct().is_service_available()`
|
|
236
|
+
3. Auto-correct is enabled - c
|
|
237
|
+
See CHANGELOG.md for version history and changes.
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=61.0", "wheel"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "selenium-selector-autocorrect"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "Automatic Selenium selector correction using AI when elements fail to be found"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.8"
|
|
11
|
+
license = {text = "MIT"}
|
|
12
|
+
authors = [
|
|
13
|
+
{name = "Marty Zhou", email = "marty.zhou@example.com"}
|
|
14
|
+
]
|
|
15
|
+
maintainers = [
|
|
16
|
+
{name = "Marty Zhou", email = "marty.zhou@example.com"}
|
|
17
|
+
]
|
|
18
|
+
keywords = [
|
|
19
|
+
"selenium",
|
|
20
|
+
"testing",
|
|
21
|
+
"automation",
|
|
22
|
+
"ai",
|
|
23
|
+
"web-testing",
|
|
24
|
+
"test-automation",
|
|
25
|
+
"selector",
|
|
26
|
+
"auto-correction"
|
|
27
|
+
]
|
|
28
|
+
classifiers = [
|
|
29
|
+
"Development Status :: 4 - Beta",
|
|
30
|
+
"Intended Audience :: Developers",
|
|
31
|
+
"License :: OSI Approved :: MIT License",
|
|
32
|
+
"Operating System :: OS Independent",
|
|
33
|
+
"Programming Language :: Python :: 3",
|
|
34
|
+
"Programming Language :: Python :: 3.8",
|
|
35
|
+
"Programming Language :: Python :: 3.9",
|
|
36
|
+
"Programming Language :: Python :: 3.10",
|
|
37
|
+
"Programming Language :: Python :: 3.11",
|
|
38
|
+
"Programming Language :: Python :: 3.12",
|
|
39
|
+
"Topic :: Software Development :: Testing",
|
|
40
|
+
"Topic :: Software Development :: Quality Assurance",
|
|
41
|
+
"Framework :: Pytest",
|
|
42
|
+
]
|
|
43
|
+
dependencies = [
|
|
44
|
+
"selenium>=4.0.0",
|
|
45
|
+
"requests>=2.25.0",
|
|
46
|
+
]
|
|
47
|
+
|
|
48
|
+
[project.optional-dependencies]
|
|
49
|
+
dev = [
|
|
50
|
+
"pytest>=7.0.0",
|
|
51
|
+
"pytest-cov>=4.0.0",
|
|
52
|
+
"black>=23.0.0",
|
|
53
|
+
"isort>=5.12.0",
|
|
54
|
+
"mypy>=1.0.0",
|
|
55
|
+
"ruff>=0.1.0",
|
|
56
|
+
]
|
|
57
|
+
|
|
58
|
+
[project.urls]
|
|
59
|
+
Homepage = "https://github.com/MartyZhou/selenium-selector-autocorrect"
|
|
60
|
+
Documentation = "https://github.com/MartyZhou/selenium-selector-autocorrect#readme"
|
|
61
|
+
Repository = "https://github.com/MartyZhou/selenium-selector-autocorrect"
|
|
62
|
+
Issues = "https://github.com/MartyZhou/selenium-selector-autocorrect/issues"
|
|
63
|
+
Changelog = "https://github.com/MartyZhou/selenium-selector-autocorrect/blob/main/CHANGELOG.md"
|
|
64
|
+
|
|
65
|
+
[tool.setuptools]
|
|
66
|
+
packages = ["selenium_selector_autocorrect"]
|
|
67
|
+
package-dir = {"" = "src"}
|
|
68
|
+
|
|
69
|
+
[tool.setuptools.package-data]
|
|
70
|
+
selenium_selector_autocorrect = ["py.typed"]
|
|
71
|
+
|
|
72
|
+
[tool.black]
|
|
73
|
+
line-length = 120
|
|
74
|
+
target-version = ['py38', 'py39', 'py310', 'py311', 'py312']
|
|
75
|
+
include = '\.pyi?$'
|
|
76
|
+
|
|
77
|
+
[tool.isort]
|
|
78
|
+
profile = "black"
|
|
79
|
+
line_length = 120
|
|
80
|
+
src_paths = ["src"]
|
|
81
|
+
|
|
82
|
+
[tool.mypy]
|
|
83
|
+
python_version = "3.8"
|
|
84
|
+
warn_return_any = true
|
|
85
|
+
warn_unused_configs = true
|
|
86
|
+
disallow_untyped_defs = false
|
|
87
|
+
ignore_missing_imports = true
|
|
88
|
+
|
|
89
|
+
[tool.ruff]
|
|
90
|
+
line-length = 120
|
|
91
|
+
target-version = "py38"
|
|
92
|
+
src = ["src"]
|
|
93
|
+
select = [
|
|
94
|
+
"E", # pycodestyle errors
|
|
95
|
+
"W", # pycodestyle warnings
|
|
96
|
+
"F", # pyflakes
|
|
97
|
+
"I", # isort
|
|
98
|
+
"B", # flake8-bugbear
|
|
99
|
+
"C4", # flake8-comprehensions
|
|
100
|
+
"UP", # pyupgrade
|
|
101
|
+
]
|
|
102
|
+
ignore = [
|
|
103
|
+
"E501", # line too long (handled by black)
|
|
104
|
+
"B008", # do not perform function calls in argument defaults
|
|
105
|
+
"C901", # too complex
|
|
106
|
+
]
|
|
107
|
+
|
|
108
|
+
[tool.ruff.per-file-ignores]
|
|
109
|
+
"__init__.py" = ["F401"]
|
|
110
|
+
"tests/**" = ["F401", "F841"]
|
|
111
|
+
|
|
112
|
+
[tool.pytest.ini_options]
|
|
113
|
+
minversion = "7.0"
|
|
114
|
+
testpaths = ["tests"]
|
|
115
|
+
python_files = ["test_*.py", "*_test.py"]
|
|
116
|
+
addopts = "-ra --strict-markers --cov=src/selenium_selector_autocorrect --cov-report=term-missing --cov-report=html"
|
|
117
|
+
markers = [
|
|
118
|
+
"unit: Unit tests",
|
|
119
|
+
"integration: Integration tests",
|
|
120
|
+
"slow: Slow tests",
|
|
121
|
+
]
|
|
122
|
+
|
|
123
|
+
[tool.coverage.run]
|
|
124
|
+
source = ["src/selenium_selector_autocorrect"]
|
|
125
|
+
branch = true
|
|
126
|
+
omit = ["*/tests/*"]
|
|
127
|
+
|
|
128
|
+
[tool.coverage.report]
|
|
129
|
+
exclude_lines = [
|
|
130
|
+
"pragma: no cover",
|
|
131
|
+
"def __repr__",
|
|
132
|
+
"raise AssertionError",
|
|
133
|
+
"raise NotImplementedError",
|
|
134
|
+
"if __name__ == .__main__.:",
|
|
135
|
+
"if TYPE_CHECKING:",
|
|
136
|
+
"pass",
|
|
137
|
+
]
|
|
138
|
+
precision = 2
|