Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion packages/webdriver/src/node/bidi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,9 @@ export async function connectWebsocket(candidateUrls: string[], options?: WebSoc
return { promise, index }
})

let connectionTimeoutId: ReturnType<typeof setTimeout> | undefined
const connectionTimeoutPromise = new Promise<undefined>((resolve) => {
setTimeout(() => {
connectionTimeoutId = setTimeout(() => {
log.error(`Could not connect to Bidi protocol of any candidate url in time: "${candidateUrls.join('", "')}"`)
return resolve(undefined)
}, CONNECTION_TIMEOUT)
Expand All @@ -88,6 +89,10 @@ export async function connectWebsocket(candidateUrls: string[], options?: WebSoc
connectionTimeoutPromise,
])

if (connectionTimeoutId) {
clearTimeout(connectionTimeoutId)
}

const socketsToCleanup = wsInfo ? websockets.filter((_, index) => wsInfo.index !== index) : websockets
for (const socket of socketsToCleanup) {
socket.removeAllListeners()
Expand Down
19 changes: 19 additions & 0 deletions packages/webdriver/tests/node/bidi.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ const log = logger('test')
describe('Bidi Node.js implementation', () => {
beforeEach(() => {
clearInstances()
vi.useRealTimers()
vi.clearAllMocks()
})

it('listWebsocketCandidateUrls', async () => {
Expand Down Expand Up @@ -89,6 +91,23 @@ describe('Bidi Node.js implementation', () => {
expect(log.error).not.toHaveBeenCalled()
})

it('does not log a timeout after a successful connection', async () => {
vi.useFakeTimers()
const wsPromise = createBidiConnection('ws://foo/bar')
await vi.advanceTimersByTimeAsync(0)

instances[1].once.mock.calls[0][1]() // success callback

const ws = await wsPromise as any
await vi.advanceTimersByTimeAsync(10000)

expect(ws.wsUrl).toBe('ws://127.0.0.1/bar')
expect(log.info).toHaveBeenCalledWith(
'Connected to Bidi protocol at ws://127.0.0.1/bar'
)
expect(log.error).not.toHaveBeenCalled()
})

it('createBidiConnection returns undefined if no socket connection is established', async () => {
const wsPromise = createBidiConnection('ws://foo/bar')
await new Promise((resolve) => setTimeout(resolve, 100))
Expand Down
Loading