Skip to content

Commit 325b0df

Browse files
chore: remove unnecessary interfaces wrapping regular selenium types
1 parent fa58ef3 commit 325b0df

7 files changed

Lines changed: 34 additions & 103 deletions

File tree

src/firefox/core.ts

Lines changed: 9 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -2,83 +2,14 @@
22
* Core WebDriver + BiDi connection management
33
*/
44

5-
import { Builder, Browser, Capabilities } from 'selenium-webdriver';
5+
import { Builder, Browser, Capabilities, WebDriver } from 'selenium-webdriver';
66
import firefox from 'selenium-webdriver/firefox.js';
77
import { mkdirSync, openSync, closeSync } from 'node:fs';
88
import { homedir } from 'node:os';
99
import { join } from 'node:path';
1010
import type { FirefoxLaunchOptions } from './types.js';
1111
import { log, logDebug } from '../utils/logger.js';
1212

13-
// ---------------------------------------------------------------------------
14-
// Shared driver interface — the minimal surface used by all consumers
15-
// (DomInteractions, PageManagement, SnapshotManager, UidResolver).
16-
// ---------------------------------------------------------------------------
17-
18-
export interface IElement {
19-
click(): Promise<void>;
20-
clear(): Promise<void>;
21-
sendKeys(...args: Array<string | number>): Promise<void>;
22-
isDisplayed(): Promise<boolean>;
23-
takeScreenshot(): Promise<string>;
24-
}
25-
26-
export interface IBiDiSocket {
27-
readyState: number;
28-
on(event: string, listener: (data: unknown) => void): void;
29-
off(event: string, listener: (data: unknown) => void): void;
30-
send(data: string): void;
31-
}
32-
33-
export interface IBiDi {
34-
socket: IBiDiSocket;
35-
subscribe?: (event: string, contexts?: string[]) => Promise<void>;
36-
}
37-
38-
/* eslint-disable @typescript-eslint/no-explicit-any */
39-
export interface IDriver {
40-
getTitle(): Promise<string>;
41-
getCurrentUrl(): Promise<string>;
42-
getWindowHandle(): Promise<string>;
43-
getAllWindowHandles(): Promise<string[]>;
44-
get(url: string): Promise<void>;
45-
getPageSource(): Promise<string>;
46-
executeScript<T>(script: string | ((...a: any[]) => any), ...args: unknown[]): Promise<T>;
47-
executeAsyncScript<T>(script: string | ((...a: any[]) => any), ...args: unknown[]): Promise<T>;
48-
takeScreenshot(): Promise<string>;
49-
close(): Promise<void>;
50-
findElement(locator: any): Promise<IElement>;
51-
switchTo(): {
52-
window(handle: string): Promise<void>;
53-
newWindow(type: string): Promise<{ handle: string }>;
54-
alert(): Promise<{
55-
accept(): Promise<void>;
56-
dismiss(): Promise<void>;
57-
getText(): Promise<string>;
58-
sendKeys(text: string): Promise<void>;
59-
}>;
60-
};
61-
navigate(): {
62-
back(): Promise<void>;
63-
forward(): Promise<void>;
64-
refresh(): Promise<void>;
65-
};
66-
manage(): {
67-
window(): {
68-
setRect(rect: { width: number; height: number }): Promise<void>;
69-
};
70-
};
71-
actions(opts?: { async?: boolean }): {
72-
move(opts: { x?: number; y?: number; origin?: unknown }): any;
73-
click(): any;
74-
doubleClick(el?: unknown): any;
75-
perform(): Promise<void>;
76-
clear(): Promise<void>;
77-
};
78-
getBidi(): Promise<IBiDi>;
79-
}
80-
/* eslint-enable @typescript-eslint/no-explicit-any */
81-
8213
// ---------------------------------------------------------------------------
8314
// Geckodriver binary finder — used only for --connect-existing mode
8415
// ---------------------------------------------------------------------------
@@ -135,7 +66,7 @@ async function findGeckodriver(): Promise<string> {
13566
}
13667

