|
1 | 1 | let cssToXPath; |
| 2 | +let csstoxpath; |
2 | 3 | import { sprintf } from 'sprintf-js' |
3 | 4 |
|
4 | 5 | import { xpathLocator } from './utils.js' |
5 | 6 |
|
| 7 | +// Load modules at startup using top-level await |
| 8 | +try { |
| 9 | + cssToXPath = (await import('css-to-xpath')).default; |
| 10 | +} catch (e) { |
| 11 | + console.warn('css-to-xpath module not available:', e.message); |
| 12 | +} |
| 13 | + |
| 14 | +try { |
| 15 | + csstoxpath = (await import('csstoxpath')).default; |
| 16 | +} catch (e) { |
| 17 | + console.warn('csstoxpath module not available:', e.message); |
| 18 | +} |
| 19 | + |
6 | 20 | const locatorTypes = ['css', 'by', 'xpath', 'id', 'name', 'fuzzy', 'frame', 'shadow', 'pw']; |
7 | 21 | /** @class */ |
8 | 22 | class Locator { |
@@ -177,14 +191,21 @@ class Locator { |
177 | 191 | const locator = `${this.value}${pseudoSelector}`; |
178 | 192 | const limitation = [':nth-of-type', ':first-of-type', ':last-of-type', ':nth-last-child', ':nth-last-of-type', ':checked', ':disabled', ':enabled', ':required', ':lang', ':nth-child', ':has']; |
179 | 193 |
|
| 194 | + let converter; |
180 | 195 | if (limitation.some(item => locator.includes(item))) { |
181 | | - cssToXPath = (await import('css-to-xpath')).default; |
| 196 | + if (!cssToXPath) { |
| 197 | + throw new Error('css-to-xpath module not available. Install with: npm install css-to-xpath'); |
| 198 | + } |
| 199 | + converter = cssToXPath; |
182 | 200 | } else { |
183 | | - cssToXPath = (await import('csstoxpath')).default; |
| 201 | + if (!csstoxpath) { |
| 202 | + throw new Error('csstoxpath module not available. Install with: npm install csstoxpath'); |
| 203 | + } |
| 204 | + converter = csstoxpath; |
184 | 205 | } |
185 | 206 |
|
186 | 207 | if (this.isXPath()) return this.value; |
187 | | - if (this.isCSS()) return cssToXPath(locator); |
| 208 | + if (this.isCSS()) return converter(locator); |
188 | 209 |
|
189 | 210 | throw new Error('Can\'t be converted to XPath'); |
190 | 211 | } |
|
0 commit comments