13768
export class FirefoxCore {
138-
private driver: IDriver | null = null;
69+
private driver: WebDriver | null = null;
13970
private currentContextId: string | null = null;
14071
private originalEnv: Record<string, string | undefined> = {};
14172
private logFilePath: string | undefined;
@@ -177,8 +108,7 @@ export class FirefoxCore {
177108
// createSession() returns synchronously; the session is established async under the hood.
178109
// Passing geckodriverPath to ServiceBuilder prevents getBinaryPaths() from running,
179110
// which would otherwise invoke selenium-manager with --browser firefox.
180-
const seleniumDriver = firefox.Driver.createSession(caps, serviceBuilder.build());
181-
this.driver = seleniumDriver as unknown as IDriver;
111+
this.driver = firefox.Driver.createSession(caps, serviceBuilder.build());
182112
} else {
183113
// Set up output file for capturing Firefox stdout/stderr
184114
if (this.options.logFile) {
@@ -255,12 +185,11 @@ export class FirefoxCore {
255185
log(`📝 Capturing Firefox output to: ${this.logFilePath}`);
256186
}
257187

258-
// selenium WebDriver satisfies IDriver structurally at runtime
259-
this.driver = (await new Builder()
188+
this.driver = await new Builder()
260189
.forBrowser(Browser.FIREFOX)
261190
.setFirefoxOptions(firefoxOptions)
262191
.setFirefoxService(serviceBuilder)
263-
.build()) as unknown as IDriver;
192+
.build();
264193
}
265194

266195
log(
@@ -285,7 +214,7 @@ export class FirefoxCore {
285214
/**
286215
* Get driver instance (throw if not connected)
287216
*/
288-
getDriver(): IDriver {
217+
getDriver(): WebDriver {
289218
if (!this.driver) {
290219
throw new Error('Driver not connected');
291220
}
@@ -395,7 +324,9 @@ export class FirefoxCore {
395324
}
396325

397326
const bidi = await this.driver.getBidi();
398-
const ws = bidi.socket;
327+
// bidi.socket is a Node.js `ws` WebSocket (EventEmitter-style), but typed as browser WebSocket
328+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
329+
const ws = bidi.socket as any;
399330

400331
// Wait for WebSocket to be ready before sending
401332
await this.waitForWebSocketOpen(ws);

src/firefox/dom.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,12 @@
22
* DOM interactions: evaluate, element lookup, input actions
33
*/
44

5-
import { Key } from 'selenium-webdriver';
6-
import type { IDriver, IElement } from './core.js';
5+
import { By, Key, WebDriver, WebElement } from 'selenium-webdriver';
76

87
export class DomInteractions {
98
constructor(
10-
private driver: IDriver,
11-
private resolveUid?: (uid: string) => Promise<IElement>
9+
private driver: WebDriver,
10+
private resolveUid?: (uid: string) => Promise<WebElement>
1211
) {}
1312

1413
/**
@@ -33,12 +32,12 @@ export class DomInteractions {
3332
/**
3433
* Poll for an element matching a CSS selector until found or timeout.
3534
*/
36-
private async waitForElement(selector: string, timeout = 5000): Promise<IElement> {
35+
private async waitForElement(selector: string, timeout = 5000): Promise<WebElement> {
3736
const deadline = Date.now() + timeout;
3837
let lastError: Error | undefined;
3938
while (Date.now() < deadline) {
4039
try {
41-
return await this.driver.findElement({ using: 'css selector', value: selector });
40+
return await this.driver.findElement(By.css(selector));
4241
} catch (e) {
4342
lastError = e instanceof Error ? e : new Error(String(e));
4443
}
@@ -50,7 +49,7 @@ export class DomInteractions {
5049
/**
5150
* Wait until an element reports isDisplayed(), ignoring failures.
5251
*/
53-
private async waitForVisible(el: IElement, timeout = 5000): Promise<void> {
52+
private async waitForVisible(el: WebElement, timeout = 5000): Promise<void> {
5453
const deadline = Date.now() + timeout;
5554
while (Date.now() < deadline) {
5655
try {

src/firefox/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
*/
44

55
import type { FirefoxLaunchOptions, ConsoleMessage } from './types.js';
6-
import { FirefoxCore, type IElement } from './core.js';
6+
import { WebElement } from 'selenium-webdriver';
7+
import { FirefoxCore } from './core.js';
78
import { logDebug } from '../utils/logger.js';
89
import { ConsoleEvents, NetworkEvents } from './events/index.js';
910
import { DomInteractions } from './dom.js';
@@ -366,7 +367,7 @@ export class FirefoxClient {
366367
return this.snapshot.resolveUidToSelector(uid);
367368
}
368369

369-
async resolveUidToElement(uid: string): Promise<IElement> {
370+
async resolveUidToElement(uid: string): Promise<WebElement> {
370371
if (!this.snapshot) {
371372
throw new Error('Not connected');
372373
}

src/firefox/pages.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22
* Page/Tab/Window management
33
*/
44

5-
import type { IDriver } from './core.js';
5+
import { WebDriver } from 'selenium-webdriver';
66
import { log } from '../utils/logger.js';
77

88
export class PageManagement {
99
constructor(
10-
private driver: IDriver,
10+
private driver: WebDriver,
1111
private getCurrentContextId: () => string | null,
1212
private setCurrentContextId: (id: string) => void
1313
) {}

src/firefox/snapshot/manager.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* Handles snapshot creation using bundled injected script
44
*/
55

6-
import type { IDriver, IElement } from '../core.js';
6+
import { WebDriver, WebElement } from 'selenium-webdriver';
77
import { readFileSync } from 'node:fs';
88
import { dirname, resolve } from 'node:path';
99
import { fileURLToPath } from 'node:url';
@@ -25,12 +25,12 @@ export interface SnapshotOptions {
2525
* Uses bundled injected script for snapshot creation
2626
*/
2727
export class SnapshotManager {
28-
private driver: IDriver;
28+
private driver: WebDriver;
2929
private resolver: UidResolver;
3030
private injectedScript: string | null = null;
3131
private currentSnapshotId = 0;
3232

33-
constructor(driver: IDriver) {
33+
constructor(driver: WebDriver) {
3434
this.driver = driver;
3535
this.resolver = new UidResolver(driver);
3636
}
@@ -159,7 +159,7 @@ export class SnapshotManager {
159159
/**
160160
* Resolve UID to WebElement (with staleness check and caching)
161161
*/
162-
async resolveUidToElement(uid: string): Promise<IElement> {
162+
async resolveUidToElement(uid: string): Promise<WebElement> {
163163
return await this.resolver.resolveUidToElement(uid);
164164
}
165165

src/firefox/snapshot/resolver.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33
* Handles UID validation, resolution to selectors/elements, and element caching
44
*/
55

6-
import type { IDriver, IElement } from '../core.js';
6+
import { By, WebDriver, WebElement } from 'selenium-webdriver';
77
import { logDebug } from '../../utils/logger.js';
88
import type { UidEntry } from './types.js';
99

10-
interface IElementCacheEntry {
10+
interface WebElementCacheEntry {
1111
selector: string;
1212
xpath?: string;
13-
cachedElement: IElement;
13+
cachedElement: WebElement;
1414
snapshotId: number;
1515
timestamp: number;
1616
}
@@ -21,10 +21,10 @@ interface IElementCacheEntry {
2121
*/
2222
export class UidResolver {
2323
private uidToEntry = new Map<string, UidEntry>();
24-
private elementCache = new Map<string, IElementCacheEntry>();
24+
private elementCache = new Map<string, WebElementCacheEntry>();
2525
private currentSnapshotId = 0;
2626

27-
constructor(private driver: IDriver) {}
27+
constructor(private driver: WebDriver) {}
2828

2929
/**
3030
* Update current snapshot ID
@@ -98,7 +98,7 @@ export class UidResolver {
9898
* Resolve UID to element (with staleness check and caching)
9999
* Tries CSS first, falls back to XPath
100100
*/
101-
async resolveUidToElement(uid: string): Promise<IElement> {
101+
async resolveUidToElement(uid: string): Promise<WebElement> {
102102
this.validateUid(uid);
103103

104104
const entry = this.uidToEntry.get(uid);
@@ -122,7 +122,7 @@ export class UidResolver {
122122

123123
// Try CSS selector first
124124
try {
125-
const element = await this.driver.findElement({ using: 'css selector', value: entry.css });
125+
const element = await this.driver.findElement(By.css(entry.css));
126126

127127
// Update cache
128128
this.elementCache.set(uid, {
@@ -142,7 +142,7 @@ export class UidResolver {
142142
const xpathSelector = entry.xpath;
143143
if (xpathSelector) {
144144
try {
145-
const element = await this.driver.findElement({ using: 'xpath', value: xpathSelector });
145+
const element = await this.driver.findElement(By.xpath(xpathSelector));
146146

147147
// Update cache
148148
this.elementCache.set(uid, {

src/firefox/snapshot/types.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* Snapshot types and interfaces
33
*/
44

5-
import type { IElement } from '../core.js';
5+
import type { WebElement } from 'selenium-webdriver';
66

77
/**
88
* UID entry with CSS and XPath selectors
@@ -100,7 +100,7 @@ export interface InjectedScriptResult {
100100
export interface ElementCacheEntry {
101101
selector: string;
102102
xpath?: string;
103-
cachedElement?: IElement;
103+
cachedElement?: WebElement;
104104
snapshotId: number;
105105
timestamp: number;
106106
}

0 commit comments

Comments
 (0)